You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
protected$empty_delete = true; # odd one out - normally defaults are false
97
+
protected$empty_delete = true; # odd one out - normally defaults are false/negative/null
98
98
protected$empty_null = null;
99
99
protected$prefix = '';
100
100
protected$remove_extra = false;
@@ -185,7 +185,8 @@ public function specs() {
185
185
186
186
187
187
/**
188
-
* Validates the given associative array.
188
+
* Validates the given associative array and returns the validated result.
189
+
* The result may be mutated depending on the options and specs used.
189
190
*
190
191
* @param array $args associative array
191
192
* @return array
@@ -254,7 +255,8 @@ public function validate(array $args) {
254
255
255
256
256
257
/**
257
-
* Validates a plain positional array of arguments.
258
+
* Validates a plain positional array of arguments and returns the validated result.
259
+
* The result may be mutated depending on the options and specs used.
258
260
* Since all PHP arrays are really associative, this function reindexes the args and the specs.
259
261
* Because of this, you can use still use strings for keys in either the args or the specs.
260
262
*
@@ -263,28 +265,49 @@ public function validate(array $args) {
263
265
* @throws ValidationException
264
266
*/
265
267
publicfunctionvalidate_pos(array$args) {
268
+
$args = array_values($args); # this make sure that args is a sequential numerically indexed array.
266
269
$specs = $this->specs();
267
270
if ($specs) {
268
271
$specs = array_values($this->specs()->toArray()); # make sure that specs is a sequential numerically indexed array.
269
-
}
270
-
$args = array_values($args); # this make sure that args is a sequential numerically indexed array.
271
-
foreach ($argsas$k => &$v) {
272
-
if ($this->empty_null && is_string($v) && !strlen($v)) {
273
-
$v = null;
272
+
$count_args = count($args);
273
+
$count_specs = count($specs);
274
+
275
+
# Handle too many arguments
276
+
if ($count_args > $count_specs) {
277
+
if (!$this->allow_extra && !$this->remove_extra) {
278
+
thrownewValidationException('Too many arguments given (' . $count_args . ') for the number of specs (' . $count_specs . ')');
279
+
#throw new ValidationException('Unexpected parameter at index ' . $this->prefix . $k);
280
+
}
281
+
if ($this->remove_extra) {
282
+
array_splice($args, $count_specs);
283
+
}
274
284
}
275
-
$spec = $specs && (is_array($specs) ? array_key_exists($k, $specs) : $specs->offsetExists($k)) ? $specs[$k] : null; # array_key_exists does not work with ArrayAccess objects yet. Perhaps in the future it will.
276
-
if (!$spec) {
277
-
# note: remove_extra doesn't apply to positional arrays
278
-
if (!$this->allow_extra) {
279
-
thrownewValidationException('Unexpected parameter at index ' . $this->prefix . $k);
285
+
}
286
+
287
+
# Convert empty strings to null values if so wanted.
288
+
if ($this->empty_null) {
289
+
foreach ($argsas$k => &$v) {
290
+
if (is_string($v) && !strlen($v)) {
291
+
$v = null;
280
292
}
281
-
continue;
293
+
unset($v);
282
294
}
283
-
if (!$spec->validate($v)) { # also applies before/after mutators to $v reference
0 commit comments