1
|
|
|
<?php |
2
|
|
|
namespace Comfort\Validator; |
3
|
|
|
|
4
|
|
|
use Comfort\Comfort; |
5
|
|
|
use Comfort\Error; |
6
|
|
|
use Comfort\Exception\ValidationException; |
7
|
|
|
use Comfort\ValidationError; |
8
|
|
|
|
9
|
|
|
class ArrayValidator extends AbstractValidator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* ArrayValidator constructor. |
13
|
|
|
* @param Comfort $comfort |
14
|
|
|
*/ |
15
|
|
|
public function __construct(Comfort $comfort) |
16
|
|
|
{ |
17
|
|
|
parent::__construct($comfort); |
18
|
|
|
|
19
|
|
|
$this->toBool(false); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Given a set of keys apply the specified validation rules |
24
|
|
|
* |
25
|
|
|
* @param array $definition |
26
|
|
|
* @return $this |
27
|
|
|
*/ |
28
|
|
|
public function keys(array $definition) |
29
|
|
|
{ |
30
|
|
|
return $this->add(function(&$value) use ($definition) { |
31
|
|
|
/** |
32
|
|
|
* @var string $key |
33
|
|
|
* @var AbstractValidator $validator |
34
|
|
|
*/ |
35
|
|
|
foreach ($definition as $key => $validator) { |
36
|
|
|
$validator->toBool(false); |
37
|
|
|
$validatorValue = isset($value[$key]) ? $value[$key] : null; |
38
|
|
|
$result = $validator($validatorValue, $key); |
39
|
|
|
if ($result instanceof ValidationError) { |
40
|
|
|
throw new ValidationException($result->getKey(), $result->getMessage()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (isset($value[$key])) { |
44
|
|
|
$value[$key] = $result; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $value; |
49
|
|
|
}); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Validate array has more than $min elements |
54
|
|
|
* |
55
|
|
|
* @param $min |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
|
|
public function min($min) |
59
|
|
|
{ |
60
|
|
|
return $this->add(function($value, $nameKey) use($min) { |
61
|
|
|
if (count($value) < $min) { |
62
|
|
|
return $this->createError('array.min', $value, $nameKey); |
63
|
|
|
} |
64
|
|
|
}); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Validate array has only up to $max characters |
69
|
|
|
* |
70
|
|
|
* @param $max |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function max($max) |
74
|
|
|
{ |
75
|
|
|
return $this->add(function($value, $nameKey) use($max) { |
76
|
|
|
if (count($value) > $max) { |
77
|
|
|
return $this->createError('array.max', $value, $nameKey); |
78
|
|
|
} |
79
|
|
|
}); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Validate array contains exactly $length elements |
84
|
|
|
* |
85
|
|
|
* @param $length |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
|
|
public function length($length) |
89
|
|
|
{ |
90
|
|
|
return $this->add(function($value, $nameKey) use($length) { |
91
|
|
|
if (count($value) != $length) { |
92
|
|
|
return $this->createError('array.length', $value, $nameKey); |
93
|
|
|
} |
94
|
|
|
}); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Validate array is unique |
99
|
|
|
* |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function unique() |
103
|
|
|
{ |
104
|
|
|
return $this->add(function($value, $nameKey) { |
105
|
|
|
if (md5(serialize($value)) != md5(serialize(array_unique($value)))) { |
106
|
|
|
return $this->createError('array.unique', $value, $nameKey); |
107
|
|
|
} |
108
|
|
|
}); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Apply $definition to individual items in the array, |
113
|
|
|
* used in case of multi-dimensional array |
114
|
|
|
* |
115
|
|
|
* @param AbstractValidator $definition |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
|
|
public function items(AbstractValidator $definition) |
119
|
|
|
{ |
120
|
|
|
return $this->add(function($value, $nameKey) use($definition) { |
121
|
|
|
if (!is_array($value)) { |
122
|
|
|
return $this->createError('array.items.not_array', $value, $nameKey); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
foreach ($value as $key => $val) { |
126
|
|
|
$resp = $definition($val, $nameKey); |
127
|
|
|
if ($resp instanceof ValidationError) { |
128
|
|
|
return $this->createError( |
129
|
|
|
'array.items.validation_failure', $value, $nameKey |
|
|
|
|
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
}); |
134
|
|
|
|
135
|
|
|
return $this; |
|
|
|
|
136
|
|
|
} |
137
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: