gbprod / elastica-specification-bundle
This bundle integrates elastica-specification with Symfony
Installs: 11 951
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Type:bundle
Requires
- php: ^7.0
- gbprod/elastica-specification: ^2.0
- symfony/expression-language: ^2.7|^3.0|^4.0
- symfony/framework-bundle: ^2.7|^3.0|^4.0
- symfony/yaml: ^2.7|^3.0|^4.0
Requires (Dev)
- phpunit/phpunit: ^6.0
This package is auto-updated.
Last update: 2022-03-07 11:53:05 UTC
README
This bundle integrates elastica-specification with Symfony.
Installation
Download bundle using composer :
composer require gbprod/elastica-specification-bundle
Declare in your app/AppKernel.php
file:
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new GBProd\ElasticaSpecificationBundle\ElasticaSpecificationBundle(), // ... ); }
Create your specification and your query factory
Take a look to Specification and Elastica Specification libraries for more informations.
Create a specification
<?php namespace GBProd\Acme\CoreDomain\Specification\Product; use GBProd\Specification\Specification; class IsAvailable implements Specification { public function isSatisfiedBy($candidate): bool { return $candidate->isSellable() && $candidate->expirationDate() > new \DateTime('now') ; } }
Create a query factory
<?php namespace GBProd\Acme\Infrastructure\Elastica\QueryFactory\Product; use GBProd\ElasticaSpecification\QueryFactory\Factory; use GBProd\Specification\Specification; use Elastica\QueryBuilder; class IsAvailableFactory implements Factory { public function build(Specification $spec, QueryBuilder $qb) { return $qb->query()->bool() ->addMust() $qb->query()->term(['available' => "0"]), ) ; } }
Configuration
Declare your Factory
# src/GBProd/Acme/AcmeBundle/Resource/config/service.yml services: acme.elastica.query_factory.is_available: class: GBProd\Acme\Infrastructure\Elastica\QueryFactory\Product\IsAvailableFactory tags: - { name: elastica.query_factory, specification: GBProd\Acme\CoreDomain\Specification\Product\IsAvailable }
Inject handler in your repository class
# src/GBProd/Acme/AcmeBundle/Resource/config/service.yml services: acme.product_repository: class: GBProd\Acme\Infrastructure\Product\ElasticaProductRepository arguments: - "@elastica.client" - "@gbprod.elastica_specification_handler"
<?php namespace GBProd\Acme\Infrastructure\Product; use Elastica\Client; use Elastica\QueryBuilder; use GBProd\ElasticaSpecification\Handler; use GBProd\Specification\Specification; class ElasticaProductRepository implements ProductRepository { private $client; private $handler; public function __construct(Client $em, Handler $handler) { $this->client = $client; $this->handler = $handler; } public function findSatisfying(Specification $specification) { $type = $this ->getIndex('catalog') ->getType('product') ; $query = $this->handler->handle($specification, new QueryBuilder()); return $type->search($query); } }
Usage
<?php $products = $productRepository->findSatisfying( new AndX( new IsAvailable(), new IsLowStock() ) );
Elastica configuration
You can specify the Elastica DSL version using dsl_version
configuration node (default: Latest
).
To use a different version, just set it to the version classname.
# app/config/config.yml elastica_specification_bundle: dsl_version: Version120