Skip to content
This repository has been archived by the owner on Jul 19, 2021. It is now read-only.

This library allows to write Elastica queries using the Specification pattern

License

Notifications You must be signed in to change notification settings

gbprod/elastica-specification

Repository files navigation

Elastica specification

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

About

This library allows to write Elastica queries using the Specification pattern

Resources

License

Stars

Watchers

Forks

Packages

No packages published