|
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
|
|
|
* This is an internal class and may change in the future. |
|
14
|
|
|
*/ |
|
15
|
|
|
class ValidationField { |
|
16
|
|
|
/** |
|
17
|
|
|
* @var array|Schema |
|
18
|
|
|
*/ |
|
19
|
|
|
private $field; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Validation |
|
23
|
|
|
*/ |
|
24
|
|
|
private $validation; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $name; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private $options; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Construct a new {@link ValidationField} object. |
|
38
|
|
|
* |
|
39
|
|
|
* @param Validation $validation The validation object that contains errors. |
|
40
|
|
|
* @param array|Schema $field The field definition. |
|
41
|
|
|
* @param string $name The path to the field. |
|
42
|
|
|
* @param array $options Validation options. |
|
43
|
|
|
* |
|
44
|
|
|
* - **sparse**: Whether or not this is a sparse validation. |
|
45
|
|
|
*/ |
|
46
|
199 |
|
public function __construct(Validation $validation, $field, $name, array $options = []) { |
|
47
|
199 |
|
$this->field = $field; |
|
48
|
199 |
|
$this->validation = $validation; |
|
49
|
199 |
|
$this->name = $name; |
|
50
|
199 |
|
$this->options = $options + ['sparse' => false]; |
|
51
|
199 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Add a validation error. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $error The message code. |
|
57
|
|
|
* @param int|array $options An array of additional information to add to the error entry or a numeric error code. |
|
58
|
|
|
* @return $this |
|
59
|
|
|
* @see Validation::addError() |
|
60
|
|
|
*/ |
|
61
|
31 |
|
public function addError($error, $options = []) { |
|
62
|
31 |
|
$this->validation->addError($this->getName(), $error, $options); |
|
63
|
31 |
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Add an invalid type error. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $type The type that was checked. |
|
70
|
|
|
* @return $this |
|
71
|
|
|
*/ |
|
72
|
45 |
|
public function addTypeError($type = '') { |
|
73
|
45 |
|
$type = $type ?: $this->getType(); |
|
74
|
|
|
|
|
75
|
45 |
|
$this->validation->addError( |
|
76
|
45 |
|
$this->getName(), |
|
77
|
45 |
|
'invalid', |
|
78
|
|
|
[ |
|
79
|
45 |
|
'type' => $type, |
|
80
|
45 |
|
'messageCode' => '{field} is not a valid {type}.', |
|
81
|
45 |
|
'status' => 422 |
|
82
|
|
|
] |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
45 |
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Check whether or not this field is has errors. |
|
90
|
|
|
* |
|
91
|
|
|
* @return bool Returns true if the field has no errors, false otherwise. |
|
92
|
|
|
*/ |
|
93
|
76 |
|
public function isValid() { |
|
94
|
76 |
|
return $this->getValidation()->isValidField($this->getName()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Merge a validation object to this one. |
|
99
|
|
|
* |
|
100
|
|
|
* @param Validation $validation The validation object to merge. |
|
101
|
|
|
* @return $this |
|
102
|
|
|
*/ |
|
103
|
|
|
public function merge(Validation $validation) { |
|
104
|
|
|
$this->getValidation()->merge($validation, $this->getName()); |
|
105
|
|
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get the field. |
|
110
|
|
|
* |
|
111
|
|
|
* @return array|Schema Returns the field. |
|
112
|
|
|
*/ |
|
113
|
199 |
|
public function getField() { |
|
114
|
199 |
|
return $this->field; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Set the field. |
|
119
|
|
|
* |
|
120
|
|
|
* This method is only meant to be called from within the schema class. |
|
121
|
|
|
* |
|
122
|
|
|
* @param array|Schema $field The new field. |
|
123
|
|
|
* @return $this |
|
124
|
|
|
*/ |
|
125
|
102 |
|
public function setField($field) { |
|
126
|
102 |
|
$this->field = $field; |
|
127
|
102 |
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get the validation. |
|
132
|
|
|
* |
|
133
|
|
|
* @return Validation Returns the validation. |
|
134
|
|
|
*/ |
|
135
|
199 |
|
public function getValidation() { |
|
136
|
199 |
|
return $this->validation; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get the name. |
|
141
|
|
|
* |
|
142
|
|
|
* @return string Returns the name. |
|
143
|
|
|
*/ |
|
144
|
199 |
|
public function getName() { |
|
145
|
199 |
|
return $this->name; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Set the name. |
|
150
|
|
|
* |
|
151
|
|
|
* This method is only meant to be called from within the schema class. |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $name The new name. |
|
154
|
|
|
* @return $this |
|
155
|
|
|
*/ |
|
156
|
115 |
|
public function setName($name) { |
|
157
|
115 |
|
$this->name = $name; |
|
158
|
115 |
|
return $this; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Get the field type. |
|
163
|
|
|
* |
|
164
|
|
|
* @return string|string[]|null Returns a type string, array of type strings, or null if there isn't one. |
|
165
|
|
|
*/ |
|
166
|
199 |
|
public function getType() { |
|
167
|
199 |
|
return $this->field['type'] ?? null; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Whether or not the field has a given type. |
|
172
|
|
|
* |
|
173
|
|
|
* @param string $type The single type to test. |
|
174
|
|
|
* @return bool Returns **true** if the field has the given type or **false** otherwise. |
|
175
|
|
|
*/ |
|
176
|
31 |
|
public function hasType($type) { |
|
177
|
31 |
|
return in_array($type, (array)$this->getType()); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Get a value fom the field. |
|
182
|
|
|
* |
|
183
|
|
|
* @param string $key The key to look at. |
|
184
|
|
|
* @param mixed $default The default to return if the key isn't found. |
|
185
|
|
|
* @return mixed Returns a value or the default. |
|
186
|
|
|
*/ |
|
187
|
199 |
|
public function val($key, $default = null) { |
|
188
|
199 |
|
return $this->field[$key] ?? $default; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Whether or not the field has a value. |
|
193
|
|
|
* |
|
194
|
|
|
* @param string $key The key to look at. |
|
195
|
|
|
* @return bool Returns **true** if the field has a key or **false** otherwise. |
|
196
|
|
|
*/ |
|
197
|
27 |
|
public function hasVal($key) { |
|
198
|
27 |
|
return array_key_exists($key, $this->field); |
|
|
|
|
|
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Get the error count for this field. |
|
203
|
|
|
*/ |
|
204
|
51 |
|
public function getErrorCount() { |
|
205
|
51 |
|
return $this->getValidation()->getErrorCount($this->getName()); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Whether or not we are validating a request. |
|
210
|
|
|
* |
|
211
|
|
|
* @return bool Returns **true** of we are validating a request or **false** otherwise. |
|
212
|
|
|
*/ |
|
213
|
103 |
|
public function isRequest(): bool { |
|
214
|
103 |
|
return $this->options['request'] ?? false; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Whether or not we are validating a response. |
|
219
|
|
|
* |
|
220
|
|
|
* @return bool Returns **true** of we are validating a response or **false** otherwise. |
|
221
|
|
|
*/ |
|
222
|
103 |
|
public function isResponse(): bool { |
|
223
|
103 |
|
return $this->options['response'] ?? false; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Whether or not this is a sparse validation.. |
|
228
|
|
|
* |
|
229
|
|
|
* @return bool Returns **true** if this is a sparse validation or **false** otherwise. |
|
230
|
|
|
*/ |
|
231
|
28 |
|
public function isSparse() { |
|
232
|
28 |
|
return $this->getOption('sparse', false); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Gets the options array. |
|
237
|
|
|
* |
|
238
|
|
|
* @return array Returns an options array. |
|
239
|
|
|
*/ |
|
240
|
119 |
|
public function getOptions(): array { |
|
241
|
119 |
|
return $this->options; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Get an indivitual option. |
|
246
|
|
|
* |
|
247
|
|
|
* @param string $option The name of the option. |
|
248
|
|
|
* @param mixed $default The default value to return if the option doesn't exist. |
|
249
|
|
|
* @return mixed Returns the option or the default value. |
|
250
|
|
|
*/ |
|
251
|
28 |
|
public function getOption(string $option, $default = null) { |
|
252
|
28 |
|
return $this->options[$option] ?? $default; |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|