|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: siim |
|
5
|
|
|
* Date: 10.02.19 |
|
6
|
|
|
* Time: 16:55 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Sf4\Api\EntityValidator; |
|
10
|
|
|
|
|
11
|
|
|
use Sf4\Api\Entity\EntityInterface; |
|
12
|
|
|
use Sf4\Api\Notification\NotificationInterface; |
|
13
|
|
|
use Sf4\Api\Notification\Traits\ConvertViolationsToErrorMessage; |
|
14
|
|
|
use Sf4\Api\Utils\Traits\TranslatorTrait; |
|
15
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
16
|
|
|
|
|
17
|
|
|
abstract class AbstractEntityValidator |
|
18
|
|
|
{ |
|
19
|
|
|
use TranslatorTrait; |
|
20
|
|
|
use ConvertViolationsToErrorMessage; |
|
21
|
|
|
|
|
22
|
|
|
public const VALUE = 'value'; |
|
23
|
|
|
public const CONSTRAINTS = 'constraints'; |
|
24
|
|
|
public const MIN = 'min'; |
|
25
|
|
|
public const PATTERN = 'pattern'; |
|
26
|
|
|
|
|
27
|
|
|
public const OPTION_MESSAGE = 'message'; |
|
28
|
|
|
public const OPTION_MIN_MESSAGE = 'minMessage'; |
|
29
|
|
|
|
|
30
|
|
|
/* |
|
31
|
|
|
* \p{L} - single character |
|
32
|
|
|
* \p{Ll} - a lowercase letter |
|
33
|
|
|
* \p{Lu} - an uppercase letter |
|
34
|
|
|
* \p{Zs} - any kind of space character |
|
35
|
|
|
* \p{N} - any kind of numeric character |
|
36
|
|
|
* \X - line break characters, |
|
37
|
|
|
* \p{P} - any kind of punctuation character |
|
38
|
|
|
*/ |
|
39
|
|
|
public const NAME_REGEX = '/^(\p{Lu})([\p{L}\p{Zs}-]+)(\p{Ll})$/um'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param EntityInterface $entity |
|
43
|
|
|
* @return array |
|
44
|
|
|
*/ |
|
45
|
|
|
abstract protected function getValidationRules(EntityInterface $entity): array; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param EntityInterface $entity |
|
49
|
|
|
* @param ValidatorInterface $validator |
|
50
|
|
|
* @param NotificationInterface $notification |
|
51
|
|
|
*/ |
|
52
|
|
|
public function validate( |
|
53
|
|
|
EntityInterface $entity, |
|
54
|
|
|
ValidatorInterface $validator, |
|
55
|
|
|
NotificationInterface $notification |
|
56
|
|
|
): void { |
|
57
|
|
|
$rules = $this->getValidationRules($entity); |
|
58
|
|
|
foreach ($rules as $fieldName => $rule) { |
|
59
|
|
|
$this->validateField($validator, $notification, $fieldName, $rule); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param ValidatorInterface $validator |
|
65
|
|
|
* @param NotificationInterface $notification |
|
66
|
|
|
* @param string $fieldName |
|
67
|
|
|
* @param array $rule |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function validateField( |
|
70
|
|
|
ValidatorInterface $validator, |
|
71
|
|
|
NotificationInterface $notification, |
|
72
|
|
|
string $fieldName, |
|
73
|
|
|
array $rule |
|
74
|
|
|
): void { |
|
75
|
|
|
$this->convertViolationsToErrormessage( |
|
76
|
|
|
$validator->validate( |
|
77
|
|
|
$rule[static::VALUE], |
|
78
|
|
|
$rule[static::CONSTRAINTS] |
|
79
|
|
|
), |
|
80
|
|
|
$notification, |
|
81
|
|
|
$fieldName |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|