|
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
|
|
|
|
|
17
|
|
|
class Factory |
|
18
|
|
|
{ |
|
19
|
|
|
protected $rulePrefixes = ['Respect\\Validation\\Rules\\']; |
|
20
|
|
|
|
|
21
|
11 |
|
public function getRulePrefixes() |
|
22
|
|
|
{ |
|
23
|
11 |
|
return $this->rulePrefixes; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
5 |
|
private function filterRulePrefix($rulePrefix) |
|
27
|
|
|
{ |
|
28
|
5 |
|
$namespaceSeparator = '\\'; |
|
29
|
5 |
|
$rulePrefix = rtrim($rulePrefix, $namespaceSeparator); |
|
30
|
|
|
|
|
31
|
5 |
|
return $rulePrefix.$namespaceSeparator; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
3 |
|
public function appendRulePrefix($rulePrefix) |
|
35
|
|
|
{ |
|
36
|
3 |
|
array_push($this->rulePrefixes, $this->filterRulePrefix($rulePrefix)); |
|
37
|
3 |
|
} |
|
38
|
|
|
|
|
39
|
2 |
|
public function prependRulePrefix($rulePrefix) |
|
40
|
|
|
{ |
|
41
|
2 |
|
array_unshift($this->rulePrefixes, $this->filterRulePrefix($rulePrefix)); |
|
42
|
2 |
|
} |
|
43
|
|
|
|
|
44
|
6 |
|
public function rule($ruleName, array $arguments = []) |
|
45
|
|
|
{ |
|
46
|
6 |
|
if ($ruleName instanceof Validatable) { |
|
47
|
|
|
return $ruleName; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
6 |
|
foreach ($this->getRulePrefixes() as $prefix) { |
|
51
|
6 |
|
$className = $prefix.ucfirst($ruleName); |
|
52
|
6 |
|
if (!class_exists($className)) { |
|
53
|
3 |
|
continue; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
4 |
|
$reflection = new ReflectionClass($className); |
|
57
|
4 |
|
if (!$reflection->isSubclassOf('Respect\\Validation\\Validatable')) { |
|
58
|
1 |
|
throw new ComponentException(sprintf('"%s" is not a valid respect rule', $className)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
3 |
|
return $reflection->newInstanceArgs($arguments); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
throw new ComponentException(sprintf('"%s" is not a valid rule name', $ruleName)); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|