1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright © 2018 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
|
|
|
/** |
11
|
|
|
* Class Combine |
12
|
|
|
*/ |
13
|
|
|
final class Combine extends AbstractNode implements CombineInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Operator code |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $operator; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Invert final result flag |
24
|
|
|
* |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
private $isInvert; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Nodes elements array |
31
|
|
|
* |
32
|
|
|
* @var \LogicTree\Node\NodeInterface[] |
33
|
|
|
*/ |
34
|
|
|
private $nodes; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Combine constructor |
38
|
|
|
* |
39
|
|
|
* @param string $operator |
40
|
|
|
* @param bool $isInvert [optional] Is false by default. |
41
|
|
|
* @param \LogicTree\Node\NodeInterface[] $children [optional] Is empty by default. |
42
|
|
|
*/ |
43
|
|
|
public function __construct( |
44
|
|
|
string $operator, |
45
|
|
|
bool $isInvert = null, |
46
|
|
|
array $children = [] |
|
|
|
|
47
|
|
|
) { |
48
|
|
|
$this->setOperator($operator); |
49
|
|
|
$this->setIsInvert($isInvert ?? false); |
50
|
|
|
$this->setChildren($children); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function getChildren(): array |
57
|
|
|
{ |
58
|
|
|
return $this->nodes; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function setChildren(array $children): CombineInterface |
65
|
|
|
{ |
66
|
|
|
$this->nodes = []; |
|
|
|
|
67
|
|
|
|
68
|
|
|
foreach ($children as $child) { |
69
|
|
|
$this->addChild($child); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function addChild(NodeInterface $condition): CombineInterface |
79
|
|
|
{ |
80
|
|
|
if ($condition === $this) { |
|
|
|
|
81
|
|
|
throw new \LogicException('Child node cannot be the current instance of itself.'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$condition->setParent($this); |
85
|
|
|
$this->nodes[] = $condition; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function getCount(): int |
94
|
|
|
{ |
95
|
|
|
return \count($this->getChildren()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function hasChildren(): bool |
102
|
|
|
{ |
103
|
|
|
return $this->getCount() > 0; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function isInvert(): bool |
110
|
|
|
{ |
111
|
|
|
return $this->isInvert; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function setIsInvert(bool $isInvert): CombineInterface |
118
|
|
|
{ |
119
|
|
|
$this->isInvert = $isInvert; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
|
|
public function getOperator(): string |
128
|
|
|
{ |
129
|
|
|
return $this->operator; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
|
|
public function setOperator(string $operator): CombineInterface |
136
|
|
|
{ |
137
|
|
|
$this->operator = $operator; |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|