This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

Quering object graphs in PHP

dev-master 2019-11-03 10:07 UTC

This package is auto-updated.

Last update: 2021-10-01 00:06:00 UTC


Build Status Test Coverage Maintainability

This library allows you to query your object graph in a consistent way. You can use it to support object mapping and to generate data representation based on the requirements of external systems.


Query and the QueryResolver are the two key components of the system. A resolver needs one or more queries and resolves these queries by processing them on a given object graph.


use Cubicl\ObjectQuery\Query\Query;
use Cubicl\ObjectQuery\QueryResolver;
use Cubicl\ObjectQuery\Definition\Path;

$resolver = new QueryResolver(
    new Query('shipName', (new Path())->get('name'))

// ['shipName' => 'Millenium Falcon']

A query consists of a name which ends up being the key in the result and a definition.

There are three main definitions in the system you can use. Path, Value and Composition.


The Value definition is a plain container which will return the given value.


use Cubicl\ObjectQuery\Query\Query;
use Cubicl\ObjectQuery\QueryResolver;
use Cubicl\ObjectQuery\Definition\Value;

$resolver = new QueryResolver(
    new Query('two', new Value(2))

// ['two' => 2]


The Composition definition is a more flexible alternative to Value. It gives access to the current source.


use Cubicl\ObjectQuery\Query\Query;
use Cubicl\ObjectQuery\QueryResolver;
use Cubicl\ObjectQuery\Definition\Composition;
use Cubicl\ObjectQuery\Source\ObjectSource;

$composition = new Composition(function(ObjectSource $source) {
    return $source->get('id');
$resolver = new QueryResolver(
    new Query('someKey', $composition)

// ['someKey' => 3000]


Path is the most complex definition. You can deep walk into the graph, filter collections and transform leaves. Have a look into the tests to get an impression of the possibilities.


use ObjectQuery\Query\Query;
use ObjectQuery\QueryResolver;
use ObjectQuery\Definition\Path;

$path = (new Path())->get('appearsIn')
    ->filter(new EpisodeFilter(Episode::EMPIRE))
$resolver = new QueryResolver(new Query('episodes', $path));

// ['episode' => [1, 2]]