PHP code example of effectiveactivism / sparql-client
1. Go to this page and download the library: Download effectiveactivism/sparql-client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
effectiveactivism / sparql-client example snippets
namespace App\Controller;
use EffectiveActivism\SparQlClient\Client\SparQlClientInterface;
use EffectiveActivism\SparQlClient\Syntax\Pattern\Triple\Triple;
use EffectiveActivism\SparQlClient\Syntax\Term\Iri\PrefixedIri;
use EffectiveActivism\SparQlClient\Syntax\Term\Literal\PlainLiteral;
use EffectiveActivism\SparQlClient\Syntax\Term\TermInterface;
use EffectiveActivism\SparQlClient\Syntax\Term\Variable\Variable;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class MyController extends AbstractController
{
public function view(SparQlClientInterface $sparQlClient)
{
// Add the 'schema' namespace.
$sparQlClient->setExtraNamespaces(['schema' => 'http://schema.org/']);
// Add a subject as a variable '_subject'.
$subject = new Variable('subject');
// Add a prefixed IRI of the form 'schema:headline'.
$predicate = new PrefixedIri('schema', 'headline');
// Add a plain literal of the form 'Lorem@la'.
$object = new PlainLiteral('Lorem', 'la');
// Add a triple that contains all the above terms.
$triple = new Triple($subject, $predicate, $object);
// Create a select statement.
$selectStatement = $sparQlClient->select([$subject])->where([$triple]);
// Perform the query.
$sets = $sparQlClient->execute($selectStatement);
// The result will contain each 'subject' found.
/** @var TermInterface[] $set */
foreach ($sets as $set) {
dump($set[$subject->getVariableName()]);
}
}
}
use EffectiveActivism\SparQlClient\Syntax\Order\Asc;
use EffectiveActivism\SparQlClient\Syntax\Term\Variable\Variable;
use EffectiveActivism\SparQlClient\Syntax\Pattern\Constraint\Operator\Binary\Multiply;
use EffectiveActivism\SparQlClient\Syntax\Term\Literal\TypedLiteral;
$selectStatement->limit(3);
$selectStatement->offset(2);
$orderVariable = new Variable('orderByThis');
$multiplier = new TypedLiteral(2);
$selectStatement->orderBy([new Asc(new Multiply($orderVariable, $multiplier))]);
namespace App\Controller;
use EffectiveActivism\SparQlClient\Client\SparQlClientInterface;
use EffectiveActivism\SparQlClient\Syntax\Pattern\Triple\Triple;
use EffectiveActivism\SparQlClient\Syntax\Term\Iri\Iri;
use EffectiveActivism\SparQlClient\Syntax\Term\Iri\PrefixedIri;
use EffectiveActivism\SparQlClient\Syntax\Term\Literal\PlainLiteral;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class MyController extends AbstractController
{
public function view(SparQlClientInterface $sparQlClient)
{
// Add the 'schema' namespace.
$sparQlClient->setExtraNamespaces(['schema' => 'http://schema.org/']);
// Add a subject as a variable '_subject'.
$subject = new Iri('urn:uuid:5b08b896-a8ee-11eb-acf0-a3d5edd5c2a6');
// Add a prefixed IRI of the form 'schema:headline'.
$predicate = new PrefixedIri('schema', 'headline');
// Add a plain literal of the form 'Lorem@la'.
$object = new PlainLiteral('Lorem', 'la');
// Add a triple that contains all the above terms.
$triple = new Triple($subject, $predicate, $object);
// Create an ask statement.
$askStatement = $sparQlClient->ask()->where([$triple]);
// Perform the query.
$result = $sparQlClient->execute($askStatement);
// The result will be a boolean value.
if ($result === true) {
dump('yes');
}
}
}
namespace App\Controller;
use EffectiveActivism\SparQlClient\Client\SparQlClientInterface;
use EffectiveActivism\SparQlClient\Syntax\Pattern\Triple\Triple;
use EffectiveActivism\SparQlClient\Syntax\Term\Iri\PrefixedIri;
use EffectiveActivism\SparQlClient\Syntax\Term\Literal\PlainLiteral;
use EffectiveActivism\SparQlClient\Syntax\Term\TermInterface;
use EffectiveActivism\SparQlClient\Syntax\Term\Variable\Variable;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class MyController extends AbstractController
{
public function view(SparQlClientInterface $sparQlClient)
{
// Add the 'schema' namespace.
$sparQlClient->setExtraNamespaces(['schema' => 'http://schema.org/']);
// Add a subject as a variable '_subject'.
$subject = new Variable('subject');
// Add a prefixed IRI of the form 'schema:headline'.
$predicate = new PrefixedIri('schema', 'headline');
// Add a plain literal of the form 'Lorem@la'.
$object = new PlainLiteral('Lorem', 'la');
// Add a triple that contains all the above terms.
$triple = new Triple($subject, $predicate, $object);
// Create a construct statement.
$constructStatement = $sparQlClient->construct([$triple])->where([$triple]);
// Perform the query.
$sets = $sparQlClient->execute($constructStatement);
// The result will contain each 'subject' found.
/** @var TermInterface[] $set */
foreach ($sets as $set) {
dump($set[$subject->getVariableName()]);
}
}
}
namespace App\Controller;
use EffectiveActivism\SparQlClient\Client\SparQlClientInterface;
use EffectiveActivism\SparQlClient\Syntax\Pattern\Triple\Triple;
use EffectiveActivism\SparQlClient\Syntax\Term\Iri\Iri;
use EffectiveActivism\SparQlClient\Syntax\Term\Iri\PrefixedIri;
use EffectiveActivism\SparQlClient\Syntax\Term\Literal\PlainLiteral;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class MyController extends AbstractController
{
public function view(SparQlClientInterface $sparQlClient)
{
// Add the 'schema' namespace.
$sparQlClient->setExtraNamespaces(['schema' => 'http://schema.org/']);
// Add a subject as a variable '_subject'.
$subject = new Iri('urn:uuid:a61d21a4-824d-11eb-95c3-ebff6d3fb918');
// Add a prefixed IRI of the form 'schema:headline'.
$predicate = new PrefixedIri('schema', 'headline');
// Add a plain literal of the form 'Lorem@la'.
$object = new PlainLiteral('Lorem', 'la');
// Add a triple that contains all the above terms.
$triple = new Triple($subject, $predicate, $object);
// Create an insert statement.
$insertStatement = $sparQlClient->insert([$triple]);
// Perform the update.
$sparQlClient->execute($insertStatement);
}
}
namespace App\Controller;
use EffectiveActivism\SparQlClient\Client\SparQlClientInterface;
use EffectiveActivism\SparQlClient\Syntax\Pattern\Triple\Triple;
use EffectiveActivism\SparQlClient\Syntax\Term\Iri\PrefixedIri;
use EffectiveActivism\SparQlClient\Syntax\Term\Literal\PlainLiteral;
use EffectiveActivism\SparQlClient\Syntax\Term\Variable\Variable;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class MyController extends AbstractController
{
public function view(SparQlClientInterface $sparQlClient)
{
// Add the 'schema' namespace.
$sparQlClient->setExtraNamespaces(['schema' => 'http://schema.org/']);
// Add a subject as a variable '_subject'.
$subject = new Variable('subject');
// Add a prefixed IRI of the form 'schema:headline'.
$predicate = new PrefixedIri('schema', 'headline');
// Add a plain literal of the form 'Lorem@la'.
$object = new PlainLiteral('Lorem', 'la');
// Add a triple that contains all the above terms.
$tripleToDelete = new Triple($subject, $predicate, $object);
// Add a prefixed IRI of the form 'rdf:type'.
$predicate2 = new PrefixedIri('rdf', 'type');
// Add a prefixed IRI of the form 'schema:Article'.
$object2 = new PrefixedIri('schema', 'Article');
$tripleToFilter = new Triple($subject, $predicate2, $object2);
// Create a delete statement.
$deleteStatement = $sparQlClient->delete([$tripleToDelete])->where([$tripleToFilter]);
// Perform the update.
$sparQlClient->execute($deleteStatement);
}
}
namespace App\Controller;
use EffectiveActivism\SparQlClient\Client\SparQlClientInterface;
use EffectiveActivism\SparQlClient\Syntax\Pattern\Triple\Triple;
use EffectiveActivism\SparQlClient\Syntax\Term\Iri\PrefixedIri;
use EffectiveActivism\SparQlClient\Syntax\Term\Literal\PlainLiteral;
use EffectiveActivism\SparQlClient\Syntax\Term\Variable\Variable;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class MyController extends AbstractController
{
public function view(SparQlClientInterface $sparQlClient)
{
// Add the 'schema' namespace.
$sparQlClient->setExtraNamespaces(['schema' => 'http://schema.org/']);
// Add a subject as a variable '_subject'.
$subject = new Variable('subject');
// Add a prefixed IRI of the form 'schema:headline'.
$predicate = new PrefixedIri('schema', 'headline');
// Add a plain literal of the form 'Lorem@la'.
$object = new PlainLiteral('Lorem', 'la');
// Add a triple that contains all the above terms.
$tripleToReplace = new Triple($subject, $predicate, $object);
// Add a prefixed IRI of the form 'rdf:type'.
$predicate2 = new PrefixedIri('schema', 'headline');
// Add a prefixed IRI of the form 'schema:Article'.
$object2 = new PlainLiteral('ipsum', 'la');
// Add a replacement triple.
$replacementTriple = new Triple($subject, $predicate2, $object2);
// Create a replace statement.
$replaceStatement = $sparQlClient
->replace([$tripleToReplace])
->with([$replacementTriple])
->where([$tripleToReplace]);
// Perform the update.
$sparQlClient->execute($replaceStatement);
}
}
use \EffectiveActivism\SparQlClient\Syntax\Term\Iri\PrefixedIri;
use \EffectiveActivism\SparQlClient\Syntax\Term\Path\InversePath;
$predicate = new PrefixedIri('schema', 'headline');
// Inverse predicate
$inversePredicate = new InversePath($predicate);
// The below will output "^schema:headline"
dump($inversePredicate->serialize());
use \EffectiveActivism\SparQlClient\Syntax\Term\Iri\PrefixedIri;
use \EffectiveActivism\SparQlClient\Syntax\Term\Path\InversePath;
use \EffectiveActivism\SparQlClient\Syntax\Term\Path\SequencePath;
$predicate = new PrefixedIri('schema', 'headline');
// Inverse predicate
$inversePredicate = new InversePath($predicate);
// Sequence of predicate and inverse predicate.
$sequencePath = new SequencePath($predicate, $inversePredicate);
// The below will output "schema:headline / ^schema:headline"
dump($sequencePath->serialize());
use \EffectiveActivism\SparQlClient\Syntax\Term\Iri\PrefixedIri;
use \EffectiveActivism\SparQlClient\Syntax\Term\Path\InversePath;
use \EffectiveActivism\SparQlClient\Syntax\Term\Set\NegatedPropertySet;
$predicate = new PrefixedIri('schema', 'headline');
// Inverse predicate
$inversePredicate = new InversePath($predicate);
// Negated set of predicate and inverse predicate.
$negatedSet = new NegatedPropertySet([$predicate, $inversePredicate]);
// The below will output "!(schema:headline | (^schema:headline))"
dump($negatedSet->serialize());
namespace App\Controller;
use EffectiveActivism\SparQlClient\Client\SparQlClientInterface;
use EffectiveActivism\SparQlClient\Syntax\Pattern\Assignment\Bind;
use EffectiveActivism\SparQlClient\Syntax\Pattern\Constraint\Operator\Binary\Multiply;use EffectiveActivism\SparQlClient\Syntax\Term\Literal\PlainLiteral;
use EffectiveActivism\SparQlClient\Syntax\Term\Literal\TypedLiteral;
use EffectiveActivism\SparQlClient\Syntax\Term\Variable\Variable;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class MyController extends AbstractController
{
public function view(SparQlClientInterface $sparQlClient)
{
$commentCountVariable = new Variable('commentCount');
$multipliedCommentCountVariable = new Variable('multipliedCommentCount');
$multiplier = new TypedLiteral(2);
// Bind an expression such as a multiplication to a variable.
$bind = new Bind(new Multiply($multiplier, $commentCountVariable), $multipliedCommentCountVariable);
$selectStatement = $sparQlClient
->select([$commentCountVariable])
->where([$bind]);
$sparQlClient->execute($selectStatement);
}
}
namespace App\Controller;
use EffectiveActivism\SparQlClient\Client\SparQlClientInterface;
use EffectiveActivism\SparQlClient\Syntax\Pattern\Assignment\Values;
use EffectiveActivism\SparQlClient\Syntax\Term\Literal\PlainLiteral;
use EffectiveActivism\SparQlClient\Syntax\Term\Literal\TypedLiteral;
use EffectiveActivism\SparQlClient\Syntax\Term\Variable\Variable;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class MyController extends AbstractController
{
public function view(SparQlClientInterface $sparQlClient)
{
$headlineVariable = new Variable('headline');
$commentCountVariable = new Variable('commentCount');
$headlineValue1 = new PlainLiteral('Lorem');
$headlineValue2 = new PlainLiteral('Ipsum');
$commentCountValue1 = new TypedLiteral(2);
$values = new Values([
$headlineVariable,
$commentCountVariable
], [
// Each sub-array has the same dimension as the variable array
// above.
[$headlineValue1, $headlineValue2],
// Null values are used to signify an undefined value.
[$commentCountValue1, null]
]);
$selectStatement = $sparQlClient
->select([$headlineVariable, $commentCountVariable])
->where([$values]);
$sparQlClient->execute($selectStatement);
}
}
use \EffectiveActivism\SparQlClient\Syntax\Term\Iri\PrefixedIri;
$subject = new PrefixedIri('‿schema', 'headline');
namespace App\Controller;
use EffectiveActivism\SparQlClient\Client\SparQlClientInterface;
use EffectiveActivism\SparQlClient\Syntax\Pattern\Triple\Triple;
use EffectiveActivism\SparQlClient\Syntax\Term\Iri\Iri;
use EffectiveActivism\SparQlClient\Syntax\Term\Iri\PrefixedIri;
use EffectiveActivism\SparQlClient\Syntax\Term\Literal\PlainLiteral;
use EffectiveActivism\SparQlClient\Syntax\Term\Variable\Variable;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class MyController extends AbstractController
{
public function view(SparQlClientInterface $sparQlClient)
{
$variable = new Variable('foo');
$subject = new Iri('urn:uuid:c40f9982-8322-11eb-b0ba-57776fae8cf3');
$predicate = new PrefixedIri('schema', 'headline');
$object = new PlainLiteral('Lorem', 'la');
$statement = $sparQlClient
->select([$variable])
->where([new Triple($subject, $predicate, $object)]);
// Throws an InvalidArgumentException.
$sparQlClient->execute($statement);
}
}
use \EffectiveActivism\SparQlClient\Syntax\Pattern\Optionally\Optionally;
$optionalClause = new Optionally([$triple, $filter]);
$statement->where([$triple, $optionalClause]);
use \EffectiveActivism\SparQlClient\Syntax\Pattern\Service\Service;
use \EffectiveActivism\SparQlClient\Syntax\Pattern\Triple\Triple;
use \EffectiveActivism\SparQlClient\Syntax\Term\Iri\PrefixedIri;
use \EffectiveActivism\SparQlClient\Syntax\Term\Literal\PlainLiteral;
use \EffectiveActivism\SparQlClient\Syntax\Term\Variable\Variable;
$service = new Service(
new PrefixedIri('bds', 'search'),
[
new Triple(
new Variable('object'),
new PrefixedIri('bds', 'search'),
new PlainLiteral('foo')
)
]
);
$statement->where([
$service,
new Triple(
new Variable('subject'),
new Variable('predicate'),
new Variable('object')
)
]);
namespace App\Controller;
use EffectiveActivism\SparQlClient\Client\SparQlClientInterface;
use EffectiveActivism\SparQlClient\Syntax\Pattern\Constraint\FilterNotExists;
use EffectiveActivism\SparQlClient\Syntax\Pattern\Triple\Triple;
use EffectiveActivism\SparQlClient\Syntax\Term\Iri\PrefixedIri;
use EffectiveActivism\SparQlClient\Syntax\Term\Literal\PlainLiteral;
use EffectiveActivism\SparQlClient\Syntax\Term\Variable\Variable;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class MyController extends AbstractController
{
public function view(SparQlClientInterface $sparQlClient)
{
$variable = new Variable('foo');
$predicate = new PrefixedIri('schema', 'headline');
$object = new PlainLiteral('Lorem');
$filterPredicate = new PrefixedIri('schema', 'identifier');
$filterObject = new PlainLiteral('13a5b1da-9060-11eb-a695-2bfde2d1d6bd');
$statement = $sparQlClient
->select([$variable])
->where([
new Triple($variable, $predicate, $object),
new FilterNotExists([
new Triple($variable, $filterPredicate, $filterObject)
]),
]);
$result = $sparQlClient->execute($statement);
}
}
use EffectiveActivism\SparQlClient\Syntax\Pattern\Constraint\Filter;
use EffectiveActivism\SparQlClient\Syntax\Pattern\Constraint\Operator\Binary\Equal;
use EffectiveActivism\SparQlClient\Syntax\Pattern\Constraint\Operator\Binary\NotEqual;
use EffectiveActivism\SparQlClient\Syntax\Term\Literal\PlainLiteral;
$object1 = new PlainLiteral('Lorem');
$object2 = new PlainLiteral('Ipsum');
$object3 = new PlainLiteral(12);
// Returns a filter of the form FILTER('Lorem' = 'Ipsum')
new Filter(new Equal($object1, $object2));
// Throws an InvalidArgumentException because the argument data types do not match.
new Filter(new NotEqual($object1, $object3));
namespace App\Controller;
use EffectiveActivism\SparQlClient\Client\SparQlClientInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
class MyController extends AbstractController
{
public function view(Request $request, SparQlClientInterface $sparQlClient)
{
/** @var UploadedFile $file */
$file = $request->files->get('file');
if ($sparQlClient->upload($file, 'application/rdf+xml')) {
dump('File uploaded!');
}
}
}
namespace App\Controller;
use EffectiveActivism\SparQlClient\Client\ShaclClientInterface;
use EffectiveActivism\SparQlClient\Client\SparQlClientInterface;
use EffectiveActivism\SparQlClient\Syntax\Pattern\Triple\Triple;
use EffectiveActivism\SparQlClient\Syntax\Pattern\Triple\TripleInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class MyController extends AbstractController
{
public function view(ShaclClientInterface $shaclClient, SparQlClientInterface $sparQlClient)
{
/** @var TripleInterface $triple */
$triple = new Triple(...);
$statement = $sparQlClient
->insert([$triple])
->where([$triple]);
if ($shaclClient->validate($statement)) {
dump('statement is valid!');
}
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.