2017 © Pedro Peláez
 

bundle doctrine-specification-bundle

This bundle integrates Doctrine specification with Symfony

image

gbprod/doctrine-specification-bundle

This bundle integrates Doctrine specification with Symfony

  • Monday, February 26, 2018
  • by gbprod
  • Repository
  • 2 Watchers
  • 5 Stars
  • 162 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 12 Versions
  • 0 % Grown

The README.md

Doctrine specification bundle

This bundle integrates doctrine-specification with Symfony., (*1)

Build Status codecov Scrutinizer Code Quality Dependency Status, (*2)

Latest Stable Version Total Downloads Latest Unstable Version License, (*3)

Installation

Download bundle using composer :, (*4)

composer require gbprod/doctrine-specification-bundle

Declare in your app/AppKernel.php file:, (*5)

<?php
// app/AppKernel.php

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

Create your specification and your expression builder

Take a look to Specification and Doctrine Specification libraries for more informations., (*6)

Create a specification

<?php

namespace GBProd\Acme\CoreDomain\Specification\Product;

use GBProd\Specification\Specification;

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

Create a query factory

<?php

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

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

class IsAvailableFactory implements Factory
{
    public function create(Specification $spec, QueryBuilder $qb)
    {
        return $qb->expr()
            ->andx(
                $qb->expr()->eq('sellable', "1"),
                $qb->expr()->gt('expirationDate', (new \DateTime('now'))->format('c'))
            )
        ;
    }
}

Configuration

Declare your factory

# src/GBProd/Acme/AcmeBundle/Resource/config/service.yml

services:
    acme.doctrine.query_factory.is_available:
        class: GBProd\Acme\Infrastructure\Doctrine\QueryFactory\Product\IsAvailableFactory
        tags:
            - { name: doctrine.query_factory, specification: GBProd\Acme\CoreDomain\Specification\Product\IsAvailable }

Inject handler in your repository class

# src/GBProd/Acme/AcmeBundle/Resource/config/service.yml

services:
    acme.product_repository:
        class: GBProd\Acme\Infrastructure\Product\DoctrineProductRepository
        arguments:
            - "@doctrine.orm.entity_manager"
            - "@gbprod.doctrine_specification_handler"
<?php

namespace GBProd\Acme\Infrastructure\Product;

use Doctrine\Common\Persistence\ObjectManager;
use GBProd\DoctrineSpecification\Handler;
use GBProd\Specification\Specification;

class DoctrineProductRepository implements ProductRepository
{
    private $em;

    private $handler;

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

    public function findSatisfying(Specification $specification)
    {
        $qb = $this
            ->em
            ->getRepository('GBProd\Acme\CoreDomain\Product\Product')
            ->createQueryBuilder('p')
        ;

        return $this->handler->handle($specification, $qb);
    }
}

Usage

<?php

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

The Versions

30/06 2017
28/06 2017

v1.0.x-dev

1.0.9999999.9999999-dev

This bundle integrates Doctrine specification with Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar gbprod

12/03 2016

v0.1.1

0.1.1.0

This bundle integrates Doctrine specification with Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar gbprod

09/03 2016

v0.1.0

0.1.0.0

This bundle integrates Doctrine specification with Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar gbprod

07/03 2016

v0.0.2-alpha

0.0.2.0-alpha

This bundle integrates Doctrine specification with Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar gbprod

07/03 2016

v0.0.1-alpha

0.0.1.0-alpha

This bundle integrates Doctrine specification with Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar gbprod