1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Model; |
13
|
|
|
|
14
|
|
|
use Phalcon\Filter\Validation\Validator\Between; |
15
|
|
|
use Phalcon\Filter\Validation\Validator\Date; |
16
|
|
|
use Phalcon\Filter\Validation\Validator\Email; |
17
|
|
|
use Phalcon\Filter\Validation\Validator\InclusionIn; |
18
|
|
|
use Phalcon\Filter\Validation\Validator\Numericality; |
19
|
|
|
use Phalcon\Filter\Validation\Validator\PresenceOf; |
20
|
|
|
use Phalcon\Filter\Validation\Validator\StringLength\Max; |
21
|
|
|
use Phalcon\Filter\Validation\Validator\StringLength\Min; |
22
|
|
|
use Phalcon\Filter\Validation\Validator\Uniqueness; |
23
|
|
|
use Zemit\Db\Column; |
24
|
|
|
use Zemit\Filter\Validation\Validator\Color; |
25
|
|
|
use Zemit\Filter\Validation\Validator\Json; |
|
|
|
|
26
|
|
|
use Zemit\Filter\Validation; |
27
|
|
|
|
28
|
|
|
trait Validate |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Apply generic validation to a validator object. |
32
|
|
|
* |
33
|
|
|
* @param Validation|null $validator The validator object to apply the validation rules to. If null, a new Validation object will be created. |
34
|
|
|
* |
35
|
|
|
* @return Validation The validator object with the generic validation rules applied. |
36
|
|
|
*/ |
37
|
2 |
|
public function genericValidation(?Validation $validator = null): Validation |
38
|
|
|
{ |
39
|
2 |
|
$validator ??= new Validation(); |
40
|
|
|
|
41
|
2 |
|
$this->addPositionValidation($validator); |
42
|
2 |
|
$this->addSoftDeleteValidation($validator); |
43
|
2 |
|
$this->addCreatedValidation($validator); |
44
|
2 |
|
$this->addUpdatedValidation($validator); |
45
|
2 |
|
$this->addDeletedValidation($validator); |
46
|
2 |
|
$this->addRestoredValidation($validator); |
47
|
|
|
|
48
|
2 |
|
return $validator; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Add validation to ensure that a field is not empty |
53
|
|
|
* |
54
|
|
|
* @param Validation $validator The validation object to add the validation to |
55
|
|
|
* @param string $field The name of the field to validate |
56
|
|
|
* @param bool $allowEmpty Whether to allow empty values. Default is false. |
57
|
|
|
* |
58
|
|
|
* @return Validation The updated validation object |
59
|
|
|
*/ |
60
|
2 |
|
public function addNotEmptyValidation(Validation $validator, string $field, bool $allowEmpty = false): Validation |
61
|
|
|
{ |
62
|
2 |
|
if (!$allowEmpty) { |
63
|
2 |
|
$this->addPresenceValidation($validator, $field, $allowEmpty); |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
return $validator; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Add presence validation to a field in a validator object |
71
|
|
|
* |
72
|
|
|
* @param Validation $validator The validator object to add the validation to |
73
|
|
|
* @param string $field The name of the field to validate |
74
|
|
|
* @param bool $allowEmpty Whether to allow empty values for the field or not (default: true) |
75
|
|
|
* |
76
|
|
|
* @return Validation The modified validator object after adding the validation |
77
|
|
|
*/ |
78
|
2 |
|
public function addPresenceValidation(Validation $validator, string $field, bool $allowEmpty = true): Validation |
79
|
|
|
{ |
80
|
2 |
|
$validator->add($field, new PresenceOf([ |
81
|
2 |
|
'message' => $this->_('required'), |
|
|
|
|
82
|
2 |
|
'allowEmpty' => $allowEmpty, |
83
|
2 |
|
])); |
84
|
|
|
|
85
|
2 |
|
return $validator; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Add validations for an unsigned integer field |
90
|
|
|
* |
91
|
|
|
* @param Validation $validator The validation object to add rules to |
92
|
|
|
* @param string $field The name of the field to validate (default: 'id') |
93
|
|
|
* @param bool $allowEmpty Whether to allow the field to be empty (default: true) |
94
|
|
|
* |
95
|
|
|
* @return Validation The updated validation object with the added rules |
96
|
|
|
*/ |
97
|
|
|
public function addUnsignedIntValidation(Validation $validator, string $field = 'id', bool $allowEmpty = true): Validation |
98
|
|
|
{ |
99
|
|
|
$this->addNotEmptyValidation($validator, $field, $allowEmpty); |
100
|
|
|
|
101
|
|
|
// Must be numeric |
102
|
|
|
$validator->add($field, new Numericality([ |
103
|
|
|
'message' => $this->_('not-numeric'), |
104
|
|
|
'allowEmpty' => true, |
105
|
|
|
])); |
106
|
|
|
|
107
|
|
|
// Must be an unsigned integer |
108
|
|
|
$validator->add($field, new Between([ |
109
|
|
|
'minimum' => Column::MIN_UNSIGNED_INT, |
110
|
|
|
'maximum' => Column::MAX_UNSIGNED_INT, |
111
|
|
|
'message' => $this->_('not-an-unsigned-integer'), |
112
|
|
|
'allowEmpty' => true, |
113
|
|
|
])); |
114
|
|
|
|
115
|
|
|
return $validator; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Add basic validations for the specified field to ensure it is an unsigned big integer |
120
|
|
|
* |
121
|
|
|
* @param Validation $validator The validation object to add rules to |
122
|
|
|
* @param string $field The name of the field to validate (default is 'id') |
123
|
|
|
* @param bool $allowEmpty Whether empty values are allowed or not (default is true) |
124
|
|
|
* |
125
|
|
|
* @return Validation The updated validation object |
126
|
|
|
*/ |
127
|
|
|
public function addUnsignedBigIntValidation(Validation $validator, string $field = 'id', bool $allowEmpty = true): Validation |
128
|
|
|
{ |
129
|
|
|
$this->addNotEmptyValidation($validator, $field, $allowEmpty); |
130
|
|
|
|
131
|
|
|
// Must be numeric |
132
|
|
|
$validator->add($field, new Numericality([ |
133
|
|
|
'message' => $this->_('not-numeric'), |
134
|
|
|
'allowEmpty' => true, |
135
|
|
|
])); |
136
|
|
|
|
137
|
|
|
// Must be an unsigned integer |
138
|
|
|
$validator->add($field, new Between([ |
139
|
|
|
'minimum' => Column::MIN_UNSIGNED_BIGINT, |
140
|
|
|
'maximum' => Column::MAX_UNSIGNED_BIGINT, |
141
|
|
|
'message' => $this->_('not-an-unsigned-big-integer'), |
142
|
|
|
'allowEmpty' => true, |
143
|
|
|
])); |
144
|
|
|
|
145
|
|
|
return $validator; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Add number validations for a given field |
150
|
|
|
* |
151
|
|
|
* @param Validation $validator The validation object to add the validations to |
152
|
|
|
* @param string $field The name of the field to validate |
153
|
|
|
* @param int $min The minimum value allowed for the field |
154
|
|
|
* @param int $max The maximum value allowed for the field |
155
|
|
|
* @param bool $allowEmpty Specifies whether the field can be empty |
156
|
|
|
* |
157
|
|
|
* @return Validation The modified validation object with the number validations added |
158
|
|
|
*/ |
159
|
|
|
public function addNumberValidation(Validation $validator, string $field, int $min, int $max, bool $allowEmpty = true): Validation |
160
|
|
|
{ |
161
|
|
|
$this->addNotEmptyValidation($validator, $field, $allowEmpty); |
162
|
|
|
|
163
|
|
|
// Must be numeric |
164
|
|
|
$validator->add($field, new Numericality([ |
165
|
|
|
'message' => $this->_('not-numeric'), |
166
|
|
|
'allowEmpty' => true, |
167
|
|
|
])); |
168
|
|
|
|
169
|
|
|
// Must be an unsigned integer |
170
|
|
|
$validator->add($field, new Between([ |
171
|
|
|
'minimum' => $min, |
172
|
|
|
'maximum' => $max, |
173
|
|
|
'message' => $this->_('not-between'), |
174
|
|
|
'allowEmpty' => true, |
175
|
|
|
])); |
176
|
|
|
|
177
|
|
|
return $validator; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Add string length validations for a field |
182
|
|
|
* |
183
|
|
|
* @param Validation $validator The validation object to add the validations to |
184
|
|
|
* @param string $field The name of the field to be validated |
185
|
|
|
* @param int $minChar The minimum number of characters allowed (default: 0) |
186
|
|
|
* @param int $maxChar The maximum number of characters allowed (default: 255) |
187
|
|
|
* @param bool $allowEmpty Whether empty values are allowed (default: true) |
188
|
|
|
* |
189
|
|
|
* @return Validation The validation object with the added validations |
190
|
|
|
*/ |
191
|
|
|
public function addStringLengthValidation(Validation $validator, string $field, int $minChar = 0, int $maxChar = 255, bool $allowEmpty = true): Validation |
192
|
|
|
{ |
193
|
|
|
$this->addNotEmptyValidation($validator, $field, $allowEmpty); |
194
|
|
|
|
195
|
|
|
$validator->add($field, new Min([ |
196
|
|
|
'min' => $minChar, |
197
|
|
|
'message' => $this->_('min-length'), |
198
|
|
|
'allowEmpty' => true, |
199
|
|
|
])); |
200
|
|
|
|
201
|
|
|
$validator->add($field, new Max([ |
202
|
|
|
'max' => $maxChar, |
203
|
|
|
'message' => $this->_('max-length'), |
204
|
|
|
'allowEmpty' => true, |
205
|
|
|
])); |
206
|
|
|
|
207
|
|
|
return $validator; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Add inclusion validation for a field |
212
|
|
|
* |
213
|
|
|
* @param Validation $validator The validation object |
214
|
|
|
* @param string $field The name of the field to be validated |
215
|
|
|
* @param array $domainList The list of valid values for the field |
216
|
|
|
* @param bool $allowEmpty Set to true to allow empty values (default: true) |
217
|
|
|
* |
218
|
|
|
* @return Validation The updated validation object with the inclusion validation added |
219
|
|
|
*/ |
220
|
|
|
public function addInclusionInValidation(Validation $validator, string $field, array $domainList = [], bool $allowEmpty = true): Validation |
221
|
|
|
{ |
222
|
|
|
$this->addNotEmptyValidation($validator, $field, $allowEmpty); |
223
|
|
|
|
224
|
|
|
$validator->add($field, new InclusionIn([ |
225
|
|
|
'message' => $this->_('not-valid'), |
226
|
|
|
'domain' => $domainList, |
227
|
|
|
'allowEmpty' => true, |
228
|
|
|
])); |
229
|
|
|
|
230
|
|
|
return $validator; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Add basic validations for a boolean field |
235
|
|
|
* - Must not be empty |
236
|
|
|
* - Must be a boolean value (1, 0, true, false) |
237
|
|
|
* |
238
|
|
|
* @param Validation $validator The validation object to add the validations to |
239
|
|
|
* @param string $field The name of the field to validate |
240
|
|
|
* @param bool $allowEmpty Whether to allow empty values or not (default: true) |
241
|
|
|
* |
242
|
|
|
* @return Validation The updated validation object |
243
|
|
|
*/ |
244
|
|
|
public function addBooleanValidation(Validation $validator, string $field, bool $allowEmpty = true): Validation |
245
|
|
|
{ |
246
|
|
|
$this->addNotEmptyValidation($validator, $field, $allowEmpty); |
247
|
|
|
|
248
|
|
|
$validator->add($field, new InclusionIn([ |
249
|
|
|
'message' => $this->_('not-boolean'), |
250
|
|
|
'domain' => [1, 0, true, false], |
251
|
|
|
'allowEmpty' => true, |
252
|
|
|
])); |
253
|
|
|
|
254
|
|
|
return $validator; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Add inclusion validation for a specified field |
259
|
|
|
* |
260
|
|
|
* This method adds an inclusion validation rule to the given validator object for the specified field. |
261
|
|
|
* The inclusion rule checks if the value of the field is included in the specified domain. |
262
|
|
|
* |
263
|
|
|
* @param Validation $validator The validator object to which the rule should be added |
264
|
|
|
* @param string $field The name of the field to be validated |
265
|
|
|
* @param array $domain The array of allowed values for the field |
266
|
|
|
* @param bool $allowEmpty Whether to allow empty values for the field (default: true) |
267
|
|
|
* @param bool $strict Whether to use strict comparison for checking inclusion (default: true) |
268
|
|
|
* |
269
|
|
|
* @return Validation The updated validator object |
270
|
|
|
*/ |
271
|
|
|
public function addInclusionValidation(Validation $validator, string $field, array $domain = [], bool $allowEmpty = true, bool $strict = true): Validation |
272
|
|
|
{ |
273
|
|
|
$this->addNotEmptyValidation($validator, $field, $allowEmpty); |
274
|
|
|
|
275
|
|
|
$validator->add($field, new InclusionIn([ |
276
|
|
|
'message' => $this->_('not-valid'), |
277
|
|
|
'domain' => $domain, |
278
|
|
|
'strict' => $strict, |
279
|
|
|
'allowEmpty' => true, |
280
|
|
|
])); |
281
|
|
|
|
282
|
|
|
return $validator; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Add uniqueness validation for the specified field(s) |
287
|
|
|
* |
288
|
|
|
* @param Validation $validator The validation object to add the validation rules to |
289
|
|
|
* @param string|array $field The field(s) to apply the uniqueness validation to |
290
|
|
|
* @param bool $allowEmpty Whether to allow empty values for the field(s) |
291
|
|
|
* |
292
|
|
|
* @return Validation The modified validation object |
293
|
|
|
*/ |
294
|
|
|
public function addUniquenessValidation(Validation $validator, string|array $field, bool $allowEmpty = true): Validation |
295
|
|
|
{ |
296
|
|
|
$this->addNotEmptyValidation($validator, $field, $allowEmpty); |
|
|
|
|
297
|
|
|
|
298
|
|
|
$validator->add($field, new Uniqueness([ |
299
|
|
|
'message' => $this->_('not-unique'), |
300
|
|
|
'allowEmpty' => true, |
301
|
|
|
])); |
302
|
|
|
|
303
|
|
|
return $validator; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Add email validation to a field |
308
|
|
|
* |
309
|
|
|
* @param Validation $validator The validator object |
310
|
|
|
* @param string $field The field name to add the validation to |
311
|
|
|
* @param bool $allowEmpty Whether to allow empty values for the field (default: true) |
312
|
|
|
* |
313
|
|
|
* @return Validation The modified validator object |
314
|
|
|
*/ |
315
|
2 |
|
public function addEmailValidation(Validation $validator, string $field, bool $allowEmpty = true): Validation |
316
|
|
|
{ |
317
|
2 |
|
$this->addNotEmptyValidation($validator, $field, $allowEmpty); |
318
|
|
|
|
319
|
2 |
|
$validator->add($field, new Email([ |
320
|
2 |
|
'message' => $this->_('invalid-email'), |
321
|
2 |
|
'allowEmpty' => true, |
322
|
2 |
|
])); |
323
|
|
|
|
324
|
2 |
|
return $validator; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Add basic validations for the date field |
329
|
|
|
* - Must not be empty |
330
|
|
|
* - Must be a valid date in the specified format |
331
|
|
|
* |
332
|
|
|
* @param Validation $validator The validation object to add the validations to |
333
|
|
|
* @param string $field The name of the date field to validate |
334
|
|
|
* @param bool $allowEmpty Whether to allow empty values for the date field (default: true) |
335
|
|
|
* @param string $format The expected format of the date field (default: Column::DATE_FORMAT) |
336
|
|
|
* |
337
|
|
|
* @return Validation The updated validation object |
338
|
|
|
*/ |
339
|
|
|
public function addDateValidation(Validation $validator, string $field, bool $allowEmpty = true, string $format = Column::DATE_FORMAT): Validation |
340
|
|
|
{ |
341
|
|
|
$this->addNotEmptyValidation($validator, $field, $allowEmpty); |
342
|
|
|
|
343
|
|
|
$validator->add($field, new Date([ |
344
|
|
|
'format' => $format, |
345
|
|
|
'message' => $this->_('invalid-date-format'), |
346
|
|
|
'allowEmpty' => true, |
347
|
|
|
])); |
348
|
|
|
|
349
|
|
|
return $validator; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Add basic validations for the datetime field |
354
|
|
|
* - Must not be empty |
355
|
|
|
* - Must be a valid datetime format |
356
|
|
|
* |
357
|
|
|
* @param Validation $validator The validation object |
358
|
|
|
* @param string $field The name of the field to validate |
359
|
|
|
* @param bool $allowEmpty Specifies if the field is allowed to be empty (default: true) |
360
|
|
|
* @param string $format The format of the datetime (default: Column::DATETIME_FORMAT) |
361
|
|
|
* |
362
|
|
|
* @return Validation The updated validation object |
363
|
|
|
*/ |
364
|
|
|
public function addDateTimeValidation(Validation $validator, string $field, bool $allowEmpty = true, string $format = Column::DATETIME_FORMAT): Validation |
365
|
|
|
{ |
366
|
|
|
$this->addNotEmptyValidation($validator, $field, $allowEmpty); |
367
|
|
|
|
368
|
|
|
$validator->add($field, new Date([ |
369
|
|
|
'format' => $format, |
370
|
|
|
'message' => $this->_('invalid-date-format'), |
371
|
|
|
'allowEmpty' => true, |
372
|
|
|
])); |
373
|
|
|
|
374
|
|
|
return $validator; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Add validations for a JSON field |
379
|
|
|
* - Must not be empty (unless allowEmpty is set to true) |
380
|
|
|
* - Must be a valid JSON string |
381
|
|
|
* |
382
|
|
|
* @param Validation $validator The validator object to add the validations to |
383
|
|
|
* @param string $field The name of the JSON field to validate |
384
|
|
|
* @param bool $allowEmpty Whether to allow an empty value for the field |
385
|
|
|
* @param int $depth The maximum depth of the JSON string (default: 512) |
386
|
|
|
* @param int $flags JSON flags to be used (default: 0) |
387
|
|
|
* |
388
|
|
|
* @return Validation The updated validator object |
389
|
|
|
*/ |
390
|
|
|
public function addJsonValidation(Validation $validator, string $field, bool $allowEmpty = true, int $depth = 512, int $flags = 0): Validation |
391
|
|
|
{ |
392
|
|
|
$this->addNotEmptyValidation($validator, $field, $allowEmpty); |
393
|
|
|
|
394
|
|
|
$validator->add($field, new Json([ |
395
|
|
|
'message' => $this->_('not-valid-json'), |
396
|
|
|
'depth' => $depth, |
397
|
|
|
'flags' => $flags, |
398
|
|
|
'allowEmpty' => true, |
399
|
|
|
])); |
400
|
|
|
|
401
|
|
|
return $validator; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Add basic validations for the color field |
406
|
|
|
* - Must not be empty (unless $allowEmpty is set to true) |
407
|
|
|
* - Must be a valid hex color code |
408
|
|
|
* |
409
|
|
|
* @param Validation $validator The validation object |
410
|
|
|
* @param string $field The name of the field to validate |
411
|
|
|
* @param bool $allowEmpty Whether empty values are allowed (default: true) |
412
|
|
|
* |
413
|
|
|
* @return Validation The modified validation object |
414
|
|
|
*/ |
415
|
|
|
public function addColorValidation(Validation $validator, string $field, bool $allowEmpty = true): Validation |
416
|
|
|
{ |
417
|
|
|
$this->addNotEmptyValidation($validator, $field, $allowEmpty); |
418
|
|
|
|
419
|
|
|
$validator->add($field, new Color([ |
420
|
|
|
'message' => $this->_('not-hex-color'), |
421
|
|
|
'allowEmpty' => true, |
422
|
|
|
])); |
423
|
|
|
|
424
|
|
|
return $validator; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Add basic validations for the id field |
429
|
|
|
* - Must be an unsigned integer |
430
|
|
|
* |
431
|
|
|
* @param Validation $validator The validation object to add validation rules to |
432
|
|
|
* @param string $field The name of the field to add validations for (default: 'id') |
433
|
|
|
* |
434
|
|
|
* @return Validation The updated validation object |
435
|
|
|
*/ |
436
|
|
|
public function addIdValidation(Validation $validator, string $field = 'id'): Validation |
437
|
|
|
{ |
438
|
|
|
if (property_exists($this, $field)) { |
439
|
|
|
$this->addUnsignedIntValidation($validator, $field); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
return $validator; |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* Add position validation to a validator object. |
447
|
|
|
* |
448
|
|
|
* @param Validation $validator The validator object to add the validation rules to. |
449
|
|
|
* @param string $field The field name to apply the validation rules to. Default is 'position'. |
450
|
|
|
* @param bool $allowEmpty Whether empty values are allowed. Default is true. |
451
|
|
|
* |
452
|
|
|
* @return Validation The updated validator object with the position validation added. |
453
|
|
|
*/ |
454
|
2 |
|
public function addPositionValidation(Validation $validator, string $field = 'position', bool $allowEmpty = true): Validation |
455
|
|
|
{ |
456
|
2 |
|
if (property_exists($this, $field)) { |
457
|
|
|
|
458
|
|
|
$this->addNotEmptyValidation($validator, $field, $allowEmpty); |
459
|
|
|
|
460
|
|
|
// Must be numeric |
461
|
|
|
$validator->add($field, new Numericality([ |
462
|
|
|
'message' => $this->_('not-numeric'), |
463
|
|
|
'allowEmpty' => true, |
464
|
|
|
])); |
465
|
|
|
|
466
|
|
|
// Must be an unsigned integer |
467
|
|
|
$validator->add($field, new Between([ |
468
|
|
|
'minimum' => Column::MIN_UNSIGNED_INT, |
469
|
|
|
'maximum' => Column::MAX_UNSIGNED_INT, |
470
|
|
|
'message' => $this->_('not-an-unsigned-integer'), |
471
|
|
|
'allowEmpty' => true, |
472
|
|
|
])); |
473
|
|
|
} |
474
|
|
|
|
475
|
2 |
|
return $validator; |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
/** |
479
|
|
|
* Add soft delete validation to a validator object. |
480
|
|
|
* |
481
|
|
|
* @param Validation $validator The validator object to add the validation rules to. |
482
|
|
|
* @param string $field The field name to apply the validation rules to. Default is 'deleted'. |
483
|
|
|
* @param bool $allowEmpty Whether empty values are allowed. Default is true. |
484
|
|
|
* |
485
|
|
|
* @return Validation The updated validator object with the soft delete validation added. |
486
|
|
|
*/ |
487
|
2 |
|
public function addSoftDeleteValidation(Validation $validator, string $field = 'deleted', bool $allowEmpty = true): Validation |
488
|
|
|
{ |
489
|
2 |
|
if (property_exists($this, $field)) { |
490
|
|
|
|
491
|
2 |
|
$this->addNotEmptyValidation($validator, $field, $allowEmpty); |
492
|
|
|
|
493
|
|
|
// Must be YES or NO |
494
|
2 |
|
$validator->add($field, new InclusionIn([ |
495
|
2 |
|
'message' => $this->_('not-valid'), |
496
|
2 |
|
'domain' => [Column::YES, Column::NO], |
497
|
2 |
|
'strict' => true, |
498
|
2 |
|
])); |
499
|
|
|
|
500
|
|
|
// Must be numeric |
501
|
2 |
|
$validator->add($field, new Numericality([ |
502
|
2 |
|
'message' => $this->_('not-numeric'), |
503
|
2 |
|
'allowEmpty' => true, |
504
|
2 |
|
])); |
505
|
|
|
} |
506
|
|
|
|
507
|
2 |
|
return $validator; |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
/** |
511
|
|
|
* Add UUID validation to a validator object. |
512
|
|
|
* |
513
|
|
|
* @param Validation $validator The validator object to add the validation rules to. |
514
|
|
|
* @param string $field The field name to apply the validation rules to. Default is 'uuid'. |
515
|
|
|
* @param bool $allowEmpty Whether empty values are allowed. Default is false. |
516
|
|
|
* |
517
|
|
|
* @return Validation The updated validator object with the UUID validation added. |
518
|
|
|
*/ |
519
|
|
|
public function addUuidValidation(Validation $validator, string $field = 'uuid', bool $allowEmpty = false): Validation |
520
|
|
|
{ |
521
|
|
|
if (property_exists($this, $field) && $this->getModelsMetaData()->hasAttribute($this, $field)) { |
|
|
|
|
522
|
|
|
|
523
|
|
|
$this->addNotEmptyValidation($validator, $field, $allowEmpty); |
524
|
|
|
|
525
|
|
|
// Must be unique |
526
|
|
|
$validator->add($field, new Uniqueness([ |
527
|
|
|
'message' => $this->_('not-unique'), |
528
|
|
|
'allowEmpty' => true, |
529
|
|
|
])); |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
return $validator; |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
/** |
536
|
|
|
* Add CRUD validation to a validator object. |
537
|
|
|
* |
538
|
|
|
* @param Validation $validator The validator object to add the validation rules to. |
539
|
|
|
* @param string $userIdField The field name for the user ID validation rules. |
540
|
|
|
* @param string $dateField The field name for the date validation rules. |
541
|
|
|
* @param bool $allowEmpty Whether empty values are allowed. Default is true. |
542
|
|
|
* |
543
|
|
|
* @return Validation The updated validator object with the CRUD validation added. |
544
|
|
|
*/ |
545
|
2 |
|
public function addCrudValidation(Validation $validator, string $userIdField, string $dateField, bool $allowEmpty = true): Validation |
546
|
|
|
{ |
547
|
2 |
|
if (property_exists($this, $userIdField)) { |
548
|
|
|
|
549
|
2 |
|
$this->addNotEmptyValidation($validator, $userIdField, $allowEmpty); |
550
|
|
|
|
551
|
|
|
// Must be numeric |
552
|
2 |
|
$validator->add($userIdField, new Numericality([ |
553
|
2 |
|
'message' => $this->_('not-numeric'), |
554
|
2 |
|
'allowEmpty' => true, |
555
|
2 |
|
])); |
556
|
|
|
|
557
|
|
|
// Must be an unsigned integer |
558
|
2 |
|
$validator->add($userIdField, new Between([ |
559
|
2 |
|
'minimum' => Column::MIN_UNSIGNED_INT, |
560
|
2 |
|
'maximum' => Column::MAX_UNSIGNED_INT, |
561
|
2 |
|
'message' => $this->_('not-an-unsigned-integer'), |
562
|
2 |
|
'allowEmpty' => true, |
563
|
2 |
|
])); |
564
|
|
|
} |
565
|
|
|
|
566
|
2 |
|
if (property_exists($this, $dateField)) { |
567
|
|
|
|
568
|
|
|
// if the $userIdField is filled |
569
|
2 |
|
if (!empty($this->readAttribute($userIdField))) { |
|
|
|
|
570
|
|
|
|
571
|
|
|
$this->addPresenceValidation($validator, $dateField, false); |
572
|
|
|
|
573
|
|
|
// Must be a valid date format |
574
|
|
|
$validator->add($dateField, new Date([ |
575
|
|
|
'format' => Column::DATETIME_FORMAT, |
576
|
|
|
'message' => $this->_('invalid-date-format'), |
577
|
|
|
'allowEmpty' => true, |
578
|
|
|
])); |
579
|
|
|
} |
580
|
|
|
} |
581
|
|
|
|
582
|
2 |
|
return $validator; |
583
|
|
|
} |
584
|
|
|
|
585
|
|
|
/** |
586
|
|
|
* Add created validation to a validator object. |
587
|
|
|
* |
588
|
|
|
* @param Validation $validator The validator object to add the validation rules to. |
589
|
|
|
* @param string $createdByField The field name to apply the validation rules for the "created by" user. Default is 'createdBy'. |
590
|
|
|
* @param string $createdAtField The field name to apply the validation rules for the "created at" timestamp. Default is 'createdAt'. |
591
|
|
|
* @param bool $allowEmpty Whether empty values are allowed. Default is true. |
592
|
|
|
* |
593
|
|
|
* @return Validation The updated validator object with the created validation added. |
594
|
|
|
*/ |
595
|
2 |
|
public function addCreatedValidation(Validation $validator, string $createdByField = 'createdBy', string $createdAtField = 'createdAt', bool $allowEmpty = true): Validation |
596
|
|
|
{ |
597
|
2 |
|
return $this->addCrudValidation($validator, $createdByField, $createdAtField, $allowEmpty); |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
/** |
601
|
|
|
* Add updated validation to a validator object. |
602
|
|
|
* |
603
|
|
|
* @param Validation $validator The validator object to add the validation rules to. |
604
|
|
|
* @param string $updatedByField The field name to apply the updated by validation rule to. Default is 'updatedBy'. |
605
|
|
|
* @param string $updatedAtField The field name to apply the updated at validation rule to. Default is 'updatedAt'. |
606
|
|
|
* @param bool $allowEmpty Whether empty values are allowed. Default is true. |
607
|
|
|
* |
608
|
|
|
* @return Validation The updated validator object with the updated validation added. |
609
|
|
|
*/ |
610
|
2 |
|
public function addUpdatedValidation(Validation $validator, string $updatedByField = 'updatedBy', string $updatedAtField = 'updatedAt', bool $allowEmpty = true): Validation |
611
|
|
|
{ |
612
|
2 |
|
return $this->addCrudValidation($validator, $updatedByField, $updatedAtField, $allowEmpty); |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
/** |
616
|
|
|
* Add deleted validation to a validator object. |
617
|
|
|
* |
618
|
|
|
* @param Validation $validator The validator object to add the validation rules to. |
619
|
|
|
* @param string $deletedField The field name to apply the validation rules to for deleted user. Default is 'deletedBy'. |
620
|
|
|
* @param string $dateField The field name to apply the validation rules to for deletion date. Default is 'deletedAt'. |
621
|
|
|
* @param bool $allowEmpty Whether empty values are allowed. Default is true. |
622
|
|
|
* |
623
|
|
|
* @return Validation The updated validator object with the deleted validation added. |
624
|
|
|
*/ |
625
|
2 |
|
public function addDeletedValidation(Validation $validator, string $deletedField = 'deletedBy', string $dateField = 'deletedAt', bool $allowEmpty = true): Validation |
626
|
|
|
{ |
627
|
2 |
|
return $this->addCrudValidation($validator, $deletedField, $dateField, $allowEmpty); |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
/** |
631
|
|
|
* Add restored validation to a validator object. |
632
|
|
|
* |
633
|
|
|
* @param Validation $validator The validator object to add the validation rules to. |
634
|
|
|
* @param string $restoredByField The field name for the restored by information. Default is 'restoredBy'. |
635
|
|
|
* @param string $restoredAtField The field name for the restored at information. Default is 'restoredAt'. |
636
|
|
|
* @param bool $allowEmpty Whether empty values are allowed. Default is true. |
637
|
|
|
* |
638
|
|
|
* @return Validation The updated validator object with the restored validation added. |
639
|
|
|
*/ |
640
|
2 |
|
public function addRestoredValidation(Validation $validator, string $restoredByField = 'restoredBy', string $restoredAtField = 'restoredAt', bool $allowEmpty = true): Validation |
641
|
|
|
{ |
642
|
2 |
|
return $this->addCrudValidation($validator, $restoredByField, $restoredAtField, $allowEmpty); |
643
|
|
|
} |
644
|
|
|
} |
645
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: