1 | <?php |
||
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 | 52 | final public function applies() |
|
134 | |||
135 | /** |
||
136 | * hook method for logging etc. |
||
137 | * |
||
138 | * @param Exception $e |
||
139 | */ |
||
140 | 12 | protected function handleException($e) |
|
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) |
|
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) |
|
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) |
|
180 | |||
181 | /** |
||
182 | * Inverse current rule |
||
183 | * |
||
184 | * @return Rule |
||
185 | */ |
||
186 | 2 | final public function not() |
|
190 | |||
191 | /** |
||
192 | * |
||
193 | * @return string |
||
194 | */ |
||
195 | 5 | public function toString() |
|
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() |
|
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) |
|
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) |
|
250 | |||
251 | /** |
||
252 | * Clear the results |
||
253 | */ |
||
254 | 52 | private function clearResult() |
|
258 | |||
259 | 52 | private function clearCache() |
|
263 | |||
264 | 45 | private function setCache($result) |
|
270 | |||
271 | /** |
||
272 | * Has any result? |
||
273 | * |
||
274 | * @return boolean |
||
275 | */ |
||
276 | 1 | final public function hasResult() |
|
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) |
|
292 | |||
293 | /** |
||
294 | * return the cached value |
||
295 | * |
||
296 | * @return boolean |
||
297 | */ |
||
298 | 1 | final protected function getCache() |
|
302 | |||
303 | /** |
||
304 | * should we return a cached value? |
||
305 | * |
||
306 | * @return boolean |
||
307 | */ |
||
308 | 52 | private function shouldReturnCache() |
|
317 | |||
318 | /** |
||
319 | * |
||
320 | * @return boolean |
||
321 | */ |
||
322 | 52 | final public function getCacheEnabled() |
|
326 | |||
327 | 1 | public function __toString() |
|
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.