-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRulesChecker.php
330 lines (298 loc) · 11.2 KB
/
RulesChecker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.7
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Datasource;
use InvalidArgumentException;
/**
* Contains logic for storing and checking rules on entities
*
* RulesCheckers are used by Table classes to ensure that the
* current entity state satisfies the application logic and business rules.
*
* RulesCheckers afford different rules to be applied in the create and update
* scenario.
*
* ### Adding rules
*
* Rules must be callable objects that return true/false depending on whether or
* not the rule has been satisfied. You can use RulesChecker::add(), RulesChecker::addCreate(),
* RulesChecker::addUpdate() and RulesChecker::addDelete to add rules to a checker.
*
* ### Running checks
*
* Generally a Table object will invoke the rules objects, but you can manually
* invoke the checks by calling RulesChecker::checkCreate(), RulesChecker::checkUpdate() or
* RulesChecker::checkDelete().
*/
class RulesChecker
{
/**
* Indicates that the checking rules to apply are those used for creating entities
*
* @var string
*/
public const CREATE = 'create';
/**
* Indicates that the checking rules to apply are those used for updating entities
*
* @var string
*/
public const UPDATE = 'update';
/**
* Indicates that the checking rules to apply are those used for deleting entities
*
* @var string
*/
public const DELETE = 'delete';
/**
* The list of rules to be checked on both create and update operations
*
* @var \Cake\Datasource\RuleInvoker[]
*/
protected $_rules = [];
/**
* The list of rules to check during create operations
*
* @var \Cake\Datasource\RuleInvoker[]
*/
protected $_createRules = [];
/**
* The list of rules to check during update operations
*
* @var \Cake\Datasource\RuleInvoker[]
*/
protected $_updateRules = [];
/**
* The list of rules to check during delete operations
*
* @var \Cake\Datasource\RuleInvoker[]
*/
protected $_deleteRules = [];
/**
* List of options to pass to every callable rule
*
* @var array
*/
protected $_options = [];
/**
* Whether or not to use I18n functions for translating default error messages
*
* @var bool
*/
protected $_useI18n = false;
/**
* Constructor. Takes the options to be passed to all rules.
*
* @param array $options The options to pass to every rule
*/
public function __construct(array $options = [])
{
$this->_options = $options;
$this->_useI18n = function_exists('__d');
}
/**
* Adds a rule that will be applied to the entity both on create and update
* operations.
*
* ### Options
*
* The options array accept the following special keys:
*
* - `errorField`: The name of the entity field that will be marked as invalid
* if the rule does not pass.
* - `message`: The error message to set to `errorField` if the rule does not pass.
*
* @param callable $rule A callable function or object that will return whether
* the entity is valid or not.
* @param string|array|null $name The alias for a rule, or an array of options.
* @param array $options List of extra options to pass to the rule callable as
* second argument.
* @return $this
*/
public function add(callable $rule, $name = null, array $options = [])
{
$this->_rules[] = $this->_addError($rule, $name, $options);
return $this;
}
/**
* Adds a rule that will be applied to the entity on create operations.
*
* ### Options
*
* The options array accept the following special keys:
*
* - `errorField`: The name of the entity field that will be marked as invalid
* if the rule does not pass.
* - `message`: The error message to set to `errorField` if the rule does not pass.
*
* @param callable $rule A callable function or object that will return whether
* the entity is valid or not.
* @param string|array|null $name The alias for a rule or an array of options.
* @param array $options List of extra options to pass to the rule callable as
* second argument.
* @return $this
*/
public function addCreate(callable $rule, $name = null, array $options = [])
{
$this->_createRules[] = $this->_addError($rule, $name, $options);
return $this;
}
/**
* Adds a rule that will be applied to the entity on update operations.
*
* ### Options
*
* The options array accept the following special keys:
*
* - `errorField`: The name of the entity field that will be marked as invalid
* if the rule does not pass.
* - `message`: The error message to set to `errorField` if the rule does not pass.
*
* @param callable $rule A callable function or object that will return whether
* the entity is valid or not.
* @param string|array|null $name The alias for a rule, or an array of options.
* @param array $options List of extra options to pass to the rule callable as
* second argument.
* @return $this
*/
public function addUpdate(callable $rule, $name = null, array $options = [])
{
$this->_updateRules[] = $this->_addError($rule, $name, $options);
return $this;
}
/**
* Adds a rule that will be applied to the entity on delete operations.
*
* ### Options
*
* The options array accept the following special keys:
*
* - `errorField`: The name of the entity field that will be marked as invalid
* if the rule does not pass.
* - `message`: The error message to set to `errorField` if the rule does not pass.
*
* @param callable $rule A callable function or object that will return whether
* the entity is valid or not.
* @param string|array|null $name The alias for a rule, or an array of options.
* @param array $options List of extra options to pass to the rule callable as
* second argument.
* @return $this
*/
public function addDelete(callable $rule, $name = null, array $options = [])
{
$this->_deleteRules[] = $this->_addError($rule, $name, $options);
return $this;
}
/**
* Runs each of the rules by passing the provided entity and returns true if all
* of them pass. The rules to be applied are depended on the $mode parameter which
* can only be RulesChecker::CREATE, RulesChecker::UPDATE or RulesChecker::DELETE
*
* @param \Cake\Datasource\EntityInterface $entity The entity to check for validity.
* @param string $mode Either 'create, 'update' or 'delete'.
* @param array $options Extra options to pass to checker functions.
* @return bool
* @throws \InvalidArgumentException if an invalid mode is passed.
*/
public function check(EntityInterface $entity, string $mode, array $options = []): bool
{
if ($mode === self::CREATE) {
return $this->checkCreate($entity, $options);
}
if ($mode === self::UPDATE) {
return $this->checkUpdate($entity, $options);
}
if ($mode === self::DELETE) {
return $this->checkDelete($entity, $options);
}
throw new InvalidArgumentException('Wrong checking mode: ' . $mode);
}
/**
* Runs each of the rules by passing the provided entity and returns true if all
* of them pass. The rules selected will be only those specified to be run on 'create'
*
* @param \Cake\Datasource\EntityInterface $entity The entity to check for validity.
* @param array $options Extra options to pass to checker functions.
* @return bool
*/
public function checkCreate(EntityInterface $entity, array $options = []): bool
{
return $this->_checkRules($entity, $options, array_merge($this->_rules, $this->_createRules));
}
/**
* Runs each of the rules by passing the provided entity and returns true if all
* of them pass. The rules selected will be only those specified to be run on 'update'
*
* @param \Cake\Datasource\EntityInterface $entity The entity to check for validity.
* @param array $options Extra options to pass to checker functions.
* @return bool
*/
public function checkUpdate(EntityInterface $entity, array $options = []): bool
{
return $this->_checkRules($entity, $options, array_merge($this->_rules, $this->_updateRules));
}
/**
* Runs each of the rules by passing the provided entity and returns true if all
* of them pass. The rules selected will be only those specified to be run on 'delete'
*
* @param \Cake\Datasource\EntityInterface $entity The entity to check for validity.
* @param array $options Extra options to pass to checker functions.
* @return bool
*/
public function checkDelete(EntityInterface $entity, array $options = []): bool
{
return $this->_checkRules($entity, $options, $this->_deleteRules);
}
/**
* Used by top level functions checkDelete, checkCreate and checkUpdate, this function
* iterates an array containing the rules to be checked and checks them all.
*
* @param \Cake\Datasource\EntityInterface $entity The entity to check for validity.
* @param array $options Extra options to pass to checker functions.
* @param \Cake\Datasource\RuleInvoker[] $rules The list of rules that must be checked.
* @return bool
*/
protected function _checkRules(EntityInterface $entity, array $options = [], array $rules = []): bool
{
$success = true;
$options += $this->_options;
foreach ($rules as $rule) {
$success = $rule($entity, $options) && $success;
}
return $success;
}
/**
* Utility method for decorating any callable so that if it returns false, the correct
* property in the entity is marked as invalid.
*
* @param callable $rule The rule to decorate
* @param string|array|null $name The alias for a rule or an array of options
* @param array $options The options containing the error message and field.
* @return \Cake\Datasource\RuleInvoker
*/
protected function _addError(callable $rule, $name = null, array $options = []): RuleInvoker
{
if (is_array($name)) {
$options = $name;
$name = null;
}
if (!($rule instanceof RuleInvoker)) {
$rule = new RuleInvoker($rule, $name, $options);
} else {
$rule->setOptions($options)->setName($name);
}
return $rule;
}
}