## Getting Started
### Minimal Setup
1. **Installation**: Add the package via Composer:
```bash
composer require effectiveactivism/sparql-client
config/packages/sparql_client.yaml:
sparql_client:
query_endpoint: "http://your-sparql-endpoint/sparql"
update_endpoint: "http://your-sparql-endpoint/sparql"
use EffectiveActivism\SparQlClient\Client\SparQlClientInterface;
use EffectiveActivism\SparQlClient\Syntax\Term\Variable\Variable;
use EffectiveActivism\SparQlClient\Syntax\Term\Iri\PrefixedIri;
use EffectiveActivism\SparQlClient\Syntax\Pattern\Triple\Triple;
public function index(SparQlClientInterface $client) {
$client->setExtraNamespaces(['schema' => 'http://schema.org/']);
$subject = new Variable('subject');
$predicate = new PrefixedIri('schema', 'name');
$triple = new Triple($subject, $predicate, new Variable('object'));
$result = $client->select([$subject])->where([$triple])->execute();
return $result->getRows();
}
CRUD Operations:
insert() with a Triple or Quad.select(), ask(), or describe() with where() clauses.replace() (DELETE+INSERT) or delete() with where().delete() with where() constraints.Complex Queries:
Count, Sum, etc., in groupBy()/having():
$select->groupBy([$subject])
->having(new GreaterThan(new Count($subject), new TypedLiteral(5)));
select() statements or use Subquery in where():
$subquery = $client->select([$var])->where([$triple])->subquery();
$select->where([new Filter(new In($var, $subquery))]);
Graph Management:
load() or clear() for graph-level actions:
$client->load(new Iri('urn:dataset'), new Iri('urn:source'))
->execute();
Validation:
shacl_endpoint:
$validator = $client->getShaclValidator();
$result = $validator->validate($data, $shaclShape);
SparQlClientInterface in services.yaml for reusable queries:
services:
App\Service\SparqlService:
arguments:
$client: '@effectiveactivism.sparql_client.client'
class ArticleQueryBuilder {
public function findByHeadline(SparQlClientInterface $client, string $headline) {
$subject = new Variable('subject');
$predicate = new PrefixedIri('schema', 'headline');
$object = new PlainLiteral($headline);
return $client->select([$subject])
->where([new Triple($subject, $predicate, $object)]);
}
}
try {
$result = $client->execute($statement);
} catch (SparqlClientException $e) {
$this->addFlash('error', $e->getMessage());
}
Endpoint Mismatch:
query_endpoint and update_endpoint for SPARQL 1.1./query and /update paths).Namespace Scope:
$client->setExtraNamespaces(['schema' => 'http://schema.org/']);
setDefaultNamespace() for global defaults.Variable Binding:
SELECT not matching WHERE clauses.$var = new Variable('x'); // Reuse the same instance/object
Update Operations:
execute() on update statements returning UpdateResultInterface (not QueryResultInterface).getStatusCode() for HTTP errors:
$result = $client->execute($insertStatement);
if ($result->getStatusCode() !== 200) {
throw new RuntimeException('Update failed');
}
Performance:
WHERE clauses.limit()/offset() for pagination.FILTER clauses:
$select->where([$triple])
->filter(new GreaterThan($var, new TypedLiteral(100)));
Raw SPARQL:
$rawQuery = $statement->getQueryString();
dump($rawQuery);
Logging:
sparql_client:
debug: true
SHACL Validation:
getResults() for detailed errors:
$results = $validator->validate($data, $shape);
foreach ($results->getConforms() as $conforms) {
if (!$conforms) {
dump($results->getMessages());
}
}
Custom Functions:
ExtensionFunction implementations:
class MyCustomFunction extends ExtensionFunction {
public function getName(): string { return 'my:customFunc'; }
public function evaluate(array $args, array $bindings): TermInterface {
// Implement logic
return new PlainLiteral('result');
}
}
services:
App\Function\MyCustomFunction:
tags: ['effectiveactivism.sparql_client.extension_function']
Query Modifiers:
StatementInterface to add reusable modifiers:
class PaginatedSelectDecorator implements StatementInterface {
private StatementInterface $statement;
public function __construct(StatementInterface $statement) {
$this->statement = $statement;
}
public function limit(int $limit): self {
$this->statement->limit($limit);
return $this;
}
// Implement other StatementInterface methods
}
Result Transformers:
QueryResultInterface to domain objects:
class SparqlToArticleTransformer {
public function transform(QueryResultInterface $result): array {
return array_map(function ($row) {
return new Article(
$row['subject']->getValue(),
$row['headline']->getValue()
);
}, $result->getRows());
}
}
Default Namespaces:
rdf, xsd) accidentally.setExtraNamespaces() for custom ones.SHACL Endpoint:
sparql_client:
shacl_endpoint: "http://validator/shacl"
shacl_headers:
Authorization: "Bearer token123"
Timeouts:
config/packages/http_client.yaml:
framework:
http_client:
timeout: 60
max_duration: 120
---
How can I help you explore Laravel packages today?