3.8. Specification
You can also find this code on
SpecificationInterface.php
- <?php
- namespace DesignPatterns\Behavioral\Specification;
- interface SpecificationInterface
- {
- public function isSatisfiedBy(Item $item): bool;
- }
OrSpecification.php
- <?php
- namespace DesignPatterns\Behavioral\Specification;
- class PriceSpecification implements SpecificationInterface
- {
- /**
- * @var float|null
- */
- private $maxPrice;
- /**
- * @var float|null
- */
- private $minPrice;
- /**
- * @param float $minPrice
- * @param float $maxPrice
- */
- public function __construct($minPrice, $maxPrice)
- {
- $this->minPrice = $minPrice;
- $this->maxPrice = $maxPrice;
- }
- public function isSatisfiedBy(Item $item): bool
- {
- if ($this->maxPrice !== null && $item->getPrice() > $this->maxPrice) {
- return false;
- }
- if ($this->minPrice !== null && $item->getPrice() < $this->minPrice) {
- return false;
- }
- return true;
- }
- }
AndSpecification.php
NotSpecification.php
- <?php
- namespace DesignPatterns\Behavioral\Specification;
- class NotSpecification implements SpecificationInterface
- {
- /**
- * @var SpecificationInterface
- */
- private $specification;
- public function __construct(SpecificationInterface $specification)
- {
- $this->specification = $specification;
- }
- public function isSatisfiedBy(Item $item): bool
- {
- return !$this->specification->isSatisfiedBy($item);
- }
- }