| @@ 19-56 (lines=38) @@ | ||
| 16 | * @package Dutek\Predicate |
|
| 17 | * @author Dušan Vejin <[email protected]> |
|
| 18 | */ |
|
| 19 | final class AndPredicate implements PredicateDecorator |
|
| 20 | { |
|
| 21 | protected $predicate1; |
|
| 22 | protected $predicate2; |
|
| 23 | ||
| 24 | /** |
|
| 25 | * AndPredicate constructor. |
|
| 26 | * |
|
| 27 | * @param Predicate $predicate1 The first predicate to check. |
|
| 28 | * @param Predicate $predicate2 The second predicate to check. |
|
| 29 | */ |
|
| 30 | public function __construct(Predicate $predicate1, Predicate $predicate2) |
|
| 31 | { |
|
| 32 | $this->predicate1 = $predicate1; |
|
| 33 | $this->predicate2 = $predicate2; |
|
| 34 | } |
|
| 35 | ||
| 36 | /** |
|
| 37 | * Gets the two predicates being decorated as an array. |
|
| 38 | * |
|
| 39 | * @return array The predicates. |
|
| 40 | */ |
|
| 41 | public function getPredicates() : array |
|
| 42 | { |
|
| 43 | return [$this->predicate1, $this->predicate2]; |
|
| 44 | } |
|
| 45 | ||
| 46 | /** |
|
| 47 | * Evaluates the predicate returning true if both predicates return true. |
|
| 48 | * |
|
| 49 | * @param mixed $value The input value. |
|
| 50 | * @return bool true if both decorated predicates return true |
|
| 51 | */ |
|
| 52 | public function __invoke($value) : bool |
|
| 53 | { |
|
| 54 | return ($this->predicate1)($value) && ($this->predicate2)($value); |
|
| 55 | } |
|
| 56 | } |
|
| 57 | ||
| @@ 19-56 (lines=38) @@ | ||
| 16 | * @package Dutek\Predicate |
|
| 17 | * @author Dušan Vejin <[email protected]> |
|
| 18 | */ |
|
| 19 | final class OrPredicate implements PredicateDecorator |
|
| 20 | { |
|
| 21 | protected $predicate1; |
|
| 22 | protected $predicate2; |
|
| 23 | ||
| 24 | /** |
|
| 25 | * AndPredicate constructor. |
|
| 26 | * |
|
| 27 | * @param Predicate $predicate1 The first predicate to check. |
|
| 28 | * @param Predicate $predicate2 The second predicate to check. |
|
| 29 | */ |
|
| 30 | public function __construct(Predicate $predicate1, Predicate $predicate2) |
|
| 31 | { |
|
| 32 | $this->predicate1 = $predicate1; |
|
| 33 | $this->predicate2 = $predicate2; |
|
| 34 | } |
|
| 35 | ||
| 36 | /** |
|
| 37 | * Gets the two predicates being decorated as an array. |
|
| 38 | * |
|
| 39 | * @return array The predicates. |
|
| 40 | */ |
|
| 41 | public function getPredicates() : array |
|
| 42 | { |
|
| 43 | return [$this->predicate1, $this->predicate2]; |
|
| 44 | } |
|
| 45 | ||
| 46 | /** |
|
| 47 | * Evaluates the predicate returning true if either predicate returns true. |
|
| 48 | * |
|
| 49 | * @param mixed $value The input value. |
|
| 50 | * @return bool true if either decorated predicate returns true |
|
| 51 | */ |
|
| 52 | public function __invoke($value) : bool |
|
| 53 | { |
|
| 54 | return ($this->predicate1)($value) || ($this->predicate2)($value); |
|
| 55 | } |
|
| 56 | } |
|
| 57 | ||