1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Respect/Validation. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandre Gomes Gaigalas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the "LICENSE.md" |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Respect\Validation; |
15
|
|
|
|
16
|
|
|
use ReflectionClass; |
17
|
|
|
use ReflectionObject; |
18
|
|
|
use Respect\Validation\Exceptions\ComponentException; |
19
|
|
|
use Respect\Validation\Exceptions\InvalidClassException; |
20
|
|
|
use Respect\Validation\Exceptions\ValidationException; |
21
|
|
|
use function array_map; |
22
|
|
|
use function array_merge; |
23
|
|
|
use function array_unique; |
24
|
|
|
use function class_exists; |
25
|
|
|
use function lcfirst; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Factory of objects. |
29
|
|
|
* |
30
|
|
|
* @author Henrique Moody <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
final class Factory |
33
|
|
|
{ |
34
|
|
|
private const DEFAULT_RULES_NAMESPACES = [ |
35
|
|
|
'Respect\\Validation\\Rules', |
36
|
|
|
'Respect\\Validation\\Rules\\Locale', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
private const DEFAULT_EXCEPTIONS_NAMESPACES = [ |
40
|
|
|
'Respect\\Validation\\Exceptions', |
41
|
|
|
'Respect\\Validation\\Exceptions\\Locale', |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Default instance of the Factory. |
46
|
|
|
* |
47
|
|
|
* @var Factory |
48
|
|
|
*/ |
49
|
|
|
private static $defaultInstance; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string[] |
53
|
|
|
*/ |
54
|
|
|
private $rulesNamespaces = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string[] |
58
|
|
|
*/ |
59
|
|
|
private $exceptionsNamespaces = []; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var callable |
63
|
|
|
*/ |
64
|
|
|
private $translator; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Initializes the factory with the defined namespaces. |
68
|
|
|
* |
69
|
|
|
* If the default namespace is not in the array, it will be add to the end |
70
|
|
|
* of the array. |
71
|
|
|
* |
72
|
|
|
* @param string[] $rulesNamespaces |
73
|
|
|
* @param string[] $exceptionsNamespaces |
74
|
|
|
*/ |
75
|
|
|
public function __construct( |
76
|
|
|
array $rulesNamespaces, |
77
|
|
|
array $exceptionsNamespaces, |
78
|
|
|
callable $translator |
79
|
|
|
) { |
80
|
|
|
$this->rulesNamespaces = $this->filterNamespaces($rulesNamespaces, self::DEFAULT_RULES_NAMESPACES); |
81
|
|
|
$this->exceptionsNamespaces = $this->filterNamespaces($exceptionsNamespaces, self::DEFAULT_EXCEPTIONS_NAMESPACES); |
82
|
|
|
$this->translator = $translator; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Define the default instance of the Factory. |
87
|
|
|
* |
88
|
|
|
* @param Factory $defaultInstance |
89
|
|
|
*/ |
90
|
|
|
public static function setDefaultInstance(self $defaultInstance): void |
91
|
|
|
{ |
92
|
|
|
self::$defaultInstance = $defaultInstance; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns the default instance of the Factory. |
97
|
|
|
* |
98
|
|
|
* @return Factory |
99
|
|
|
*/ |
100
|
2 |
|
public static function getDefaultInstance(): self |
101
|
|
|
{ |
102
|
2 |
|
if (null === self::$defaultInstance) { |
103
|
|
|
self::$defaultInstance = new self( |
104
|
|
|
self::DEFAULT_RULES_NAMESPACES, |
105
|
|
|
self::DEFAULT_EXCEPTIONS_NAMESPACES, |
106
|
|
|
function (string $message): string { |
107
|
|
|
return $message; |
108
|
|
|
} |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
return self::$defaultInstance; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Creates a rule. |
117
|
|
|
* |
118
|
|
|
* @param string $ruleName |
119
|
|
|
* @param array $arguments |
120
|
|
|
* |
121
|
|
|
* @throws ComponentException |
122
|
|
|
* |
123
|
|
|
* @return Validatable |
124
|
|
|
*/ |
125
|
2 |
|
public function rule(string $ruleName, array $arguments = []): Validatable |
126
|
|
|
{ |
127
|
2 |
|
foreach ($this->rulesNamespaces as $namespace) { |
128
|
2 |
|
$className = sprintf('%s\\%s', $namespace, ucfirst($ruleName)); |
129
|
2 |
|
if (!class_exists($className)) { |
130
|
1 |
|
continue; |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
return $this->createReflectionClass($className, Validatable::class)->newInstanceArgs($arguments); |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
throw new ComponentException(sprintf('"%s" is not a valid rule name', $ruleName)); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Creates an exception. |
141
|
|
|
* |
142
|
|
|
* |
143
|
|
|
* @param Validatable $validatable |
144
|
|
|
* @param mixed $input |
145
|
|
|
* @param array $extraParams |
146
|
|
|
* |
147
|
|
|
* @throws ComponentException |
148
|
|
|
* |
149
|
|
|
* @return ValidationException |
150
|
|
|
*/ |
151
|
|
|
public function exception(Validatable $validatable, $input, array $extraParams = []): ValidationException |
152
|
|
|
{ |
153
|
|
|
$reflection = new ReflectionObject($validatable); |
154
|
|
|
$ruleName = $reflection->getShortName(); |
155
|
|
|
$params = ['input' => $input] + $extraParams + $this->extractPropertiesValues($validatable, $reflection); |
156
|
|
|
$id = lcfirst($ruleName); |
157
|
|
|
if ($validatable->getName()) { |
158
|
|
|
$id = $params['name'] = $validatable->getName(); |
159
|
|
|
} |
160
|
|
|
foreach ($this->exceptionsNamespaces as $namespace) { |
161
|
|
|
$exceptionName = sprintf('%s\\%sException', $namespace, $ruleName); |
162
|
|
|
if (!class_exists($exceptionName)) { |
163
|
|
|
continue; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $this->createValidationException($exceptionName, $id, $input, $params); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $this->createValidationException(ValidationException::class, $id, $input, $params); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Creates a reflection based on class name. |
174
|
|
|
* |
175
|
|
|
* |
176
|
|
|
* @param string $name |
177
|
|
|
* @param string $parentName |
178
|
|
|
* |
179
|
|
|
* @throws InvalidClassException |
180
|
|
|
* |
181
|
|
|
* @return ReflectionClass |
182
|
|
|
*/ |
183
|
1 |
|
private function createReflectionClass(string $name, string $parentName): ReflectionClass |
184
|
|
|
{ |
185
|
1 |
|
$reflection = new ReflectionClass($name); |
186
|
1 |
|
if (!$reflection->isSubclassOf($parentName) && $parentName !== $name) { |
187
|
|
|
throw new InvalidClassException(sprintf('"%s" must be an instance of "%s"', $name, $parentName)); |
188
|
|
|
} |
189
|
|
|
|
190
|
1 |
|
if (!$reflection->isInstantiable()) { |
191
|
|
|
throw new InvalidClassException(sprintf('"%s" must be instantiable', $name)); |
192
|
|
|
} |
193
|
|
|
|
194
|
1 |
|
return $reflection; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Filters namespaces. |
199
|
|
|
* |
200
|
|
|
* Ensure namespaces are in the right format and contain the default namespaces. |
201
|
|
|
* |
202
|
|
|
* @param array $namespaces |
203
|
|
|
* @param array $defaultNamespaces |
204
|
|
|
* |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
|
|
private function filterNamespaces(array $namespaces, array $defaultNamespaces): array |
208
|
|
|
{ |
209
|
|
|
$filter = function (string $namespace): string { |
210
|
|
|
return trim($namespace, '\\'); |
211
|
|
|
}; |
212
|
|
|
|
213
|
|
|
return array_unique( |
214
|
|
|
array_merge( |
215
|
|
|
array_map($filter, $namespaces), |
216
|
|
|
array_map($filter, $defaultNamespaces) |
217
|
|
|
) |
218
|
|
|
); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Creates a Validation exception. |
223
|
|
|
* |
224
|
|
|
* @param string $exceptionName |
225
|
|
|
* @param string $id |
226
|
|
|
* @param mixed $input |
227
|
|
|
* @param array $params |
228
|
|
|
* |
229
|
|
|
* @throws InvalidClassException |
230
|
|
|
* |
231
|
|
|
* @return ValidationException |
232
|
|
|
*/ |
233
|
|
|
private function createValidationException(string $exceptionName, string $id, $input, array $params): ValidationException |
234
|
|
|
{ |
235
|
|
|
/* @var ValidationException $exception */ |
236
|
|
|
$exception = $this->createReflectionClass($exceptionName, ValidationException::class) |
237
|
|
|
->newInstance($input, $id, $params, $this->translator); |
238
|
|
|
if (isset($params['template'])) { |
239
|
|
|
$exception->updateTemplate($params['template']); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return $exception; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @param Validatable $validatable |
247
|
|
|
* @param ReflectionClass $reflection |
248
|
|
|
* |
249
|
|
|
* @return array |
250
|
|
|
*/ |
251
|
|
|
private function extractPropertiesValues(Validatable $validatable, ReflectionClass $reflection): array |
252
|
|
|
{ |
253
|
|
|
$values = []; |
254
|
|
|
foreach ($reflection->getProperties() as $property) { |
255
|
|
|
$property->setAccessible(true); |
256
|
|
|
|
257
|
|
|
$values[$property->getName()] = $property->getValue($validatable); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
if (($parentReflection = $reflection->getParentClass())) { |
261
|
|
|
return $values + $this->extractPropertiesValues($validatable, $parentReflection); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return $values; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|