1
|
|
|
<?php namespace Cornford\Logical; |
2
|
|
|
|
3
|
|
|
use Cornford\Logical\Contracts\LogicalStatementInterface; |
4
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
5
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
6
|
|
|
|
7
|
|
|
abstract class LogicalAbstract |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Logical statement instance. |
11
|
|
|
* |
12
|
|
|
* @var \Cornford\Logical\Contracts\LogicalStatementInterface |
13
|
|
|
*/ |
14
|
|
|
protected static $logicalStatementInstance; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Property accessor instance. |
18
|
|
|
* |
19
|
|
|
* @var \Symfony\Component\PropertyAccess\PropertyAccessor |
20
|
|
|
*/ |
21
|
|
|
protected static $propertyAccessorInstance; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The input array. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $input = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The logic string. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $logic; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The decoded logic statements. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $decodedLogicStatements = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The output results. |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $results = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Construct Logical object. |
53
|
|
|
* |
54
|
|
|
* @param array $input |
55
|
|
|
* @param string|null $logic |
56
|
|
|
* @param LogicalStatementInterface $logicalStatement |
57
|
|
|
*/ |
58
|
|
|
public function __construct( |
59
|
|
|
array $input = [], |
60
|
|
|
$logic = null, |
61
|
|
|
LogicalStatementInterface $logicalStatement |
62
|
|
|
) { |
63
|
|
|
self::$logicalStatementInstance = $logicalStatement; |
64
|
|
|
self::$propertyAccessorInstance = PropertyAccess::createPropertyAccessor(); |
65
|
|
|
$this->input = $input; |
66
|
|
|
$this->logic = $logic; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set the logical statement instance. |
71
|
|
|
* |
72
|
|
|
* @param LogicalStatementInterface $value |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public function setLogicalStatementInstance(LogicalStatementInterface $value) |
77
|
|
|
{ |
78
|
|
|
self::$logicalStatementInstance = $value; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the logic statement instance. |
83
|
|
|
* |
84
|
|
|
* @return LogicalStatementInterface |
85
|
|
|
*/ |
86
|
|
|
public function getLogicalStatementInstance() |
87
|
|
|
{ |
88
|
|
|
return self::$logicalStatementInstance; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the property accessor instance. |
93
|
|
|
* |
94
|
|
|
* @return PropertyAccessor |
95
|
|
|
*/ |
96
|
|
|
protected function getPropertyAccessorInstance() |
97
|
|
|
{ |
98
|
|
|
return self::$propertyAccessorInstance; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Set the input value. |
103
|
|
|
* |
104
|
|
|
* @param array $value |
105
|
|
|
* |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
public function setInput(array $value = []) |
109
|
|
|
{ |
110
|
|
|
$this->input = $value; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the input value. |
115
|
|
|
* |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
public function getInput() |
119
|
|
|
{ |
120
|
|
|
return $this->input; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Set the logic value. |
125
|
|
|
* |
126
|
|
|
* @param string $value |
127
|
|
|
* |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
public function setLogic($value) |
131
|
|
|
{ |
132
|
|
|
$this->logic = $value; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get the logic value. |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function getLogic() |
141
|
|
|
{ |
142
|
|
|
return $this->logic; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Set a decoded logic statements value. |
147
|
|
|
* |
148
|
|
|
* @param array $value |
149
|
|
|
* |
150
|
|
|
* @return void |
151
|
|
|
*/ |
152
|
|
|
protected function setDecodedLogicStatements($value) |
153
|
|
|
{ |
154
|
|
|
$this->decodedLogicStatements = $value; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Set a decoded logic statement value. |
159
|
|
|
* |
160
|
|
|
* @param array $value |
161
|
|
|
* |
162
|
|
|
* @return void |
163
|
|
|
*/ |
164
|
|
|
protected function setDecodedLogicStatement($value) |
165
|
|
|
{ |
166
|
|
|
if (!array_key_exists(key($value), $this->decodedLogicStatements)) { |
167
|
|
|
$this->decodedLogicStatements[key($value)] = []; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$this->decodedLogicStatements[key($value)] = array_merge($this->decodedLogicStatements[key($value)], $value); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get the decoded logic statements value. |
175
|
|
|
* |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
|
|
protected function getDecodedLogicStatements() |
179
|
|
|
{ |
180
|
|
|
return $this->decodedLogicStatements; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Set the results value. |
185
|
|
|
* |
186
|
|
|
* @param array $value |
187
|
|
|
* |
188
|
|
|
* @return void |
189
|
|
|
*/ |
190
|
|
|
protected function setResults($value) |
191
|
|
|
{ |
192
|
|
|
$this->results = $value; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Merge the results value with an input results value. |
197
|
|
|
* |
198
|
|
|
* @param array $value |
199
|
|
|
* |
200
|
|
|
* @return void |
201
|
|
|
*/ |
202
|
|
|
protected function mergeResults($value) |
203
|
|
|
{ |
204
|
|
|
foreach ($value as $key => $result) { |
205
|
|
|
if (!array_key_exists($key, $this->results)) { |
206
|
|
|
$this->results[$key] = $result; |
207
|
|
|
} else { |
208
|
|
|
if (!in_array($result, $this->results)) { |
209
|
|
|
if (gettype($result) != $this->results[$key]) { |
210
|
|
|
$this->results[] = $result; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get the results value. |
219
|
|
|
* |
220
|
|
|
* @return array |
221
|
|
|
*/ |
222
|
|
|
public function getResults() |
223
|
|
|
{ |
224
|
|
|
return $this->results; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Reset items. |
229
|
|
|
* |
230
|
|
|
* @return self |
231
|
|
|
*/ |
232
|
|
|
public function reset() |
233
|
|
|
{ |
234
|
|
|
$this->input = []; |
235
|
|
|
$this->logic = null; |
236
|
|
|
$this->results = []; |
237
|
|
|
$this->decodedLogicStatements = []; |
238
|
|
|
|
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|