1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace AmmitPhp\Ammit\UI\Resolver\Validator; |
5
|
|
|
|
6
|
|
|
use AmmitPhp\Ammit\UI\Resolver\Validator\Implementation\Pragmatic\StringBetweenLengthValidatorTrait; |
7
|
|
|
use AmmitPhp\Ammit\Domain\MailMxValidation; |
8
|
|
|
use AmmitPhp\Ammit\UI\Resolver\Validator\Implementation\Pragmatic\InArrayValidatorTrait; |
9
|
|
|
use AmmitPhp\Ammit\UI\Resolver\Validator\Implementation\Pragmatic\UuidValidatorTrait; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @internal Contains Domain Validation assertions (but class won't be removed in next version) |
13
|
|
|
* Domain Validation should be done in Domain |
14
|
|
|
* Should be used for prototyping project knowing you are accumulating technical debt |
15
|
|
|
*/ |
16
|
|
|
class PragmaticRawValueValidator extends RawValueValidator |
17
|
|
|
{ |
18
|
|
|
use UuidValidatorTrait; |
19
|
|
|
use InArrayValidatorTrait; |
20
|
|
|
use StringBetweenLengthValidatorTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Domain should be responsible for string emptiness |
24
|
|
|
* Exceptions are caught in order to be processed later |
25
|
|
|
* @param mixed $value String not empty ? |
26
|
|
|
* |
27
|
|
|
* @return mixed Untouched value |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
public function mustBeStringNotEmpty($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) |
|
|
|
|
30
|
|
|
{ |
31
|
1 |
|
$this->validationEngine->validateFieldValue( |
32
|
1 |
|
$parentValidator ?: $this, |
33
|
1 |
|
function() use ($value, $propertyPath, $exceptionMessage) { |
34
|
1 |
|
if (!empty($value)) { |
35
|
1 |
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
if (null === $exceptionMessage) { |
39
|
|
|
$exceptionMessage = sprintf( |
|
|
|
|
40
|
|
|
'Value "%s" is empty.', |
41
|
|
|
$value |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
throw new InvalidArgumentException( |
46
|
|
|
$exceptionMessage, |
47
|
1 |
|
0, |
48
|
|
|
$propertyPath, |
49
|
|
|
$value |
50
|
|
|
); |
51
|
1 |
|
} |
52
|
|
|
); |
53
|
|
|
|
54
|
1 |
|
return $value; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Domain should be responsible for email format |
59
|
|
|
* Exceptions are caught in order to be processed later |
60
|
|
|
* @param mixed $value Email ? |
61
|
|
|
* |
62
|
|
|
* @return mixed Untouched value |
63
|
|
|
*/ |
64
|
|
|
public function mustBeEmailAddress($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) |
65
|
|
|
{ |
66
|
|
|
$mailMxValidation = new MailMxValidation(); |
67
|
|
|
$this->validationEngine->validateFieldValue( |
68
|
|
|
$parentValidator ?: $this, |
69
|
|
View Code Duplication |
function() use ($value, $propertyPath, $exceptionMessage, $mailMxValidation) { |
|
|
|
|
70
|
|
|
if ($mailMxValidation->isEmailFormatValid($value) && $mailMxValidation->isEmailHostValid($value)) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (null === $exceptionMessage) { |
75
|
|
|
$exceptionMessage = sprintf( |
|
|
|
|
76
|
|
|
'Mail "%s" is not valid.', |
77
|
|
|
$value |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
throw new InvalidArgumentException( |
82
|
|
|
$exceptionMessage, |
83
|
|
|
0, |
84
|
|
|
$propertyPath, |
85
|
|
|
$value |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
return $value; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Domain should be responsible for regex validation |
95
|
|
|
* Exceptions are caught in order to be processed later |
96
|
|
|
* @param mixed $value Valid against Regex ? |
97
|
|
|
* @param string $pattern Regex |
98
|
|
|
* |
99
|
|
|
* @return mixed Untouched value |
100
|
|
|
*/ |
101
|
|
View Code Duplication |
public function mustBeValidAgainstRegex($value, string $pattern, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) |
|
|
|
|
102
|
|
|
{ |
103
|
1 |
|
$this->validationEngine->validateFieldValue( |
104
|
1 |
|
$parentValidator ?: $this, |
105
|
1 |
|
function() use ($value, $pattern, $propertyPath, $exceptionMessage) { |
106
|
1 |
|
if (preg_match($pattern, $value)) { |
107
|
1 |
|
return; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
throw new InvalidArgumentException( |
111
|
1 |
|
$exceptionMessage ?: sprintf('Value "%s" not valid against regex "%s".', $value, $pattern), |
112
|
1 |
|
0, |
113
|
|
|
$propertyPath, |
114
|
|
|
$value |
115
|
|
|
); |
116
|
1 |
|
} |
117
|
|
|
); |
118
|
|
|
|
119
|
1 |
|
return $value; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.