Skip to content
This repository was archived by the owner on Dec 19, 2022. It is now read-only.

Commit e2f0397

Browse files
author
Seif Kamal
committed
Simplify validation logic
1 parent a8384b8 commit e2f0397

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/Validator.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,31 @@ public static function validate(array &$data, Struct $struct): bool
2424
$value = array_key_exists($field, $data)
2525
? $data[$field]
2626
: Missing::property($field);
27-
$valueIsMissing = is_a($value, Missing::class);
27+
$propertyIsMissing = is_a($value, Missing::class);
2828

2929
if (is_callable($validator)) {
3030
if (!$validator($value)) {
3131
throw new Exception\InvalidValueException($field);
3232
}
3333
// If value has changed, set corresponding data field
34-
if ($valueIsMissing && !is_a($value, Missing::class)) {
34+
if ($propertyIsMissing && !is_a($value, Missing::class)) {
3535
$data[$field] = $value;
3636
}
37-
} elseif (is_a($validator, Struct::class)) {
38-
if ($valueIsMissing) {
39-
throw new Exception\MissingPropertyException($field);
40-
}
37+
return true;
38+
}
39+
40+
if ($propertyIsMissing) {
41+
throw new Exception\MissingPropertyException($field);
42+
}
43+
44+
if (is_a($validator, Struct::class)) {
4145
if (!validate($value, $validator)) {
4246
throw new Exception\InvalidValueException($field);
4347
}
44-
} else {
45-
throw new Exception\InvalidValidatorException($validator);
48+
return true;
4649
}
50+
51+
throw new Exception\InvalidValidatorException($validator);
4752
}
4853
} catch (\Throwable $t) {
4954
/**

0 commit comments

Comments
 (0)