|
1
|
|
|
<?php |
|
2
|
|
|
namespace izzum\rules; |
|
3
|
|
|
use izzum\rules\Exception; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Rules are used to encapsulate business rules/logic of the type where you ask |
|
7
|
|
|
* a |
|
8
|
|
|
* question: 'does this piece of code conform to a specific business rule?' |
|
9
|
|
|
* |
|
10
|
|
|
* Rules should never have side effects and should only return true or false. |
|
11
|
|
|
* |
|
12
|
|
|
* This Rule serves as a base class for all your business logic, encapsulating |
|
13
|
|
|
* and centralizing the logic in a class and making it reusable through it's |
|
14
|
|
|
* interface. |
|
15
|
|
|
* |
|
16
|
|
|
* This is a way of seperating mechanism and policy of code. |
|
17
|
|
|
* In other words: how it is done versus what should be done. The mechanism is a |
|
18
|
|
|
* rule |
|
19
|
|
|
* library, the policy is what is defined in the rule. |
|
20
|
|
|
* https://en.wikipedia.org/wiki/Separation_of_mechanism_and_policy |
|
21
|
|
|
* |
|
22
|
|
|
* The rules package aims to prevent code like this: |
|
23
|
|
|
* if ($name == 'blabla' && $password == 'lala') { |
|
24
|
|
|
* //do something here. the statement above might be duplicated in a lot of |
|
25
|
|
|
* places |
|
26
|
|
|
* } |
|
27
|
|
|
* and substitute it by this: |
|
28
|
|
|
* $rule = new AccesRule($name, $password); |
|
29
|
|
|
* if($rule->applies()) { |
|
30
|
|
|
* //do something |
|
31
|
|
|
* } |
|
32
|
|
|
* |
|
33
|
|
|
* or: |
|
34
|
|
|
* $rule = new IsOrderTaggedForDelivery($order); |
|
35
|
|
|
* $rule->applies(); |
|
36
|
|
|
* |
|
37
|
|
|
* usage: |
|
38
|
|
|
* Clients should subclass this Rule and implement the |
|
39
|
|
|
* protected '_applies' method and let that return a boolean value. |
|
40
|
|
|
* |
|
41
|
|
|
* A concrete Rule (a subclass) can (and should be) injected with contextual |
|
42
|
|
|
* data via |
|
43
|
|
|
* dependency injection in the constructor |
|
44
|
|
|
* |
|
45
|
|
|
* |
|
46
|
|
|
* @author Rolf Vreijdenberger |
|
47
|
|
|
* @author Richard Ruiter |
|
48
|
|
|
* @link https://en.wikipedia.org/wiki/Separation_of_mechanism_and_policy |
|
49
|
|
|
*/ |
|
50
|
|
|
abstract class Rule implements IRule { |
|
51
|
|
|
/** |
|
52
|
|
|
* contains results that a concrete Rule can set. |
|
53
|
|
|
* This allows clients of the Rule to check if certain conditions in |
|
54
|
|
|
* a rule have been met. The subclassed rule should add a result itself. |
|
55
|
|
|
* This will happen in case of multiple conditions being checked for a rule |
|
56
|
|
|
* to evaluate to true. If one of the conditions is not met, you can set a |
|
57
|
|
|
* result there. |
|
58
|
|
|
* |
|
59
|
|
|
* @var RuleResult[] |
|
60
|
|
|
*/ |
|
61
|
|
|
private $result = array(); |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* should we cache the result or not? |
|
65
|
|
|
* TRICKY: this might be very dangerous for non-deterministic rules but a |
|
66
|
|
|
* great speed optimizer for rules that are evaluated multiple times and are |
|
67
|
|
|
* deterministic |
|
68
|
|
|
* |
|
69
|
|
|
* @var boolean |
|
70
|
|
|
*/ |
|
71
|
|
|
private $use_caching = false; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* if the result is cached, it will be put in this variable |
|
75
|
|
|
* after the applies method has run |
|
76
|
|
|
* |
|
77
|
|
|
* @var boolean |
|
78
|
|
|
*/ |
|
79
|
|
|
private $cache; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* A concrete rule should at least implement the _applies method and return |
|
83
|
|
|
* a |
|
84
|
|
|
* boolean. |
|
85
|
|
|
* When the rule logic cannot determine if it applies then an exception |
|
86
|
|
|
* should |
|
87
|
|
|
* be thrown instead of a boolean value |
|
88
|
|
|
* |
|
89
|
|
|
* @return boolean |
|
90
|
|
|
* @throws \Exception |
|
91
|
|
|
*/ |
|
92
|
|
|
abstract protected function _applies(); |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* The applies method is the only point where the rule can be validated. |
|
96
|
|
|
* |
|
97
|
|
|
* Internally each rule implements the _applies() method to do the actual |
|
98
|
|
|
* validation. |
|
99
|
|
|
* |
|
100
|
|
|
* There are only two types of outcome for each rule. |
|
101
|
|
|
* Either the rule applies (true) or doesn't (false). |
|
102
|
|
|
* Any other outcome should always be thrown as an exception so no false |
|
103
|
|
|
* assumptions can be made by the caller. For example when a rule returns a |
|
104
|
|
|
* NULL value the caller may asume that false is meant. The rule cannot |
|
105
|
|
|
* trust that the caller checks the boolean type so we will. |
|
106
|
|
|
* |
|
107
|
|
|
* @return boolean |
|
108
|
|
|
* @throws Exception https://en.wikipedia.org/wiki/Template_method_pattern |
|
109
|
|
|
*/ |
|
110
|
51 |
|
final public function applies() |
|
111
|
|
|
{ |
|
112
|
|
|
try { |
|
113
|
51 |
|
if ($this->shouldReturnCache()) { |
|
114
|
1 |
|
return $this->getCache(); |
|
115
|
|
|
} |
|
116
|
51 |
|
$this->clearResult(); |
|
117
|
51 |
|
$this->clearCache(); |
|
118
|
51 |
|
$result = $this->_applies(); |
|
119
|
49 |
|
if (is_bool($result)) { |
|
120
|
44 |
|
$this->setCache($result); |
|
121
|
44 |
|
return $result; |
|
122
|
|
|
} else { |
|
123
|
5 |
|
throw new Exception('A rule must return a boolean.', Exception::CODE_NONBOOLEAN); |
|
124
|
|
|
} |
|
125
|
12 |
|
} catch(Exception $e) { |
|
126
|
12 |
|
$this->handleException($e); |
|
127
|
12 |
|
throw $e; |
|
128
|
1 |
|
} catch(\Exception $e) { |
|
129
|
1 |
|
$e = new Exception($e->getMessage(), $e->getCode(), $e); |
|
130
|
1 |
|
$this->handleException($e); |
|
131
|
1 |
|
throw $e; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* hook method for logging etc. |
|
137
|
|
|
* |
|
138
|
|
|
* @param Exception $e |
|
139
|
|
|
*/ |
|
140
|
12 |
|
protected function handleException($e) |
|
|
|
|
|
|
141
|
|
|
{ |
|
142
|
|
|
// implement in subclass if needed |
|
143
|
12 |
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Chain a 'OR' rule. |
|
147
|
|
|
* This means one of the rules should apply. |
|
148
|
|
|
* |
|
149
|
|
|
* @param Rule $other |
|
150
|
|
|
* @return Rule |
|
151
|
|
|
*/ |
|
152
|
3 |
|
final public function orRule(Rule $other) |
|
153
|
|
|
{ |
|
154
|
3 |
|
return new OrRule($this, $other); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Chain a 'XOR' rule. |
|
159
|
|
|
* This means one of the rules should apply but not both. |
|
160
|
|
|
* |
|
161
|
|
|
* @param Rule $other |
|
162
|
|
|
* @return Rule |
|
163
|
|
|
*/ |
|
164
|
3 |
|
final public function xorRule(Rule $other) |
|
165
|
|
|
{ |
|
166
|
3 |
|
return new XorRule($this, $other); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Chain a 'AND' rule. |
|
171
|
|
|
* This means both rules should apply. |
|
172
|
|
|
* |
|
173
|
|
|
* @param Rule $other |
|
174
|
|
|
* @return Rule |
|
175
|
|
|
*/ |
|
176
|
3 |
|
final public function andRule(Rule $other) |
|
177
|
|
|
{ |
|
178
|
3 |
|
return new AndRule($this, $other); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Inverse current rule |
|
183
|
|
|
* |
|
184
|
|
|
* @return Rule |
|
185
|
|
|
*/ |
|
186
|
2 |
|
final public function not() |
|
187
|
|
|
{ |
|
188
|
2 |
|
return new NotRule($this); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* |
|
193
|
|
|
* @return string |
|
194
|
|
|
*/ |
|
195
|
5 |
|
public function toString() |
|
196
|
|
|
{ |
|
197
|
|
|
// includes the namespace |
|
198
|
5 |
|
return get_class($this); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Gets an array of RuleResult to check if a rule has set a certain result |
|
203
|
|
|
* for the client of the rule. |
|
204
|
|
|
* This will mostly be useful to check what actually happened when a rule |
|
205
|
|
|
* has failed and if the _applies() method actually calls 'addResult()' to |
|
206
|
|
|
* state what has happened in a rule. |
|
207
|
|
|
* |
|
208
|
|
|
* @return RuleResult[] |
|
209
|
|
|
*/ |
|
210
|
5 |
|
public function getResults() |
|
211
|
|
|
{ |
|
212
|
5 |
|
return $this->result; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Add a result to this rule. |
|
217
|
|
|
* |
|
218
|
|
|
* |
|
219
|
|
|
* this might be useful if a client wants to check if a rule |
|
220
|
|
|
* has executed certain steps during the logic of rule execution. |
|
221
|
|
|
* |
|
222
|
|
|
* @param string $result |
|
223
|
|
|
*/ |
|
224
|
1 |
|
final protected function addResult($result) |
|
225
|
|
|
{ |
|
226
|
1 |
|
$this->result [] = new RuleResult($this, $result); |
|
227
|
1 |
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Check if this rule contains a certain expected result. |
|
231
|
|
|
* This is only matched on the string, not on the class that generated |
|
232
|
|
|
* the result. you can check this against constants in a class eg: |
|
233
|
|
|
* Rule::RESULT_<*>. |
|
234
|
|
|
* In case you want to also know the class or classname, use getResults() |
|
235
|
|
|
* |
|
236
|
|
|
* @see Rule::getResults() |
|
237
|
|
|
* @param string $expected |
|
238
|
|
|
*/ |
|
239
|
1 |
|
final public function containsResult($expected) |
|
240
|
|
|
{ |
|
241
|
1 |
|
$output = false; |
|
242
|
1 |
|
$results = $this->getResults(); |
|
243
|
1 |
|
foreach ($results as $result) { |
|
244
|
1 |
|
if ($result->getResult() === $expected) { |
|
245
|
1 |
|
$output = true; |
|
246
|
1 |
|
} |
|
247
|
1 |
|
} |
|
248
|
1 |
|
return $output; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Clear the results |
|
253
|
|
|
*/ |
|
254
|
51 |
|
private function clearResult() |
|
255
|
|
|
{ |
|
256
|
51 |
|
$this->result = array(); |
|
257
|
51 |
|
} |
|
258
|
|
|
|
|
259
|
51 |
|
private function clearCache() |
|
260
|
|
|
{ |
|
261
|
51 |
|
$this->cache = null; |
|
262
|
51 |
|
} |
|
263
|
|
|
|
|
264
|
44 |
|
private function setCache($result) |
|
265
|
|
|
{ |
|
266
|
44 |
|
if ($this->getCacheEnabled()) { |
|
267
|
1 |
|
$this->cache = $result; |
|
268
|
1 |
|
} |
|
269
|
44 |
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Has any result? |
|
273
|
|
|
* |
|
274
|
|
|
* @return boolean |
|
275
|
|
|
*/ |
|
276
|
1 |
|
final public function hasResult() |
|
277
|
|
|
{ |
|
278
|
1 |
|
return count($this->getResults()) !== 0; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* should we cache the result if the rule is applied more than once? |
|
283
|
|
|
* |
|
284
|
|
|
* @param boolean $cached |
|
285
|
|
|
*/ |
|
286
|
1 |
|
final public function setCacheEnabled($cached = true) |
|
287
|
|
|
{ |
|
288
|
1 |
|
$cached = (bool) $cached; |
|
289
|
1 |
|
$this->use_caching = $cached; |
|
290
|
1 |
|
$this->clearCache(); |
|
291
|
1 |
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* return the cached value |
|
295
|
|
|
* |
|
296
|
|
|
* @return boolean |
|
297
|
|
|
*/ |
|
298
|
1 |
|
final protected function getCache() |
|
299
|
|
|
{ |
|
300
|
1 |
|
return $this->cache; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* should we return a cached value? |
|
305
|
|
|
* |
|
306
|
|
|
* @return boolean |
|
307
|
|
|
*/ |
|
308
|
51 |
|
private function shouldReturnCache() |
|
309
|
|
|
{ |
|
310
|
51 |
|
if ($this->getCacheEnabled()) { |
|
311
|
1 |
|
if ($this->cache !== null) { |
|
312
|
1 |
|
return true; |
|
313
|
|
|
} |
|
314
|
1 |
|
} |
|
315
|
51 |
|
return false; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* |
|
320
|
|
|
* @return boolean |
|
321
|
|
|
*/ |
|
322
|
51 |
|
final public function getCacheEnabled() |
|
323
|
|
|
{ |
|
324
|
51 |
|
return $this->use_caching; |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
1 |
|
public function __toString() |
|
328
|
|
|
{ |
|
329
|
1 |
|
return $this->toString(); |
|
330
|
|
|
} |
|
331
|
|
|
} |
|
332
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.