1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Todd Burry <[email protected]> |
4
|
|
|
* @copyright 2009-2018 Vanilla Forums Inc. |
5
|
|
|
* @license MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Garden\Schema; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* A parameters class for field validation. |
12
|
|
|
*/ |
13
|
|
|
class ValidationField { |
14
|
|
|
/** |
15
|
|
|
* @var array|Schema |
16
|
|
|
*/ |
17
|
|
|
private $field; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Validation |
21
|
|
|
*/ |
22
|
|
|
private $validation; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $name; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
private $sparse; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Construct a new {@link ValidationField} object. |
36
|
|
|
* |
37
|
|
|
* @param Validation $validation The validation object that contains errors. |
38
|
|
|
* @param array|Schema $field The field definition. |
39
|
|
|
* @param string $name The path to the field. |
40
|
|
|
* @param bool $sparse Whether this is a sparse validation or not. |
41
|
|
|
*/ |
42
|
173 |
|
public function __construct(Validation $validation, $field, $name, $sparse = false) { |
43
|
173 |
|
$this->field = $field; |
44
|
173 |
|
$this->validation = $validation; |
45
|
173 |
|
$this->name = $name; |
46
|
173 |
|
$this->sparse = $sparse; |
47
|
173 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Add a validation error. |
51
|
|
|
* |
52
|
|
|
* @param string $error The message code. |
53
|
|
|
* @param int|array $options An array of additional information to add to the error entry or a numeric error code. |
54
|
|
|
* @return $this |
55
|
|
|
* @see Validation::addError() |
56
|
|
|
*/ |
57
|
22 |
|
public function addError($error, $options = []) { |
58
|
22 |
|
$this->validation->addError($this->getName(), $error, $options); |
59
|
22 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add an invalid type error. |
64
|
|
|
* |
65
|
|
|
* @param string $type The type that was checked. |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
45 |
|
public function addTypeError($type = '') { |
69
|
45 |
|
$type = $type ?: $this->getType(); |
70
|
|
|
|
71
|
45 |
|
$this->validation->addError( |
72
|
45 |
|
$this->getName(), |
73
|
45 |
|
'invalid', |
74
|
|
|
[ |
75
|
45 |
|
'type' => $type, |
76
|
45 |
|
'messageCode' => '{field} is not a valid {type}.', |
77
|
45 |
|
'status' => 422 |
78
|
|
|
] |
79
|
|
|
); |
80
|
|
|
|
81
|
45 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Check whether or not this field is has errors. |
86
|
|
|
* |
87
|
|
|
* @return bool Returns true if the field has no errors, false otherwise. |
88
|
|
|
*/ |
89
|
66 |
|
public function isValid() { |
90
|
66 |
|
return $this->getValidation()->isValidField($this->getName()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Merge a validation object to this one. |
95
|
|
|
* |
96
|
|
|
* @param Validation $validation The validation object to merge. |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
|
|
public function merge(Validation $validation) { |
100
|
|
|
$this->getValidation()->merge($validation, $this->getName()); |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the field. |
106
|
|
|
* |
107
|
|
|
* @return array|Schema Returns the field. |
108
|
|
|
*/ |
109
|
173 |
|
public function getField() { |
110
|
173 |
|
return $this->field; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set the field. |
115
|
|
|
* |
116
|
|
|
* This method is only meant to be called from within the schema class. |
117
|
|
|
* |
118
|
|
|
* @param array|Schema $field The new field. |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
94 |
|
public function setField($field) { |
122
|
94 |
|
$this->field = $field; |
123
|
94 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get the validation. |
128
|
|
|
* |
129
|
|
|
* @return Validation Returns the validation. |
130
|
|
|
*/ |
131
|
173 |
|
public function getValidation() { |
132
|
173 |
|
return $this->validation; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get the name. |
137
|
|
|
* |
138
|
|
|
* @return string Returns the name. |
139
|
|
|
*/ |
140
|
173 |
|
public function getName() { |
141
|
173 |
|
return $this->name; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set the name. |
146
|
|
|
* |
147
|
|
|
* This method is only meant to be called from within the schema class. |
148
|
|
|
* |
149
|
|
|
* @param string $name The new name. |
150
|
|
|
* @return $this |
151
|
|
|
*/ |
152
|
106 |
|
public function setName($name) { |
153
|
106 |
|
$this->name = $name; |
154
|
106 |
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get the field type. |
159
|
|
|
* |
160
|
|
|
* @return string|string[]|null Returns a type string, array of type strings, or null if there isn't one. |
161
|
|
|
*/ |
162
|
173 |
|
public function getType() { |
163
|
173 |
|
return isset($this->field['type']) ? $this->field['type'] : null; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Whether or not the field has a given type. |
168
|
|
|
* |
169
|
|
|
* @param string $type The single type to test. |
170
|
|
|
* @return bool Returns **true** if the field has the given type or **false** otherwise. |
171
|
|
|
*/ |
172
|
31 |
|
public function hasType($type) { |
173
|
31 |
|
return in_array($type, (array)$this->getType()); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get a value fom the field. |
178
|
|
|
* |
179
|
|
|
* @param string $key The key to look at. |
180
|
|
|
* @param mixed $default The default to return if the key isn't found. |
181
|
|
|
* @return mixed Returns a value or the default. |
182
|
|
|
*/ |
183
|
173 |
|
public function val($key, $default = null) { |
184
|
173 |
|
return isset($this->field[$key]) ? $this->field[$key] : $default; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Whether or not the field has a value. |
189
|
|
|
* |
190
|
|
|
* @param string $key The key to look at. |
191
|
|
|
* @return bool Returns **true** if the field has a key or **false** otherwise. |
192
|
|
|
*/ |
193
|
25 |
|
public function hasVal($key) { |
194
|
25 |
|
return array_key_exists($key, $this->field); |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get the error count for this field. |
199
|
|
|
*/ |
200
|
|
|
public function getErrorCount() { |
201
|
|
|
return $this->getValidation()->getErrorCount($this->getName()); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Whether or not this is a sparse validation.. |
206
|
|
|
* |
207
|
|
|
* @return bool Returns **true** if this is a sparse validation or **false** otherwise. |
208
|
|
|
*/ |
209
|
3 |
|
public function isSparse() { |
210
|
3 |
|
return $this->sparse; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|