PHP code example of gbprod / doctrine-specification

1. Go to this page and download the library: Download gbprod/doctrine-specification 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 / doctrine-specification example snippets


namespace GBProd\Acme\Doctrine\SpecificationFactory;

use GBProd\DoctrineSpecification\QueryFactory\Factory;
use GBProd\Specification\Specification;
use Doctrine\ORM\QueryBuilder;

class IsAvailableFactory implements Factory
{
    public function create(Specification $spec, QueryFactory $qb)
    {
        return $qb->expr()
            ->andx(
                $qb->expr()->eq('available', "0"),
                $qb->expr()->gt('limitDate', "2016-03-05 00:00:00"),
            )
        ;
    }
}

$registry = new GBProd\DoctrineSpecification\Registry();

$handler = new GBProd\DoctrineSpecification\Handler(
    $registry,
    $this->em->createQueryBuilder()
);

$handler->registerFactory(
    IsAvailable::class,
    new IsAvailableFactory()
);

$handler->registerFactory(
    StockGreaterThan::class,
    new StockGreaterThanFactory()
);

$availableWithLowStock = (new IsAvailable())
    ->andX((new StockGreaterThan(4))->not())
;

$qb = $this->em
    ->getRepository('GBProd\Acme\CoreDomain\Product\Product')
    ->createQueryBuilder('p')
;

$qb->where(
    $handler->handle($availableWithLowStock)
);

return $qb->getQuery()->getResult();