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
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $schemaPath; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Construct a new {@link ValidationField} object. |
43
|
|
|
* |
44
|
|
|
* @param Validation $validation The validation object that contains errors. |
45
|
|
|
* @param array|Schema $field The field definition. |
46
|
|
|
* @param string $name The path to the field. |
47
|
|
|
* @param string $schemaPath The path to the field within the parent schema. |
48
|
|
|
* |
49
|
|
|
* - **sparse**: Whether or not this is a sparse validation. |
50
|
|
|
* @param array $options Validation options. |
51
|
|
|
*/ |
52
|
236 |
|
public function __construct(Validation $validation, $field, string $name, string $schemaPath, array $options = []) { |
53
|
236 |
|
$this->field = $field; |
54
|
236 |
|
$this->validation = $validation; |
55
|
236 |
|
$this->name = $name; |
56
|
236 |
|
$this->schemaPath = $schemaPath; |
57
|
236 |
|
$this->options = $options + ['sparse' => false]; |
58
|
236 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Add a validation error. |
62
|
|
|
* |
63
|
|
|
* @param string $error The message code. |
64
|
|
|
* @param array $options An array of additional information to add to the error entry or a numeric error code. |
65
|
|
|
* @return $this |
66
|
|
|
* @see Validation::addError() |
67
|
|
|
*/ |
68
|
46 |
|
public function addError(string $error, array $options = []) { |
69
|
46 |
|
$this->validation->addError($this->getName(), $error, $options); |
70
|
46 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Add an invalid type error. |
75
|
|
|
* |
76
|
|
|
* @param mixed $value The erroneous value. |
77
|
|
|
* @param string $type The type that was checked. |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
43 |
|
public function addTypeError($value, $type = '') { |
81
|
43 |
|
$type = $type ?: $this->getType(); |
82
|
|
|
|
83
|
43 |
|
$this->validation->addError( |
84
|
43 |
|
$this->getName(), |
85
|
43 |
|
'type', |
86
|
|
|
[ |
87
|
43 |
|
'type' => $type, |
88
|
43 |
|
'value' => is_scalar($value) ? $value : null, |
89
|
43 |
|
'messageCode' => is_scalar($value) ? "{value} is not a valid $type." : "The value is not a valid $type." |
90
|
|
|
] |
91
|
|
|
); |
92
|
|
|
|
93
|
43 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Check whether or not this field is has errors. |
98
|
|
|
* |
99
|
|
|
* @return bool Returns true if the field has no errors, false otherwise. |
100
|
|
|
*/ |
101
|
102 |
|
public function isValid() { |
102
|
102 |
|
return $this->getValidation()->isValidField($this->getName()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Merge a validation object to this one. |
107
|
|
|
* |
108
|
|
|
* @param Validation $validation The validation object to merge. |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
|
|
public function merge(Validation $validation) { |
112
|
|
|
$this->getValidation()->merge($validation, $this->getName()); |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get the field. |
118
|
|
|
* |
119
|
|
|
* @return array|Schema Returns the field. |
120
|
|
|
*/ |
121
|
233 |
|
public function getField() { |
122
|
233 |
|
return $this->field; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Set the field. |
127
|
|
|
* |
128
|
|
|
* This method is only meant to be called from within the schema class. |
129
|
|
|
* |
130
|
|
|
* @param array|Schema $field The new field. |
131
|
|
|
* @return $this |
132
|
|
|
*/ |
133
|
119 |
|
public function setField($field) { |
134
|
119 |
|
$this->field = $field; |
135
|
119 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get the validation. |
140
|
|
|
* |
141
|
|
|
* @return Validation Returns the validation. |
142
|
|
|
*/ |
143
|
236 |
|
public function getValidation() { |
144
|
236 |
|
return $this->validation; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get the name. |
149
|
|
|
* |
150
|
|
|
* @return string Returns the name. |
151
|
|
|
*/ |
152
|
194 |
|
public function getName() { |
153
|
194 |
|
return $this->name; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Set the name. |
158
|
|
|
* |
159
|
|
|
* This method is only meant to be called from within the schema class. |
160
|
|
|
* |
161
|
|
|
* @param string $name The new name. |
162
|
|
|
* @return $this |
163
|
|
|
*/ |
164
|
134 |
|
public function setName($name) { |
165
|
134 |
|
$this->name = ltrim($name, '/'); |
166
|
134 |
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Check if allOf is available |
171
|
|
|
* |
172
|
|
|
* @return bool |
173
|
|
|
*/ |
174
|
225 |
|
public function hasAllOf() { |
175
|
225 |
|
return isset($this->field['allOf']); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get allof tree |
180
|
|
|
* |
181
|
|
|
* @return array |
182
|
|
|
*/ |
183
|
4 |
|
public function getAllOf() { |
184
|
4 |
|
return $this->field['allOf'] ?? []; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get the field type. |
189
|
|
|
* |
190
|
|
|
* @return string|string[] Returns a type string, array of type strings, or null if there isn't one. |
191
|
|
|
*/ |
192
|
224 |
|
public function getType() { |
193
|
224 |
|
return $this->field['type'] ?? ''; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Whether or not the field has a given type. |
198
|
|
|
* |
199
|
|
|
* @param string $type The single type to test. |
200
|
|
|
* @return bool Returns **true** if the field has the given type or **false** otherwise. |
201
|
|
|
*/ |
202
|
32 |
|
public function hasType($type) { |
203
|
32 |
|
return in_array($type, (array)$this->getType()); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get a value fom the field. |
208
|
|
|
* |
209
|
|
|
* @param string $key The key to look at. |
210
|
|
|
* @param mixed $default The default to return if the key isn't found. |
211
|
|
|
* @return mixed Returns a value or the default. |
212
|
|
|
*/ |
213
|
236 |
|
public function val($key, $default = null) { |
214
|
236 |
|
return $this->field[$key] ?? $default; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Whether or not the field has a value. |
219
|
|
|
* |
220
|
|
|
* @param string $key The key to look at. |
221
|
|
|
* @return bool Returns **true** if the field has a key or **false** otherwise. |
222
|
|
|
*/ |
223
|
36 |
|
public function hasVal($key) { |
224
|
36 |
|
return isset($this->field[$key]) || (is_array($this->field) && array_key_exists($key, $this->field)); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Get the error count for this field. |
229
|
|
|
*/ |
230
|
64 |
|
public function getErrorCount() { |
231
|
64 |
|
return $this->getValidation()->getErrorCount($this->getName()); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Whether or not we are validating a request. |
236
|
|
|
* |
237
|
|
|
* @return bool Returns **true** of we are validating a request or **false** otherwise. |
238
|
|
|
*/ |
239
|
121 |
|
public function isRequest(): bool { |
240
|
121 |
|
return $this->options['request'] ?? false; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Whether or not we are validating a response. |
245
|
|
|
* |
246
|
|
|
* @return bool Returns **true** of we are validating a response or **false** otherwise. |
247
|
|
|
*/ |
248
|
121 |
|
public function isResponse(): bool { |
249
|
121 |
|
return $this->options['response'] ?? false; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Whether or not this is a sparse validation.. |
254
|
|
|
* |
255
|
|
|
* @return bool Returns **true** if this is a sparse validation or **false** otherwise. |
256
|
|
|
*/ |
257
|
37 |
|
public function isSparse() { |
258
|
37 |
|
return $this->getOption('sparse', false); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Gets the options array. |
263
|
|
|
* |
264
|
|
|
* @return array Returns an options array. |
265
|
|
|
*/ |
266
|
142 |
|
public function getOptions(): array { |
267
|
142 |
|
return $this->options; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Get an indivitual option. |
272
|
|
|
* |
273
|
|
|
* @param string $option The name of the option. |
274
|
|
|
* @param mixed $default The default value to return if the option doesn't exist. |
275
|
|
|
* @return mixed Returns the option or the default value. |
276
|
|
|
*/ |
277
|
37 |
|
public function getOption(string $option, $default = null) { |
278
|
37 |
|
return $this->options[$option] ?? $default; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Get the schemaPath. |
283
|
|
|
* |
284
|
|
|
* @return string Returns the schemaPath. |
285
|
|
|
*/ |
286
|
236 |
|
public function getSchemaPath(): string { |
287
|
236 |
|
return $this->schemaPath; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Set the schemaPath. |
292
|
|
|
* |
293
|
|
|
* @param string $schemaPath |
294
|
|
|
* @return $this |
295
|
|
|
*/ |
296
|
119 |
|
public function setSchemaPath(string $schemaPath) { |
297
|
119 |
|
$this->schemaPath = ltrim($schemaPath, '/'); |
298
|
119 |
|
return $this; |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|