gbprod / elastica-specification
This library allows you to create Elastica queries using the Specification pattern
Installs: 14 850
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^7.0
- gbprod/specification: ^2.0
- ruflin/elastica: ^5.0|^6.0
Requires (Dev)
- phpunit/phpunit: ^6.0
This package is auto-updated.
Last update: 2021-08-07 10:30:25 UTC
README
This library allows you to create Elastica queries using the specification pattern.
Usage
You can write specifications using gbprod/specification library.
Creates a elastica specification filter
namespace GBProd\Acme\Elastica\SpecificationFactory; use GBProd\ElasticaSpecification\QueryFactory\Factory; use GBProd\Specification\Specification; use Elastica\QueryBuilder; class IsAvailableFactory implements Factory { public function create(Specification $spec, QueryBuilder $qb) { return $qb->query()->bool() ->addMust( $qb->query()->term(['available' => "0"]), ) ; } }
Configure
$registry = new GBProd\ElasticaSpecification\Registry(); $qb = new \Elastica\QueryBuilder(); $handler = new GBProd\ElasticaSpecification\Handler($registry, $qb); $handler->registerFactory(IsAvailable::class, new IsAvailableFactory()); $handler->registerFactory(StockGreaterThan::class, new StockGreaterThanFactory());
Use it
$available = new IsAvailable(); $hightStock = new StockGreaterThan(4); $availableWithLowStock = $available ->andX( $hightStock->not() ) ; $type = $this->elasticaClient ->getIndex('my_index') ->getType('my_type') ; $query = $handler->handle($availableWithLowStock) $results = $type->search($query);
Requirements
- PHP 7.0+
Installation
Using composer
composer require gbprod/elastica-specification