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