gbprod/elastica-specification

This package is abandoned and no longer maintained. No replacement package was suggested.

This library allows you to create Elastica queries using the Specification pattern

v2.1.0 2018-02-08 06:45 UTC

This package is auto-updated.

Last update: 2021-08-07 10:30:25 UTC


README

Build Status codecov Scrutinizer Code Quality Dependency Status

Latest Stable Version Total Downloads Latest Unstable Version License

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