SPARQLer 🥂

Filters

All Builders of different types (SELECT, INSERT and DELETE, CONSTRUCT) have their conditions (the WHERE part...), which can be chained to the Builder itself with a set of dedicated methods.


$client->doSelect()
    ->where('rdf:predicate', 'value')->get();
Show generated query

The most common condition: the predicate is applied to the implicit OwnSubject of the query. By default, the first parameter is wrapped within a Iri term, the second in a Plain (assumes it is a string).
use MadBob\Sparqler\Terms\Iri;

$client->doSelect()
    ->where('rdf:predicate', new Iri('a:subject'))->get();
Show generated query

Passing Term objects as parameters, you can enforce their meaning and the way those will be appended into the query. This is true for every conditional function of the Builder.
$client->doSelect()
    ->where('rdf:predicate', function($query) {
        $query->where('rdf:other', 'value');
    })->get();
Show generated query

The value of the condition can be a sub-query: a new random Variable will be used for further comparison an evaluations.
use MadBob\Sparqler\Terms\Variable;

$variable = new Variable('foo');

$client->doSelect()
    ->where('rdf:predicate1', $variable)
    ->where($variable, 'rdf:predicate2', 'value')->get();
Show generated query

When three parameters are passed to where(), they become a complete Triple.
use MadBob\Sparqler\Terms\Variable;

$client->doSelect(['rdf:predicate'])
    ->whereOptional('rdf:predicate', new Variable('opt'))->get();
Show generated query

An OPTIONAL condition is to filter entities having a given predicate with a given value, or not that predicate at all. To optionally select a given predicate into a SELECT query it is more convenient to use the Optional term.
$client->doSelect()
    ->where('rdf:predicate', '!=', 'value')->get();
Show generated query

$client->doSelect()
    ->where('rdf:predicate', 'lang', 'it')->get();
Show generated query

$client->doSelect()
    ->where('rdf:predicate', 'regex', 'end$')->get();
Show generated query

Basic evaluation functions are built into the where() function, which generates proper FILTER conditions.
Supported operators:
  • = the default
  • < minor than
  • > major than
  • <= minor or equal than
  • >= major or equal than
  • != not equal to
  • regex matches a regular expression
  • in alternative to whereIn()
  • lang filter strings by language
$client->doSelect()
    ->whereIn('rdf:predicate', ['value1', 'value2'])->get();
Show generated query

Many different values can be matched at once.
use MadBob\Sparqler\Terms\Iri;

$client->doSelect()
    ->whereReverse('rdf:predicate', new Iri('a:subject'))->get();
Show generated query

To reverse the operands of the Triple, and use the implicit OwnSubject as object instead of subject.
$client->doSelect()
    ->whereReverse('rdf:predicate', function($query) {
        $query->where('rdf:other', 'value');
    })->get();
Show generated query

Also reverted relations can be extended with sub-queries, in which the subject will be inherited from the parent one.
$client->doSelect()
    ->whereRaw('FILTER(true)')->get();
Show generated query

An arbitrary expression can be appended to the query.
$client->doSelect()
    ->filterNotExists(function($query) {
        $query->where('rdf:predicate', 'value');
    })->get();
Show generated query

Reverts the filter, matching entities for which the given sub query produces no results.
$client->doSelect()
    ->where('rdf:predicate1', 'value')
    ->minus(function($query) {
        $query->where('rdf:predicate2', 'another value');
    })->get();
Show generated query

Part of the entities matching a query can be excluded from the final result if they match some other condition, using the MINUS operator.
$client->doSelect()
    ->where('rdf:predicate1', 'value')
    ->or(function($query) {
        $query->where('rdf:predicate2', 'value');
    })
    ->get();
Show generated query

The entities may match a set of conditions or another, using the UNION operator.
Note that the or() function only accepts a callable function, where a (more or less complex) sub query can be expressed.

SPARQLer 🥂