PHPackages                             benestar/asparagus - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. benestar/asparagus

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

benestar/asparagus
==================

SPARQL abstraction layer for PHP

0.4.2(10y ago)1554.9k↓24.5%3[7 issues](https://github.com/Benestar/asparagus/issues)[1 PRs](https://github.com/Benestar/asparagus/pulls)5GNU GPL v2+PHPPHP &gt;=5.3.0

Since Jun 17Pushed 8y ago2 watchersCompare

[ Source](https://github.com/Benestar/asparagus)[ Packagist](https://packagist.org/packages/benestar/asparagus)[ Docs](https://github.com/benestar/Asparagus)[ RSS](/packages/benestar-asparagus/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)DependenciesVersions (9)Used By (5)

Asparagus
=========

[](#asparagus)

\[[![Build Status](https://camo.githubusercontent.com/7a196871404de8e36316782141785025084b52124c66d20b84da408ceacbe732/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f42656e65737461722f6173706172616775732e706e673f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/7a196871404de8e36316782141785025084b52124c66d20b84da408ceacbe732/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f42656e65737461722f6173706172616775732e706e673f6272616e63683d6d6173746572)\] () \[[![Scrutinizer Code Quality](https://camo.githubusercontent.com/619144378b83f8a3cf1e8660c0e260721b55b5b4fff820e4831e58209345afea/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f42656e65737461722f6173706172616775732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://camo.githubusercontent.com/619144378b83f8a3cf1e8660c0e260721b55b5b4fff820e4831e58209345afea/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f42656e65737461722f6173706172616775732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)\] () \[[![Code Coverage](https://camo.githubusercontent.com/c3e2c9093582391ba4235cc3d38a012ef287590adc63705b1036f4c0944e21ce/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f42656e65737461722f6173706172616775732f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://camo.githubusercontent.com/c3e2c9093582391ba4235cc3d38a012ef287590adc63705b1036f4c0944e21ce/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f42656e65737461722f6173706172616775732f6261646765732f636f7665726167652e706e673f623d6d6173746572)\] () \[[![Download count](https://camo.githubusercontent.com/d7f6cec76ffbeec27e8c6fcf454959c8416153b71f7285cbc217bd157455c683/68747470733a2f2f706f7365722e707567782e6f72672f62656e65737461722f6173706172616775732f642f746f74616c2e706e67)](https://camo.githubusercontent.com/d7f6cec76ffbeec27e8c6fcf454959c8416153b71f7285cbc217bd157455c683/68747470733a2f2f706f7365722e707567782e6f72672f62656e65737461722f6173706172616775732f642f746f74616c2e706e67)\] () \[[![License](https://camo.githubusercontent.com/e2db3ab89cef60161db35189df2b479f750134d07503a0f1494b2d1a9f99ce0a/68747470733a2f2f706f7365722e707567782e6f72672f62656e65737461722f6173706172616775732f6c6963656e73652e737667)](https://camo.githubusercontent.com/e2db3ab89cef60161db35189df2b479f750134d07503a0f1494b2d1a9f99ce0a/68747470733a2f2f706f7365722e707567782e6f72672f62656e65737461722f6173706172616775732f6c6963656e73652e737667)\] ()

\[[![Latest Stable Version](https://camo.githubusercontent.com/ca14e9bb0d6f5d7fb74c51f7e1121ad68187ed9d7cdeb3e9648b4a5c8a8acafe/68747470733a2f2f706f7365722e707567782e6f72672f62656e65737461722f6173706172616775732f76657273696f6e2e706e67)](https://camo.githubusercontent.com/ca14e9bb0d6f5d7fb74c51f7e1121ad68187ed9d7cdeb3e9648b4a5c8a8acafe/68747470733a2f2f706f7365722e707567782e6f72672f62656e65737461722f6173706172616775732f76657273696f6e2e706e67)\] () \[[![Latest Unstable Version](https://camo.githubusercontent.com/3dd9f8085ea10c2f4e99ddb50a42cba3d39620c1162e6605befec7926712072a/68747470733a2f2f706f7365722e707567782e6f72672f62656e65737461722f6173706172616775732f762f756e737461626c652e737667)](https://camo.githubusercontent.com/3dd9f8085ea10c2f4e99ddb50a42cba3d39620c1162e6605befec7926712072a/68747470733a2f2f706f7365722e707567782e6f72672f62656e65737461722f6173706172616775732f762f756e737461626c652e737667)\] (//packagist.org/packages/benestar/asparagus)

**Asparagus** is a SPARQL abstraction layer for PHP. It's design is inspired by the DBAL query builder.

Installation
------------

[](#installation)

You can use [Composer](http://getcomposer.org/) to download and install this package as well as its dependencies. Alternatively you can simply clone the git repository and take care of loading yourself.

### Composer

[](#composer)

To add this package as a local, per-project dependency to your project, simply add a dependency on `benestar/asparagus` to your project's `composer.json` file. Here is a minimal example of a `composer.json` file that just defines a dependency on Asparagus 0.3:

```
{
    "require": {
        "benestar/asparagus": "~0.4"
    }
}
```

### Manual

[](#manual)

Get the Asparagus code, either via git, or some other means. Also get all dependencies. You can find a list of the dependencies in the "require" section of the composer.json file. The "autoload" section of this file specifies how to load the resources provide by this library.

Usage
-----

[](#usage)

Most of the methods in `QueryBuilder` return the builder instance so you can build a query by calling the methods one by one. Currently, the `QueryBuilder` supports to manage prefixes, select variables, add basic triple conditions and group them by subject and predicate, and full support for all query modifiers SPARQL provides.

The `QueryBuilder` instance can be passed to a `QueryExecuter` or the SPARQL can be obtained as is using `getSPARQL` or formatted using `format`.

### Basic functionality

[](#basic-functionality)

In the following example, a simple SPARQL query is generated asking for all persons who have a name and an email address stored in the database.

```
use Asparagus\QueryBuilder;

$prefixes = array(
	'test' => 'http://www.example.com/test#'
);

$queryBuilder = new QueryBuilder( $prefixes );
$queryBuilder->select( '?name', '?email' )
	->where( '?person', 'test:name', '?name' )
	->also( 'test:email', '?email' )
	->limit( 10 );

echo $queryBuilder->format();
```

The generated query looks like:

```
PREFIX test:

SELECT ?name ?email WHERE {
	?person test:name ?name ;
		test:email ?email .
}
LIMIT 10
```

### Optionals and filters

[](#optionals-and-filters)

The following snippet creates a more complex query using optional values and filters. Only persons who do not have their email address deposited in the database are shown.

```
use Asparagus\QueryBuilder;

$prefixes = array(
	'test' => 'http://www.example.com/test#'
);

$queryBuilder = new QueryBuilder( $prefixes );
$queryBuilder->select( '?name' )
	->where( '?person', 'test:name', '?name' )
	->optional( '?person', 'test:email', '?email' )
	->filter( '!BOUND (?email)' );

echo $queryBuilder->format();
```

The generated query looks like:

```
PREFIX test:

SELECT ?name WHERE {
	?person test:name ?name .
	OPTIONAL {
		?person test:email ?email .
	}
	FILTER (!BOUND (?email))
}
```

### Unions

[](#unions)

More complex queries can be built by using subgraphs or subqueries. To create a new subgrapho or subquery, you can call `QueryBuilder::newSubgraph` or `QueryBuilder::newSubquery`. `GraphBuilder`supports all graph functions also supported by `QueryBuilder` and it will return itself as well. If alternative conditions should be matched, you can use `QueryBuilder::union` to specify several graph patterns which are all allowed.

The next query returns titles and authors of books recorded using Dublin Core properties from version 1.0 or version 1.1.

```
use Asparagus\QueryBuilder;

$prefixes = array(
	'dc10' => 'http://purl.org/dc/elements/1.0/',
	'dc11' => 'http://purl.org/dc/elements/1.1/'
);

$queryBuilder = new QueryBuilder( $prefixes );

$queryBuilder->select( '?title', '?author' )
	->union(
		$queryBuilder->newSubgraph()
			->where( '?book', 'dc10:title', '?title' )
			->also( 'dc10:creator', '?author' ),
		$queryBuilder->newSubgraph()
			->where( '?book', 'dc11:title', '?title' )
			->also( 'dc11:creator', '?author' )
	);

echo $queryBuilder->format();
```

The generated query looks like:

```
PREFIX dc10:
PREFIX dc11:

SELECT ?title ?author WHERE {
	{
		?book dc10:title ?title ;
			dc10:creator ?author .
	} UNION {
		?book dc11:title ?title ;
			dc11:creator ?author .
	}
}
```

Tests
-----

[](#tests)

This library comes with a set up PHPUnit tests that cover all non-trivial code. You can run these tests using the PHPUnit configuration file found in the root directory. The tests can also be run via TravisCI, as a TravisCI configuration file is also provided in the root directory.

Release notes
-------------

[](#release-notes)

### 0.4.2 (2016-01-21)

[](#042-2016-01-21)

- Fixed incompatible changes in `QueryExecuter`

### 0.4.1 (2016-01-21)

[](#041-2016-01-21)

- Fixed return value of `QueryExecuter::execute`

### 0.4 (2015-10-01)

[](#04-2015-10-01)

- Added `QueryBuilder::describe`
- `QueryBuilder::select`, `QueryBuilder::selectDistinct` and `QueryBuilder::selectReduced` now throw `RuntimeException`

### 0.3.1 (2015-09-06)

[](#031-2015-09-06)

- Added support for native values in selects

### 0.3 (2015-06-22)

[](#03-2015-06-22)

- Renamed previously package-private `QueryConditionBuilder` to `GraphBuilder`
- Removed `QueryBuilder::hasSubquery`
- Added `QueryBuilder::getSelects`
- Added `QueryBuilder::selectDistinct` and `QueryBuilder::selectReduced`
- Added `QueryBuilder::optional`
- Added `QueryBuilder::filter`, `QueryBuilder::filterExists` and `QueryBuilder::filterNotExists`
- Added `QueryBuilder::union`
- Added `QueryBuilder::newSubgraph`
- `QueryBuilder::select` and `QueryBuilder::groupBy` now require functions to be wrapped by brackets
- `QueryBuilder::groupBy` now accepts multiple arguments
- `QueryBuilder::where` and `QueryBuilder::also` now support property paths in predicates

### 0.2.1 (2015-06-19)

[](#021-2015-06-19)

- Fixed README.md to use prefixed variables in `QueryBuilder::select`

### 0.2 (2015-06-18)

[](#02-2015-06-18)

- Renamed `QueryBuilder::plus` to `QueryBuilder::also`
- `QueryBuilder::select`, `QueryBuilder::groupBy` and `QueryBuilder::orderBy` now expect prefixed variables instead of just the variable name
- Removed `QueryBuilder::prefix` as prefixes should be defined in the constructor
- Added more validation for variables and prefixes. `QueryBuilder::getSPARQL` will throw a `RangeException` if the validation fails.
    - Selected variables that don't occur in the conditions are detected
    - Prefixes which haven't been declared are detected
    - Variable names and IRIs now have to mach the correct format
    - A list of supported functions has been added and a check to find bracket mismatches

### 0.1 (2015-06-17)

[](#01-2015-06-17)

Initial release with these features:

- A `QueryBuilder` with basic functionality to generate SPARQL queries
- A `QueryFormatter` to make SPARQL queries human-readable
- A `QueryExecuter` which sends queries to a SPARQL endpoint and parses the result

License
-------

[](#license)

Asparagus is licensed under the GNU General Public License Version 2. A copy of the license can be found in the [LICENSE file](LICENSE).

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance11

Infrequent updates — may be unmaintained

Popularity38

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 99% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~31 days

Recently: every ~53 days

Total

8

Last Release

3770d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4883d4f603a92f4570c866a8f475ae87d45b4250c6ebbac61081017fd5bacbde?d=identicon)[benestar](/maintainers/benestar)

---

Top Contributors

[![Benestar](https://avatars.githubusercontent.com/u/2998254?v=4)](https://github.com/Benestar "Benestar (104 commits)")[![cdekok](https://avatars.githubusercontent.com/u/190633?v=4)](https://github.com/cdekok "cdekok (1 commits)")

---

Tags

sparqlAbstraction Layerasparagus

### Embed Badge

![Health badge](/badges/benestar-asparagus/health.svg)

```
[![Health](https://phpackages.com/badges/benestar-asparagus/health.svg)](https://phpackages.com/packages/benestar-asparagus)
```

###  Alternatives

[easyrdf/easyrdf

EasyRdf is a PHP library designed to make it easy to consume and produce RDF.

62231.5M42](/packages/easyrdf-easyrdf)[semsol/arc2

ARC2 is a PHP library for working with RDF. It also provides a MySQL-based triplestore with SPARQL support.

334250.0k15](/packages/semsol-arc2)[sweetrdf/easyrdf

EasyRdf is a PHP library designed to make it easy to consume and produce RDF.

261.3M11](/packages/sweetrdf-easyrdf)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
