vendor/happyr/doctrine-specification/src/Specification/BaseSpecification.php line 63

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * This file is part of the Happyr Doctrine Specification package.
  5.  *
  6.  * (c) Tobias Nyholm <tobias@happyr.com>
  7.  *     Kacper Gunia <kacper@gunia.me>
  8.  *     Peter Gribanov <info@peter-gribanov.ru>
  9.  *
  10.  * For the full copyright and license information, please view the LICENSE
  11.  * file that was distributed with this source code.
  12.  */
  13. namespace Happyr\DoctrineSpecification\Specification;
  14. use Doctrine\ORM\QueryBuilder;
  15. use Happyr\DoctrineSpecification\Filter\Filter;
  16. use Happyr\DoctrineSpecification\Filter\Satisfiable;
  17. use Happyr\DoctrineSpecification\Query\QueryModifier;
  18. /**
  19.  * Extend this abstract class if you want to build a new spec with your domain logic.
  20.  */
  21. abstract class BaseSpecification implements Specification
  22. {
  23.     /**
  24.      * @var string|null
  25.      */
  26.     private $context;
  27.     /**
  28.      * @param string|null $context
  29.      */
  30.     public function __construct(?string $context null)
  31.     {
  32.         $this->context $context;
  33.     }
  34.     /**
  35.      * @param QueryBuilder $qb
  36.      * @param string       $context
  37.      *
  38.      * @return string
  39.      */
  40.     public function getFilter(QueryBuilder $qbstring $context): string
  41.     {
  42.         $spec $this->getSpec();
  43.         if ($spec instanceof Filter) {
  44.             return $spec->getFilter($qb$this->getContext($context));
  45.         }
  46.         return '';
  47.     }
  48.     /**
  49.      * @param QueryBuilder $qb
  50.      * @param string       $context
  51.      */
  52.     public function modify(QueryBuilder $qbstring $context): void
  53.     {
  54.         $spec $this->getSpec();
  55.         if ($spec instanceof QueryModifier) {
  56.             $spec->modify($qb$this->getContext($context));
  57.         }
  58.     }
  59.     /**
  60.      * {@inheritdoc}
  61.      */
  62.     public function filterCollection(iterable $collection, ?string $context null): iterable
  63.     {
  64.         $spec $this->getSpec();
  65.         if ($spec instanceof Satisfiable) {
  66.             return $spec->filterCollection($collection$this->resolveContext($context));
  67.         }
  68.         return $collection;
  69.     }
  70.     /**
  71.      * {@inheritdoc}
  72.      */
  73.     public function isSatisfiedBy($candidate, ?string $context null): bool
  74.     {
  75.         $spec $this->getSpec();
  76.         if ($spec instanceof Satisfiable) {
  77.             return $spec->isSatisfiedBy($candidate$this->resolveContext($context));
  78.         }
  79.         return true;
  80.     }
  81.     /**
  82.      * Return all the specifications.
  83.      *
  84.      * @return Filter|QueryModifier
  85.      */
  86.     abstract protected function getSpec();
  87.     /**
  88.      * @param string $context
  89.      *
  90.      * @return string
  91.      */
  92.     private function getContext(string $context): string
  93.     {
  94.         if (null !== $this->context) {
  95.             return sprintf('%s.%s'$context$this->context);
  96.         }
  97.         return $context;
  98.     }
  99.     /**
  100.      * @param string $context
  101.      *
  102.      * @return string
  103.      */
  104.     protected function getNestedContext(string $context): string
  105.     {
  106.         if (null !== $this->context) {
  107.             return sprintf('%s.%s'$this->context$context);
  108.         }
  109.         return $context;
  110.     }
  111.     /**
  112.      * @param string|null $context
  113.      *
  114.      * @return string|null
  115.      */
  116.     private function resolveContext(?string $context): ?string
  117.     {
  118.         if (null !== $this->context && null !== $context) {
  119.             return sprintf('%s.%s'$context$this->context);
  120.         }
  121.         if (null !== $this->context) {
  122.             return $this->context;
  123.         }
  124.         return $context;
  125.     }
  126. }