thomas-kl1 /
php-combine-conditions
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Copyright © Thomas Klein, All rights reserved. |
||
| 4 | * See LICENSE bundled with this library for license details. |
||
| 5 | */ |
||
| 6 | declare(strict_types=1); |
||
| 7 | |||
| 8 | namespace LogicTree\Node; |
||
| 9 | |||
| 10 | use LogicException; |
||
| 11 | use function count; |
||
| 12 | |||
| 13 | final class Combine extends AbstractNode implements CombineInterface |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | private $operator; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var bool |
||
| 22 | */ |
||
| 23 | private $isInvert; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var NodeInterface[] |
||
| 27 | */ |
||
| 28 | private $nodes; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param string $operator |
||
| 32 | * @param bool $isInvert [optional] Is false by default. |
||
| 33 | * @param NodeInterface[] $children [optional] Is empty by default. |
||
| 34 | */ |
||
| 35 | public function __construct( |
||
| 36 | string $operator, |
||
| 37 | bool $isInvert = null, |
||
| 38 | array $children = [] |
||
| 39 | ) { |
||
| 40 | $this->setOperator($operator); |
||
| 41 | $this->setIsInvert($isInvert ?? false); |
||
| 42 | $this->setChildren($children); |
||
| 43 | } |
||
| 44 | |||
| 45 | public function getChildren(): array |
||
| 46 | { |
||
| 47 | return $this->nodes; |
||
| 48 | } |
||
| 49 | |||
| 50 | public function setChildren(array $children): CombineInterface |
||
| 51 | { |
||
| 52 | $this->nodes = []; |
||
| 53 | |||
| 54 | foreach ($children as $child) { |
||
| 55 | $this->addChild($child); |
||
| 56 | } |
||
| 57 | |||
| 58 | return $this; |
||
| 59 | } |
||
| 60 | |||
| 61 | public function addChild(NodeInterface $condition): CombineInterface |
||
| 62 | { |
||
| 63 | if ($condition === $this) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 64 | throw new LogicException('Child node cannot be the current instance of itself.'); |
||
| 65 | } |
||
| 66 | |||
| 67 | $condition->setParent($this); |
||
| 68 | $this->nodes[] = $condition; |
||
| 69 | |||
| 70 | return $this; |
||
| 71 | } |
||
| 72 | |||
| 73 | public function getCount(): int |
||
| 74 | { |
||
| 75 | return count($this->getChildren()); |
||
| 76 | } |
||
| 77 | |||
| 78 | public function hasChildren(): bool |
||
| 79 | { |
||
| 80 | return $this->getCount() > 0; |
||
| 81 | } |
||
| 82 | |||
| 83 | public function isInvert(): bool |
||
| 84 | { |
||
| 85 | return $this->isInvert; |
||
| 86 | } |
||
| 87 | |||
| 88 | public function setIsInvert(bool $isInvert): CombineInterface |
||
| 89 | { |
||
| 90 | $this->isInvert = $isInvert; |
||
| 91 | |||
| 92 | return $this; |
||
| 93 | } |
||
| 94 | |||
| 95 | public function getOperator(): string |
||
| 96 | { |
||
| 97 | return $this->operator; |
||
| 98 | } |
||
| 99 | |||
| 100 | public function setOperator(string $operator): CombineInterface |
||
| 101 | { |
||
| 102 | $this->operator = $operator; |
||
| 103 | |||
| 104 | return $this; |
||
| 105 | } |
||
| 106 | } |
||
| 107 |