@@ -2,7 +2,6 @@ |
||
2 | 2 | namespace Comfort\Validator; |
3 | 3 | |
4 | 4 | use Comfort\Comfort; |
5 | -use Comfort\Error; |
|
6 | 5 | use Comfort\Exception\ValidationException; |
7 | 6 | use Comfort\ValidationError; |
8 | 7 |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | { |
84 | 84 | $this->optional = false; |
85 | 85 | |
86 | - $this->add(function ($value, $nameKey) { |
|
86 | + $this->add(function($value, $nameKey) { |
|
87 | 87 | if (is_null($value)) { |
88 | 88 | $this->createError('required', $value, $nameKey); |
89 | 89 | } |
@@ -127,7 +127,7 @@ discard block |
||
127 | 127 | */ |
128 | 128 | public function anyOf(array $vals) |
129 | 129 | { |
130 | - $this->add(function ($value, $nameKey) use ($vals) { |
|
130 | + $this->add(function($value, $nameKey) use ($vals) { |
|
131 | 131 | if (!in_array($value, $vals)) { |
132 | 132 | return $this->createError('anyof', $value, $nameKey); |
133 | 133 | } |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | */ |
145 | 145 | public function invalid(array $vals) |
146 | 146 | { |
147 | - return $this->add(function ($value, $nameKey) use ($vals) { |
|
147 | + return $this->add(function($value, $nameKey) use ($vals) { |
|
148 | 148 | if (in_array($value, $vals)) { |
149 | 149 | return $this->createError('invalid', $value, $nameKey); |
150 | 150 | } |
@@ -211,7 +211,7 @@ discard block |
||
211 | 211 | */ |
212 | 212 | public function errorMessages(array $errorMessages) |
213 | 213 | { |
214 | - $errorMessages = array_map(function ($errorMessage) { |
|
214 | + $errorMessages = array_map(function($errorMessage) { |
|
215 | 215 | if (is_string($errorMessage)) { |
216 | 216 | $errorMessage = ['message' => $errorMessage]; |
217 | 217 | } |
@@ -244,7 +244,7 @@ discard block |
||
244 | 244 | |
245 | 245 | $errorHandler = $this->errorHandlers[$key]; |
246 | 246 | if (!array_key_exists('message_formatter', $errorHandler)) { |
247 | - $messageFormatter = function ($template, $value, $validationValue = null) { |
|
247 | + $messageFormatter = function($template, $value, $validationValue = null) { |
|
248 | 248 | return sprintf($template, $value, $validationValue); |
249 | 249 | }; |
250 | 250 | } else { |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | |
18 | 18 | $this->toBool(false); |
19 | 19 | |
20 | - $this->add(function ($value, $nameKey) { |
|
20 | + $this->add(function($value, $nameKey) { |
|
21 | 21 | if (!is_array($value)) { |
22 | 22 | return $this->createError('array.not_array', $value, $nameKey); |
23 | 23 | } |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | */ |
54 | 54 | public function keys(array $definition) |
55 | 55 | { |
56 | - return $this->add(function (&$value) use ($definition) { |
|
56 | + return $this->add(function(&$value) use ($definition) { |
|
57 | 57 | /** |
58 | 58 | * @var string $key |
59 | 59 | * @var AbstractValidator $validator |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | */ |
88 | 88 | public function min($min) |
89 | 89 | { |
90 | - return $this->add(function ($value, $nameKey) use ($min) { |
|
90 | + return $this->add(function($value, $nameKey) use ($min) { |
|
91 | 91 | if (count($value) < $min) { |
92 | 92 | return $this->createError('array.min', $value, $nameKey); |
93 | 93 | } |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | */ |
103 | 103 | public function max($max) |
104 | 104 | { |
105 | - return $this->add(function ($value, $nameKey) use ($max) { |
|
105 | + return $this->add(function($value, $nameKey) use ($max) { |
|
106 | 106 | if (count($value) > $max) { |
107 | 107 | return $this->createError('array.max', $value, $nameKey); |
108 | 108 | } |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | */ |
118 | 118 | public function length($length) |
119 | 119 | { |
120 | - return $this->add(function ($value, $nameKey) use ($length) { |
|
120 | + return $this->add(function($value, $nameKey) use ($length) { |
|
121 | 121 | if (count($value) != $length) { |
122 | 122 | return $this->createError('array.length', $value, $nameKey); |
123 | 123 | } |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | */ |
132 | 132 | public function unique() |
133 | 133 | { |
134 | - return $this->add(function ($value, $nameKey) { |
|
134 | + return $this->add(function($value, $nameKey) { |
|
135 | 135 | if (md5(serialize($value)) != md5(serialize(array_unique($value)))) { |
136 | 136 | return $this->createError('array.unique', $value, $nameKey); |
137 | 137 | } |
@@ -147,7 +147,7 @@ discard block |
||
147 | 147 | */ |
148 | 148 | public function items(AbstractValidator $definition) |
149 | 149 | { |
150 | - return $this->add(function ($value, $nameKey) use ($definition) { |
|
150 | + return $this->add(function($value, $nameKey) use ($definition) { |
|
151 | 151 | foreach ($value as $key => $val) { |
152 | 152 | $resp = $definition($val, $nameKey); |
153 | 153 | if ($resp instanceof ValidationError) { |
@@ -11,7 +11,7 @@ discard block |
||
11 | 11 | */ |
12 | 12 | public function format($format) |
13 | 13 | { |
14 | - return $this->add(function ($value, $namekey) use ($format) { |
|
14 | + return $this->add(function($value, $namekey) use ($format) { |
|
15 | 15 | $date = \DateTime::createFromFormat($format, $value); |
16 | 16 | if (false === $date) { |
17 | 17 | return $this->createError('date.format', $value, $namekey); |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | */ |
28 | 28 | public function min($date) |
29 | 29 | { |
30 | - return $this->add(function ($value, $nameKey) use ($date) { |
|
30 | + return $this->add(function($value, $nameKey) use ($date) { |
|
31 | 31 | $minDate = strtotime($date); |
32 | 32 | $curDate = strtotime($value); |
33 | 33 | |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | */ |
46 | 46 | public function max($date) |
47 | 47 | { |
48 | - return $this->add(function ($value, $nameKey) use ($date) { |
|
48 | + return $this->add(function($value, $nameKey) use ($date) { |
|
49 | 49 | $maxDate = strtotime($date); |
50 | 50 | $curDate = strtotime($value); |
51 | 51 | |
@@ -62,10 +62,10 @@ discard block |
||
62 | 62 | */ |
63 | 63 | public function timestamp() |
64 | 64 | { |
65 | - return $this->add(function ($value, $nameKey) { |
|
65 | + return $this->add(function($value, $nameKey) { |
|
66 | 66 | try { |
67 | 67 | new \DateTime('@' . $value); |
68 | - } catch (\Exception $e) { |
|
68 | + }catch (\Exception $e) { |
|
69 | 69 | return $this->createError('date.timestamp', $value, $nameKey); |
70 | 70 | } |
71 | 71 | }); |
@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | |
17 | 17 | $this->toBool(false); |
18 | 18 | |
19 | - $this->add(function ($value, $nameKey) { |
|
19 | + $this->add(function($value, $nameKey) { |
|
20 | 20 | json_decode($value); |
21 | 21 | if (json_last_error() != JSON_ERROR_NONE) { |
22 | 22 | return $this->createError('json.invalid', $value, $nameKey); |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | */ |
33 | 33 | public function keys(array $definition) |
34 | 34 | { |
35 | - return $this->add(function ($value, $nameKey) use ($definition) { |
|
35 | + return $this->add(function($value, $nameKey) use ($definition) { |
|
36 | 36 | $decodedValue = json_decode($value, true); |
37 | 37 | $validation = $this->array()->keys($definition); |
38 | 38 | return $validation($decodedValue); |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | */ |
48 | 48 | public function items(AbstractValidator $definition) |
49 | 49 | { |
50 | - return $this->add(function ($value, $nameKey) use ($definition) { |
|
50 | + return $this->add(function($value, $nameKey) use ($definition) { |
|
51 | 51 | $decodedValue = json_decode($value, true); |
52 | 52 | $validation = $this->array()->items($definition); |
53 | 53 | return $validation($decodedValue); |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | */ |
51 | 51 | public function min($min) |
52 | 52 | { |
53 | - return $this->add(function ($value, $nameKey) use ($min) { |
|
53 | + return $this->add(function($value, $nameKey) use ($min) { |
|
54 | 54 | if ($value < $min) { |
55 | 55 | return $this->createError('number.min', $value, $nameKey); |
56 | 56 | } |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | */ |
66 | 66 | public function max($max) |
67 | 67 | { |
68 | - return $this->add(function ($value, $nameKey) use ($max) { |
|
68 | + return $this->add(function($value, $nameKey) use ($max) { |
|
69 | 69 | if ($value > $max) { |
70 | 70 | return $this->createError('number.max', $value, $nameKey); |
71 | 71 | } |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | */ |
82 | 82 | public function precision($precision) |
83 | 83 | { |
84 | - return $this->add(function ($value, $nameKey) use ($precision) { |
|
84 | + return $this->add(function($value, $nameKey) use ($precision) { |
|
85 | 85 | if (strlen(substr($value, strpos($value, '.') + 1)) != $precision) { |
86 | 86 | return $this->createError('number.precision', $value, $nameKey); |
87 | 87 | } |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | */ |
96 | 96 | public function positive() |
97 | 97 | { |
98 | - return $this->add(function ($value, $nameKey) { |
|
98 | + return $this->add(function($value, $nameKey) { |
|
99 | 99 | if ($value < 0) { |
100 | 100 | return $this->createError('number.positive', $value, $nameKey); |
101 | 101 | } |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | */ |
108 | 108 | public function negative() |
109 | 109 | { |
110 | - return $this->add(function ($value, $nameKey) { |
|
110 | + return $this->add(function($value, $nameKey) { |
|
111 | 111 | if ($value > 0) { |
112 | 112 | return $this->createError('number.negative', $value, $nameKey); |
113 | 113 | } |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | */ |
122 | 122 | public function isInt() |
123 | 123 | { |
124 | - return $this->add(function ($value, $nameKey) { |
|
124 | + return $this->add(function($value, $nameKey) { |
|
125 | 125 | if (!is_int($value)) { |
126 | 126 | return $this->createError('number.is_int', $value, $nameKey); |
127 | 127 | } |
@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | */ |
136 | 136 | public function isFloat() |
137 | 137 | { |
138 | - return $this->add(function ($value, $nameKey) { |
|
138 | + return $this->add(function($value, $nameKey) { |
|
139 | 139 | if (!is_float($value)) { |
140 | 140 | return $this->createError('number.is_float', $value, $nameKey); |
141 | 141 | } |
@@ -149,7 +149,7 @@ discard block |
||
149 | 149 | */ |
150 | 150 | public function isNumber() |
151 | 151 | { |
152 | - return $this->add(function ($value, $nameKey) { |
|
152 | + return $this->add(function($value, $nameKey) { |
|
153 | 153 | if (!is_numeric($value)) { |
154 | 154 | return $this->createError('number.is_numeric', $value, $nameKey); |
155 | 155 | } |
@@ -11,7 +11,7 @@ |
||
11 | 11 | */ |
12 | 12 | public function anyOf(array $vals) |
13 | 13 | { |
14 | - $this->add(function ($value, $nameKey) use ($vals) { |
|
14 | + $this->add(function($value, $nameKey) use ($vals) { |
|
15 | 15 | if (!in_array($value, $vals)) { |
16 | 16 | return $this->createError('anyof', $value, $nameKey); |
17 | 17 | } |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | */ |
34 | 34 | private function orMethod($conditions) |
35 | 35 | { |
36 | - return $this->add(function ($value, $nameKey) use ($conditions) { |
|
36 | + return $this->add(function($value, $nameKey) use ($conditions) { |
|
37 | 37 | $errors = []; |
38 | 38 | foreach ($conditions as $condition) { |
39 | 39 | if (!$condition instanceof AbstractValidator) { |
@@ -58,7 +58,7 @@ discard block |
||
58 | 58 | */ |
59 | 59 | private function ifThenElse($conditions) |
60 | 60 | { |
61 | - return $this->add(function ($value, $nameKey) use ($conditions) { |
|
61 | + return $this->add(function($value, $nameKey) use ($conditions) { |
|
62 | 62 | |
63 | 63 | foreach ($conditions as $condition) { |
64 | 64 | if (!isset($condition['is'])) { |
@@ -10,7 +10,7 @@ |
||
10 | 10 | * Execute the validation stack and fail on first |
11 | 11 | * |
12 | 12 | * @param $value |
13 | - * @param null $key |
|
13 | + * @param null|string $key |
|
14 | 14 | * @return bool|ValidationError|null |
15 | 15 | */ |
16 | 16 | protected function validate($value, $key = null) |
@@ -72,7 +72,7 @@ |
||
72 | 72 | } while (next($this->validationStack)); |
73 | 73 | |
74 | 74 | return $value; |
75 | - } catch (ValidationException $validationException) { |
|
75 | + }catch (ValidationException $validationException) { |
|
76 | 76 | if ($this->toBool) { |
77 | 77 | return false; |
78 | 78 | } |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | ] |
46 | 46 | ]; |
47 | 47 | |
48 | - $this->add(function ($value, $nameKey) { |
|
48 | + $this->add(function($value, $nameKey) { |
|
49 | 49 | if (is_null($value)) { |
50 | 50 | return; |
51 | 51 | } |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | */ |
65 | 65 | public function token() |
66 | 66 | { |
67 | - return $this->add(function ($value, $nameKey) { |
|
67 | + return $this->add(function($value, $nameKey) { |
|
68 | 68 | preg_match('/[a-zA-Z0-9_]+/', $value, $matches); |
69 | 69 | if (!(isset($matches[0]) && ($matches[0] == $value))) { |
70 | 70 | $this->createError('string.token', $value, $nameKey); |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | */ |
82 | 82 | public function min($min) |
83 | 83 | { |
84 | - return $this->add(function ($value, $nameKey) use ($min) { |
|
84 | + return $this->add(function($value, $nameKey) use ($min) { |
|
85 | 85 | if (strlen($value) < $min) { |
86 | 86 | $this->createError('string.min', $value, $nameKey, $min); |
87 | 87 | } |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | */ |
97 | 97 | public function max($max) |
98 | 98 | { |
99 | - return $this->add(function ($value, $nameKey) use ($max) { |
|
99 | + return $this->add(function($value, $nameKey) use ($max) { |
|
100 | 100 | if (strlen($value) > $max) { |
101 | 101 | return $this->createError('string.max', $value, $nameKey, $max); |
102 | 102 | } |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | */ |
112 | 112 | public function matches($regex) |
113 | 113 | { |
114 | - return $this->add(function ($value, $nameKey) use ($regex) { |
|
114 | + return $this->add(function($value, $nameKey) use ($regex) { |
|
115 | 115 | if (!preg_match($regex, $value)) { |
116 | 116 | return $this->createError('string.matches', $value, $nameKey, $regex); |
117 | 117 | } |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | */ |
126 | 126 | public function length($length) |
127 | 127 | { |
128 | - return $this->add(function ($value, $nameKey) use ($length) { |
|
128 | + return $this->add(function($value, $nameKey) use ($length) { |
|
129 | 129 | if (strlen($value) != $length) { |
130 | 130 | return $this->createError('string.length', $value, $nameKey, $length); |
131 | 131 | } |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | */ |
140 | 140 | public function alphanum() |
141 | 141 | { |
142 | - return $this->add(function ($value, $nameKey) { |
|
142 | + return $this->add(function($value, $nameKey) { |
|
143 | 143 | if (!ctype_alnum($value)) { |
144 | 144 | return $this->createError('string.alphanum', $value, $nameKey); |
145 | 145 | } |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | */ |
154 | 154 | public function alpha() |
155 | 155 | { |
156 | - return $this->add(function ($value, $nameKey) { |
|
156 | + return $this->add(function($value, $nameKey) { |
|
157 | 157 | if (!ctype_alpha($value)) { |
158 | 158 | return $this->createError('string.alpha', $value, $nameKey); |
159 | 159 | } |
@@ -167,7 +167,7 @@ discard block |
||
167 | 167 | */ |
168 | 168 | public function email() |
169 | 169 | { |
170 | - return $this->add(function ($value, $nameKey) { |
|
170 | + return $this->add(function($value, $nameKey) { |
|
171 | 171 | if (!filter_var($value, FILTER_VALIDATE_EMAIL)) { |
172 | 172 | return $this->createError('string.email', $value, $nameKey); |
173 | 173 | } |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | */ |
182 | 182 | public function creditCard() |
183 | 183 | { |
184 | - return $this->add(function ($value, $nameKey) { |
|
184 | + return $this->add(function($value, $nameKey) { |
|
185 | 185 | $i = strlen($value); |
186 | 186 | $sum = 0; |
187 | 187 | $mul = 1; |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | */ |
207 | 207 | public function ip() |
208 | 208 | { |
209 | - return $this->add(function ($value, $nameKey) { |
|
209 | + return $this->add(function($value, $nameKey) { |
|
210 | 210 | if (!filter_var($value, FILTER_VALIDATE_IP)) { |
211 | 211 | return $this->createError('string.ip', $value, $nameKey); |
212 | 212 | } |
@@ -220,7 +220,7 @@ discard block |
||
220 | 220 | */ |
221 | 221 | public function uri() |
222 | 222 | { |
223 | - return $this->add(function ($value, $nameKey) { |
|
223 | + return $this->add(function($value, $nameKey) { |
|
224 | 224 | if (!filter_var($value, FILTER_VALIDATE_URL)) { |
225 | 225 | return $this->createError('string.uri', $value, $nameKey); |
226 | 226 | } |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | */ |
238 | 238 | public function replace($pattern, $replacement) |
239 | 239 | { |
240 | - return $this->add(function ($value, $nameKey) use ($pattern, $replacement) { |
|
240 | + return $this->add(function($value, $nameKey) use ($pattern, $replacement) { |
|
241 | 241 | return preg_replace($pattern, $replacement, $value); |
242 | 242 | }); |
243 | 243 | } |