| @@ 15-60 (lines=46) @@ | ||
| 12 | /** |
|
| 13 | * Class Conjunction |
|
| 14 | */ |
|
| 15 | class Conjunction implements Matchable |
|
| 16 | { |
|
| 17 | /** |
|
| 18 | * @var array|Matchable[] |
|
| 19 | */ |
|
| 20 | private $matchable = []; |
|
| 21 | ||
| 22 | /** |
|
| 23 | * OrTypeHint constructor. |
|
| 24 | * @param Matchable $matcher |
|
| 25 | */ |
|
| 26 | public function __construct(Matchable $matcher) |
|
| 27 | { |
|
| 28 | $this->addMatcher($matcher); |
|
| 29 | } |
|
| 30 | ||
| 31 | /** |
|
| 32 | * @param Matchable $matcher |
|
| 33 | * @return Disjunction |
|
| 34 | */ |
|
| 35 | public function addMatcher(Matchable $matcher): Matchable |
|
| 36 | { |
|
| 37 | if ($matcher instanceof self) { |
|
| 38 | $this->matchable = \array_merge($this->matchable, $matcher->matchable); |
|
| 39 | } else { |
|
| 40 | $this->matchable[] = $matcher; |
|
| 41 | } |
|
| 42 | ||
| 43 | return $this; |
|
| 44 | } |
|
| 45 | ||
| 46 | /** |
|
| 47 | * @param mixed $value |
|
| 48 | * @return bool |
|
| 49 | */ |
|
| 50 | public function match($value): bool |
|
| 51 | { |
|
| 52 | foreach ($this->matchable as $matcher) { |
|
| 53 | if (! $matcher->match($value)) { |
|
| 54 | return false; |
|
| 55 | } |
|
| 56 | } |
|
| 57 | ||
| 58 | return true; |
|
| 59 | } |
|
| 60 | } |
|
| 61 | ||
| @@ 15-61 (lines=47) @@ | ||
| 12 | /** |
|
| 13 | * Class Disjunction |
|
| 14 | */ |
|
| 15 | class Disjunction implements Matchable |
|
| 16 | { |
|
| 17 | /** |
|
| 18 | * @var array|Matchable[] |
|
| 19 | */ |
|
| 20 | private $matchable = []; |
|
| 21 | ||
| 22 | /** |
|
| 23 | * OrTypeHint constructor. |
|
| 24 | * @param Matchable $matcher |
|
| 25 | */ |
|
| 26 | public function __construct(Matchable $matcher) |
|
| 27 | { |
|
| 28 | $this->addMatcher($matcher); |
|
| 29 | } |
|
| 30 | ||
| 31 | /** |
|
| 32 | * @param Matchable $matcher |
|
| 33 | * @return Disjunction |
|
| 34 | */ |
|
| 35 | public function addMatcher(Matchable $matcher): Matchable |
|
| 36 | { |
|
| 37 | if ($matcher instanceof self) { |
|
| 38 | $this->matchable = \array_merge($this->matchable, $matcher->matchable); |
|
| 39 | } else { |
|
| 40 | $this->matchable[] = $matcher; |
|
| 41 | } |
|
| 42 | ||
| 43 | return $this; |
|
| 44 | } |
|
| 45 | ||
| 46 | ||
| 47 | /** |
|
| 48 | * @param mixed $value |
|
| 49 | * @return bool |
|
| 50 | */ |
|
| 51 | public function match($value): bool |
|
| 52 | { |
|
| 53 | foreach ($this->matchable as $matcher) { |
|
| 54 | if ($matcher->match($value)) { |
|
| 55 | return true; |
|
| 56 | } |
|
| 57 | } |
|
| 58 | ||
| 59 | return false; |
|
| 60 | } |
|
| 61 | } |
|
| 62 | ||