PHP code example of gbprod / algolia-specification-bundle

1. Go to this page and download the library: Download gbprod/algolia-specification-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gbprod / algolia-specification-bundle example snippets



// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new GBProd\AlgoliaSpecificationBundle\AlgoliaSpecificationBundle(),
        // ...
    );
}



namespace GBProd\Acme\CoreDomain\Specification\Product;

use GBProd\Specification\Specification;

class IsAvailable implements Specification
{
    public function isSatisfiedBy($candidate)
    {
        return $candidate->isSellable()
            && $candidate->expirationDate() > new \DateTime('now')
        ;
    }
}



namespace GBProd\Acme\Infrastructure\Algolia\QueryFactory\Product;

use GBProd\AlgoliaSpecification\QueryFactory\Factory;
use GBProd\Specification\Specification;
use Algolia\QueryBuilder;

class IsAvailableFactory implements Factory
{
    public function create(Specification $spec)
    {
        return 'available=1';
    }
}



namespace GBProd\Acme\Infrastructure\Product;

use Algolia\Client;
use GBProd\AlgoliaSpecification\Handler;
use GBProd\Specification\Specification;

class AlgoliaProductRepository implements ProductRepository
{
    private $client;

    private $handler;

    public function __construct(Client $em, Handler $handler)
    {
        $this->client  = $client;
        $this->handler = $handler;
    }

    public function findSatisfying(Specification $specification)
    {
        $index = $this->client->initIndex('products');
        
        $query = $this->handler->handle($specification);
        
        return $type->search(['filters' => $query]);
    }
}



$products = $productRepository->findSatisfying(
    new AndX(
        new IsAvailable(),
        new IsLowStock()
    )
);