Skip to content

Commit 0dee64c

Browse files
Quick bug-fix of "null" values for normalizers (which was not working properly before)
1 parent 2589fa8 commit 0dee64c

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

src/PHP.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static function frameworkDir() {
106106
*/
107107
public static function simpUtilsVersion(): Version|string {
108108
$class = static::redef(Version::class);
109-
return new $class('1.0.4', 'SimpUtils');
109+
return new $class('1.0.5', 'SimpUtils');
110110
}
111111

112112
/**

src/generic/BasicValidator.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,35 @@
33
namespace spaf\simputils\generic;
44

55
use spaf\simputils\interfaces\ValidatorInterface;
6+
use function is_null;
67

78
/**
89
* @codeCoverageIgnore
910
*/
1011
abstract class BasicValidator extends SimpleObject implements ValidatorInterface {
1112

12-
13+
/**
14+
* Wrapper method that is used to permit null without processing
15+
*
16+
* For example if "null|..." or "?..." specified.
17+
*
18+
* NOTE Really important to note that in case of "not-allowed", it means that
19+
* null value goes directly to the "process" method. And then the exact
20+
* normalizer has to deal with it.
21+
*
22+
* TODO One of the reasons why it's done like this, that the registry with methods
23+
* attached to properties do not have stored additional arguments from
24+
* "properties" attributes pre-processing. When more advanced reference
25+
* storage will be built, this could be done differently.
26+
*
27+
* @param mixed $value
28+
*
29+
* @return mixed|null
30+
*/
31+
static function whenNullAllowed(mixed $value): mixed {
32+
if (is_null($value)) {
33+
return null;
34+
}
35+
return static::process($value);
36+
}
1337
}

src/traits/PropertiesTrait.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -277,16 +277,19 @@ private function _simpUtilsGetValidator($item, $attr, $call_type): ?Closure {
277277
$attr_instance = $attr->newInstance();
278278
$valid = $attr_instance?->valid ?? false;
279279

280+
$t = null;
281+
if ($item instanceof ReflectionProperty) {
282+
$t = $item?->getType();
283+
} else if ($item instanceof ReflectionMethod) {
284+
$param = $item->getParameters()[0] ?? null;
285+
/** @var \ReflectionParameter $param */
286+
$t = $param?->getType();
287+
}
288+
$_method = $t && $t->allowsNull()?'whenNullAllowed':'process';
289+
280290
if ($valid === true &&
281291
$validators_enabled === CommonMemoryCacheIndex::PROPERTY_VALIDATOR_ENABLED
282292
) {
283-
if ($item instanceof ReflectionProperty) {
284-
$t = $item?->getType();
285-
} else if ($item instanceof ReflectionMethod) {
286-
$param = $item->getParameters()[0] ?? null;
287-
/** @var \ReflectionParameter $param */
288-
$t = $param?->getType();
289-
}
290293
if ($t instanceof ReflectionUnionType) {
291294
// NOTE Union-Types are not supported due to unpredictable nature
292295
return null; // @codeCoverageIgnore
@@ -304,18 +307,18 @@ private function _simpUtilsGetValidator($item, $attr, $call_type): ?Closure {
304307
}
305308
}
306309
if (!empty($validators[$class]) && PHP::isClass($validators[$class])) {
307-
$closure = Closure::fromCallable([$validators[$class], 'process']);
310+
$closure = Closure::fromCallable([$validators[$class], $_method]);
308311
return $closure;
309312
}
310313
}
311314
} else if (is_string($valid) &&
312315
$validators_enabled >= CommonMemoryCacheIndex::PROPERTY_VALIDATOR_LIMITED
313316
) {
314317
if (!empty($validators[$valid])) {
315-
$closure = Closure::fromCallable([$validators[$valid], 'process']);
318+
$closure = Closure::fromCallable([$validators[$valid], $_method]);
316319
return $closure;
317320
} else if (PHP::isClass($valid)) {
318-
$closure = Closure::fromCallable([$valid, 'process']);
321+
$closure = Closure::fromCallable([$valid, $_method]);
319322
return $closure;
320323
}
321324
}

0 commit comments

Comments
 (0)