|
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
|
|
|
namespace Respect\Validation; |
|
13
|
|
|
|
|
14
|
|
|
use ReflectionClass; |
|
15
|
|
|
use Respect\Validation\Exceptions\ComponentException; |
|
16
|
|
|
use Respect\Validation\Exceptions\InvalidRuleException; |
|
17
|
|
|
use Respect\Validation\Exceptions\RuleNotFoundException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Factory to create rules. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Henrique Moody <[email protected]> |
|
23
|
|
|
* |
|
24
|
|
|
* @since 0.8.0 |
|
25
|
|
|
*/ |
|
26
|
|
|
final class Factory |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var string[] |
|
30
|
|
|
*/ |
|
31
|
|
|
private $namespaces; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var self |
|
35
|
|
|
*/ |
|
36
|
|
|
private static $defaultInstance; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Initializes the rule with the defined namespaces. |
|
40
|
|
|
* |
|
41
|
|
|
* If the default namespace is not in the array, it will be add to the end |
|
42
|
|
|
* of the array. |
|
43
|
|
|
* |
|
44
|
|
|
* @param array $namespaces |
|
45
|
|
|
*/ |
|
46
|
11 |
|
public function __construct(array $namespaces = []) |
|
47
|
|
|
{ |
|
48
|
11 |
|
if (!in_array(__NAMESPACE__, $namespaces)) { |
|
49
|
9 |
|
$namespaces[] = __NAMESPACE__; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
11 |
|
$this->namespaces = $namespaces; |
|
53
|
11 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Returns a list of namespaces. |
|
57
|
|
|
* |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
9 |
|
public function getNamespaces(): array |
|
61
|
|
|
{ |
|
62
|
9 |
|
return $this->namespaces; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Creates a rule based on its name with the defined arguments. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $ruleName |
|
69
|
|
|
* @param array $arguments |
|
70
|
|
|
* |
|
71
|
|
|
* @throws ComponentException When the rule cannot be created |
|
72
|
|
|
* |
|
73
|
|
|
* @return Rule |
|
74
|
|
|
*/ |
|
75
|
6 |
|
public function rule(string $ruleName, array $arguments = []): Rule |
|
76
|
|
|
{ |
|
77
|
6 |
|
foreach ($this->getNamespaces() as $namespace) { |
|
78
|
6 |
|
$className = rtrim($namespace, '\\').'\\Rules\\'.ucfirst($ruleName); |
|
79
|
6 |
|
if (!class_exists($className)) { |
|
80
|
1 |
|
continue; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
5 |
|
$reflection = new ReflectionClass($className); |
|
84
|
5 |
|
if (!$reflection->isSubclassOf(Rule::class)) { |
|
85
|
1 |
|
throw new InvalidRuleException(sprintf('"%s" is not a valid rule', $className)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
4 |
|
if (!$reflection->isInstantiable()) { |
|
89
|
1 |
|
throw new InvalidRuleException(sprintf('"%s" is not instantiable', $className)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
3 |
|
return $reflection->newInstanceArgs($arguments); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
throw new RuleNotFoundException(sprintf('Could not find "%s" rule', $ruleName)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Defines the default instance of the factory. |
|
100
|
|
|
* |
|
101
|
|
|
* @param Factory $factory |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public static function setDefaultInstance(self $factory) |
|
104
|
|
|
{ |
|
105
|
1 |
|
self::$defaultInstance = $factory; |
|
|
|
|
|
|
106
|
1 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Returns the default instance of the factory. |
|
110
|
|
|
* |
|
111
|
|
|
* @return self |
|
112
|
|
|
*/ |
|
113
|
2 |
|
public static function getDefaultInstance(): self |
|
114
|
|
|
{ |
|
115
|
2 |
|
if (!self::$defaultInstance instanceof self) { |
|
116
|
1 |
|
self::$defaultInstance = new self(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
2 |
|
return self::$defaultInstance; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
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..