|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
|
4
|
|
|
* Date: 12/29/14 |
|
5
|
|
|
* Time: 9:53 PM |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace NilPortugues\Validator\Factory; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class ValidatorRule |
|
15
|
|
|
* @package NilPortugues\Validator\Factory |
|
16
|
|
|
*/ |
|
17
|
|
|
class ValidatorRule |
|
18
|
|
|
{ |
|
19
|
|
|
protected static $rules = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function __construct() |
|
25
|
|
|
{ |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return static |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function getInstance() |
|
32
|
|
|
{ |
|
33
|
|
|
static $instance = null; |
|
34
|
|
|
if (null === $instance) { |
|
35
|
|
|
$instance = new static(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return $instance; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param \NilPortugues\Validator\AbstractValidator $validatorType |
|
43
|
|
|
* @param string $rule |
|
44
|
|
|
* |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function parseRule($validatorType, $rule) |
|
48
|
|
|
{ |
|
49
|
|
|
$className = \get_class($validatorType); |
|
50
|
|
|
|
|
51
|
|
|
if (empty(self::$rules[$className])) { |
|
52
|
|
|
self::buildRules($validatorType, $className); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$ruleName = \explode(":", $rule); |
|
56
|
|
|
$methodName = self::getMethodName($className, $ruleName[0]); |
|
57
|
|
|
|
|
58
|
|
|
$arguments = \substr($rule, \strlen($ruleName[0]) + 1, \strlen($rule)); |
|
59
|
|
|
$arguments = \explode(":", $arguments); |
|
60
|
|
|
$arguments = self::convertStringBooleanToBoolean($arguments); |
|
61
|
|
|
|
|
62
|
|
|
return [$methodName, $arguments]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param \NilPortugues\Validator\AbstractValidator $validatorType |
|
67
|
|
|
* @param string $key |
|
68
|
|
|
*/ |
|
69
|
|
|
private static function buildRules($validatorType, $key) |
|
70
|
|
|
{ |
|
71
|
|
|
$classMethods = self::extractValidatorMethods($validatorType); |
|
72
|
|
|
$funcMethods = []; |
|
73
|
|
|
|
|
74
|
|
|
foreach ($classMethods as $method) { |
|
75
|
|
|
$camelCase = self::camelCaseToUnderscore($method); |
|
76
|
|
|
$funcMethods[$method] = $method; |
|
77
|
|
|
$funcMethods[$camelCase] = $method; |
|
78
|
|
|
|
|
79
|
|
|
if (self::startsWith($camelCase, 'is_')) { |
|
80
|
|
|
$methodAlias = \str_replace('is_', '', $camelCase); |
|
81
|
|
|
$funcMethods[$methodAlias] = $method; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
self::$rules[$key] = $funcMethods; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param \NilPortugues\Validator\AbstractValidator $validatorType |
|
90
|
|
|
* |
|
91
|
|
|
* @return array |
|
92
|
|
|
*/ |
|
93
|
|
|
private static function extractValidatorMethods($validatorType) |
|
94
|
|
|
{ |
|
95
|
|
|
$classMethods = \get_class_methods($validatorType); |
|
96
|
|
|
$remove = ['__construct', 'validate', 'setError', 'getErrors']; |
|
97
|
|
|
|
|
98
|
|
|
return self::unsetValues($classMethods, $remove); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param array $data |
|
103
|
|
|
* @param string[] $removeValues |
|
104
|
|
|
* |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
private static function unsetValues(array &$data, array &$removeValues) |
|
108
|
|
|
{ |
|
109
|
|
|
foreach ($removeValues as $value) { |
|
110
|
|
|
$position = \array_search($value, $data); |
|
111
|
|
|
if ($position !== false) { |
|
112
|
|
|
unset($data[$position]); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
return $data; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param $camel |
|
120
|
|
|
* @param string $splitter |
|
121
|
|
|
* |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
private static function camelCaseToUnderscore($camel, $splitter = "_") |
|
125
|
|
|
{ |
|
126
|
|
|
$camel = \preg_replace( |
|
127
|
|
|
'/(?!^)[[:upper:]][[:lower:]]/', |
|
128
|
|
|
'$0', |
|
129
|
|
|
\preg_replace('/(?!^)[[:upper:]]+/', $splitter . '$0', $camel) |
|
130
|
|
|
); |
|
131
|
|
|
|
|
132
|
|
|
return \strtolower($camel); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param string $haystack |
|
137
|
|
|
* @param string $needle |
|
138
|
|
|
* |
|
139
|
|
|
* @return bool |
|
140
|
|
|
*/ |
|
141
|
|
|
private static function startsWith($haystack, $needle) |
|
142
|
|
|
{ |
|
143
|
|
|
return \substr($haystack, 0, \strlen($needle)) === $needle; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param string $key |
|
148
|
|
|
* @param $rule |
|
149
|
|
|
* |
|
150
|
|
|
* @return string |
|
151
|
|
|
*/ |
|
152
|
|
|
private static function getMethodName($key, $rule) |
|
153
|
|
|
{ |
|
154
|
|
|
return self::$rules[$key][$rule]; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param $arguments |
|
159
|
|
|
* |
|
160
|
|
|
* @return mixed |
|
161
|
|
|
*/ |
|
162
|
|
|
private static function convertStringBooleanToBoolean($arguments) |
|
163
|
|
|
{ |
|
164
|
|
|
$toBoolean = ['true' => true, 'false' => false]; |
|
165
|
|
|
|
|
166
|
|
|
foreach ($arguments as &$argument) { |
|
167
|
|
|
$toLower = \strtolower($argument); |
|
168
|
|
|
if (\array_key_exists($toLower, $toBoolean)) { |
|
169
|
|
|
$argument = $toBoolean[$toLower]; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
return $arguments; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|