demos-europe / edt-queries
Eases querying entities via property paths.
Requires
- php: ^8.1
- webmozart/assert: ^1.11
Conflicts
- demos-europe/edt-access-definitions: <0.17.1
- demos-europe/edt-dql: <0.17.1
- demos-europe/edt-extra: <0.17.1
- demos-europe/edt-jsonapi: <0.17.1
- demos-europe/edt-paths: <0.17.1
- dev-main
- 0.26.0
- 0.25.0
- 0.24.42
- 0.24.41
- 0.24.40
- 0.24.39
- 0.24.38
- 0.24.37
- 0.24.36
- 0.24.35
- 0.24.34
- 0.24.32
- 0.24.31
- 0.24.30
- 0.24.29
- 0.24.28
- 0.24.27
- 0.24.26
- 0.24.25
- 0.24.24
- 0.24.23
- 0.24.22
- 0.24.21
- 0.24.20
- 0.24.19
- 0.24.18
- 0.24.17
- 0.24.16
- 0.24.15
- 0.24.14
- 0.24.13
- 0.24.12
- 0.24.11
- 0.24.10
- 0.24.9
- 0.24.8
- 0.24.7
- 0.24.6
- 0.24.5
- 0.24.4
- 0.24.3
- 0.24.2
- 0.24.1
- 0.24.0
- 0.23.6
- 0.23.5
- 0.23.4
- 0.23.3
- 0.23.2
- 0.23.1
- 0.23.0
- 0.22.3
- 0.22.2
- 0.22.1
- 0.22.0
- 0.21.4
- 0.21.3
- 0.21.2
- 0.21.1
- 0.21.0
- 0.20.1
- 0.19.0
- 0.18.0
- 0.17.5
- 0.16.1
- 0.16.0
- 0.15.1
- 0.15.0
- 0.14.1
- 0.14.0
- 0.13.2
- 0.13.1
- 0.13.0
- 0.12.23
- 0.12.22
- 0.12.21
- 0.12.20
- 0.12.19
- 0.12.18
- 0.12.17
- 0.12.16
- 0.12.15
- 0.12.14
- 0.12.13
- 0.12.12
- 0.12.11
- 0.12.10
- 0.12.9
- 0.12.8
- 0.12.7
- 0.12.6
- 0.12.5
- 0.12.4
- 0.12.3
- 0.12.2
- 0.12.1
- 0.12.0
- 0.11.2
- 0.11.1
- 0.11.0
- 0.10.0
- 0.9.0
- 0.8.0
- 0.7.1
- 0.7.0
- 0.6.4
- 0.6.3
- 0.6.2
- 0.6.1
This package is auto-updated.
Last update: 2024-11-20 20:52:46 UTC
README
This composer library provides PHP classes to ease the access to a wanted list of entities for common use cases while being agnostic about the source of the entities.
Overview
Property path based approach
Two common requirements when fetching a list of entities are to 1) limit it to entities matching specific conditions and 2) sort it using specific sort methods.
Different query languages handle these requirements in different scopes and manners. EDT-Queries allows the usage of paths when accessing entity properties. This eases the creation of queries, as no manual joins like in SQL need to be defined and allows to focus on the actual intention instead of the implementation details of the data source. On the other hand this adds some requirements regarding the relationship between entities: to fetch books by the name of their authors, the book entity must reference the author entity. Likewise, to fetch authors by the title of their books, the author entity must reference the book entity.
While a bidirectional relationship between books and authors seems reasonable, this drawback becomes more apparent in cases where a unidirectional relationship would have been sufficient normally. Suppose you have a separate entity for E-mail addresses which is referenced by different entities like "organisation", "person" and "block list". Using EDT-Queries to simply get all e-mail addresses that are still in use would require the e-mail address entity to have a relationship to each of these three entities.
Data source agnostic
When creating a query using EDT-Queries the thought model is based on entities with properties and relationships to other entities. If that thought model can be converted to the model of the backing data source, then support for the backing data source is possible to implement.
For example when fetching books by their author’s name we may use a condition equivalent to
book.getAuthor().getName() === $authorName
(the actual syntax to create conditions is shown later).
If book
and author
exist as tables in a relational database, then in principle this expression can be converted
into a SQL query with a JOIN
between book
and author
and a WHERE
condition to compare the actual author’s name to the
given author’s name.
Implementations for specific data sources are mostly separated from this project. See Providers and factories setup for additional information.
Usage examples
In the following examples we get all books by authors born after the year 1800 whose last name start with 'A', primarily sorted by the name of their publishing company and secondarily sorted by their publication date.
As this example focuses on the usage only, we use placeholders for the factories and the provider, instead of actually initializing them. A possible setup is shown in Providers and factories setup.
use EDT\ConditionFactory\ConditionFactoryInterface; use EDT\Querying\Contracts\ObjectProviderInterface; use EDT\Querying\Contracts\SortMethodFactoryInterface; // Placeholder setup: The actual factory and provider instances depend on the type of data source. /** @var ConditionFactoryInterface $conditionFactory */ /** @var SortMethodFactoryInterface $sortMethodFactory */ /** @var ObjectProviderInterface $bookProvider */ // Query definition $nameCondition = $conditionFactory->propertyStartsWithCaseInsensitive('A', 'author', 'lastName'); $birthDateCondition = $conditionFactory->valueGreaterThan(1800, 'author', 'birth', 'year'); $primarySortMethod = $sortMethodFactory->propertyAscending('publisher', 'name'); $secondarySortMethod = $sortMethodFactory->propertyDescending('publishDate'); $conditions = [$nameCondition, $birthDateCondition]; $sortMethods = [$primarySortMethod, $secondarySortMethod]; // Executing the query $booksResultList = $bookProvider->getObjects($conditions, $sortMethods);
As an alternative the above can be written as fluent query as shown below.
use EDT\ConditionFactory\ConditionFactoryInterface; use EDT\Querying\Contracts\ObjectProviderInterface; use EDT\Querying\Contracts\SortMethodFactoryInterface; use EDT\Querying\FluentQueries\FluentQuery; use EDT\Querying\FluentQueries\ConditionDefinition; use EDT\Querying\FluentQueries\SortDefinition; use EDT\Querying\FluentQueries\SliceDefinition; // Query definition $query = create_book_query(); $query->getConditionDefinition() ->propertyStartsWithCaseInsensitive('A', 'author', 'lastName') ->valueGreaterThan(1800, 'author', 'birth', 'year'); $query->getSortDefinition() ->propertyAscending('publisher', 'name') ->propertyDescending('publishDate'); // Executing the query $bookResultList = $query->getEntities(); // Placeholder setup: The actual factory and provider instances depend on the type of data source. function create_book_query(): FluentQuery { /** @var ConditionFactoryInterface $conditionFactory */ /** @var SortMethodFactoryInterface $sortMethodFactory */ /** @var ObjectProviderInterface $bookProvider */ return new FluentQuery( $bookProvider, new ConditionDefinition($conditionFactory), new SortDefinition($sortMethodFactory), new SliceDefinition() ); }
Providers and factories setup
Which implementations to use for the ConditionFactoryInterface
, SortMethodFactoryInterface
and
ObjectProviderInterface
depends on your actual data source.
Please note that factories and object providers for different data sources can only be used in a mix and match manner under specific requirements. To be specific: to access a data source via an object provider not only the object provider must support the data source but the factories from which the conditions and sort methods were created too.
To keep this projects dependencies small, EDT-Queries only supports a single data source, namely an already loaded PHP array
providing the entity objects.
An implementation for the Doctrine ORM has already been written and is available as EDT-DQL. It provides factories for conditions and sort methods that are automatically converted into DQL, which in turn already supports a multitude of different data sources.
PHP array data source
Even though their real-world use cases are limited, the array
-based approach can still be used on small data sets,
that can be loaded into an array. To complete the example above you
could instantiate a PhpConditionFactory
and a PhpSortMethodFactory
to create the corresponding
instances. When creating the PrefilledObjectProvider
you need to inject the PropertyAccessorInterface
dependency, which determines how values are read from the entities when applying conditions and sort methods.
This allows to adjust the behavior of the provider without the need to completely extend it and override its methods.
// The books to be filtered, preloaded into an array /** @var list<object> $books */ // Setting up the factories and provider $conditionFactory = new \EDT\Querying\ConditionFactories\PhpConditionFactory(); $sortMethodFactory = new EDT\Querying\SortMethodFactories\PhpSortMethodFactory(); $bookProvider = new \EDT\Querying\ObjectProviders\PrefilledObjectProvider( new \EDT\Querying\PropertyAccessors\ReflectionPropertyAccessor(), $books ); // Filtering books by the name of the auther's children and sorting them by their title $bookProvider->getObjects( [$conditionFactory->propertyHasValue('Christopher Tolkien', 'author', 'children' 'name')], [$sortMethodFactory->propertyAscending('title')] );
Credits and acknowledgements
Conception and implementation by Christian Dressler with many thanks to eFrane.