|
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 function Respect\Stringifier\stringify; |
|
17
|
|
|
use ReflectionClass; |
|
18
|
|
|
use ReflectionObject; |
|
19
|
|
|
use Respect\Validation\Exceptions\ComponentException; |
|
20
|
|
|
use Respect\Validation\Exceptions\ValidationException; |
|
21
|
|
|
|
|
22
|
|
|
class Factory |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Default instance of the Factory. |
|
26
|
|
|
* |
|
27
|
|
|
* @var Factory |
|
28
|
|
|
*/ |
|
29
|
|
|
protected static $defaultInstance; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string[] |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $rulePrefixes = ['Respect\\Validation\\Rules\\']; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Define the default instance of the Factory. |
|
38
|
|
|
* |
|
39
|
|
|
* @param Factory $defaultInstance |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function setDefaultInstance(self $defaultInstance): void |
|
42
|
|
|
{ |
|
43
|
|
|
self::$defaultInstance = $defaultInstance; |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Returns the default instance of the Factory. |
|
48
|
|
|
* |
|
49
|
|
|
* @return Factory |
|
50
|
|
|
*/ |
|
51
|
5 |
|
public static function getDefaultInstance(): self |
|
52
|
|
|
{ |
|
53
|
5 |
|
if (!self::$defaultInstance instanceof self) { |
|
54
|
|
|
self::$defaultInstance = new static(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
5 |
|
return self::$defaultInstance; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return array |
|
62
|
|
|
*/ |
|
63
|
11 |
|
public function getRulePrefixes(): array |
|
64
|
|
|
{ |
|
65
|
11 |
|
return $this->rulePrefixes; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
5 |
|
private function filterRulePrefix(string $rulePrefix): string |
|
69
|
|
|
{ |
|
70
|
5 |
|
$namespaceSeparator = '\\'; |
|
71
|
5 |
|
$rulePrefix = rtrim($rulePrefix, $namespaceSeparator); |
|
72
|
|
|
|
|
73
|
5 |
|
return $rulePrefix.$namespaceSeparator; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
3 |
|
public function appendRulePrefix(string $rulePrefix): void |
|
77
|
|
|
{ |
|
78
|
3 |
|
array_push($this->rulePrefixes, $this->filterRulePrefix($rulePrefix)); |
|
79
|
3 |
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
public function prependRulePrefix(string $rulePrefix): void |
|
82
|
|
|
{ |
|
83
|
2 |
|
array_unshift($this->rulePrefixes, $this->filterRulePrefix($rulePrefix)); |
|
84
|
2 |
|
} |
|
85
|
|
|
|
|
86
|
6 |
|
public function rule(string $ruleName, array $arguments = []): Validatable |
|
87
|
|
|
{ |
|
88
|
6 |
|
foreach ($this->getRulePrefixes() as $prefix) { |
|
89
|
6 |
|
$className = $prefix.ucfirst($ruleName); |
|
90
|
6 |
|
if (!class_exists($className)) { |
|
91
|
3 |
|
continue; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
4 |
|
$reflection = new ReflectionClass($className); |
|
95
|
4 |
|
if (!$reflection->isSubclassOf(Validatable::class)) { |
|
96
|
1 |
|
throw new ComponentException(sprintf('"%s" is not a valid respect rule', $className)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
3 |
|
return $reflection->newInstanceArgs($arguments); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
2 |
|
throw new ComponentException(sprintf('"%s" is not a valid rule name', $ruleName)); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
4 |
|
public function exception(Validatable $validatable, $input, array $extraParams = []): ValidationException |
|
106
|
|
|
{ |
|
107
|
4 |
|
$reflection = new ReflectionObject($validatable); |
|
108
|
|
|
|
|
109
|
4 |
|
$exceptionName = str_replace('\\Rules\\', '\\Exceptions\\', $reflection->getName()).'Exception'; |
|
|
|
|
|
|
110
|
4 |
|
$exception = new $exceptionName(); |
|
111
|
|
|
|
|
112
|
4 |
|
$params = ['input' => $input] + $extraParams; |
|
113
|
4 |
|
foreach ($reflection->getProperties() as $property) { |
|
114
|
4 |
|
$property->setAccessible(true); |
|
115
|
4 |
|
$params += [$property->getName() => $property->getValue($validatable)]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
4 |
|
$name = $validatable->getName() ?: stringify($input); |
|
119
|
4 |
|
$exception->configure($name, $params); |
|
120
|
4 |
|
if (isset($params['template'])) { |
|
121
|
|
|
$exception->setTemplate($params['template']); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
4 |
|
return $exception; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..