From f37a53cfbdafd71bc11dbf54da4499ab96af9aae Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Thu, 20 Mar 2025 08:01:50 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- src/php/access/ChainAccess.php | 35 ++++++--- src/php/access/FormAccess.php | 22 ++++-- src/php/access/KeyAccess.php | 23 ++++-- src/php/access/PropertyAccess.php | 85 +++++++++++++-------- src/schema/TODO.md | 3 - src/schema/_assoc/AssocWrapper.php | 28 ++++--- src/schema/_scalar/ScalarWrapper.php | 12 +-- src/schema/input/ChainInput.php | 5 -- src/schema/input/FormInput.php | 16 ++-- src/schema/input/GetInput.php | 6 +- src/schema/input/IInput.php | 18 ----- src/schema/input/Input.php | 36 ++++++--- src/schema/input/PostInput.php | 6 +- src/schema/types/IType.php | 4 +- src/schema/types/tarray.php | 2 +- src/schema/types/tbool.php | 2 +- src/schema/types/tcallable.php | 2 +- src/schema/types/tcontent.php | 2 +- src/schema/types/tfloat.php | 2 +- src/schema/types/tgeneric.php | 2 +- src/schema/types/tint.php | 2 +- src/schema/types/tkey.php | 2 +- src/schema/types/tmixed.php | 2 +- src/schema/types/tpkey.php | 2 +- src/schema/types/trawstring.php | 2 +- tests/wip/schema/_assoc/AssocSchemaTest.php | 27 ++++++- 26 files changed, 214 insertions(+), 134 deletions(-) delete mode 100644 src/schema/input/ChainInput.php delete mode 100644 src/schema/input/IInput.php diff --git a/src/php/access/ChainAccess.php b/src/php/access/ChainAccess.php index cd3d1cd..299c01d 100644 --- a/src/php/access/ChainAccess.php +++ b/src/php/access/ChainAccess.php @@ -9,6 +9,10 @@ use ReflectionException; class ChainAccess extends AbstractAccess { const ACCESS_AUTO = 0, ACCESS_KEY = 1, ACCESS_PROPERTY = 2; + private static function unexpected_access_type(): StateException { + return StateException::unexpected_state("access_type"); + } + function __construct(IAccess $access, $key, ?array $params=null) { parent::__construct(); $this->access = $access; @@ -18,7 +22,7 @@ class ChainAccess extends AbstractAccess { protected IAccess $access; - /** @var int|string|array */ + /** @var null|int|string|array */ protected $key; protected int $accessType; @@ -45,7 +49,7 @@ class ChainAccess extends AbstractAccess { $class = new ReflectionClass($src); return $class->hasProperty($key) || property_exists($src, $key); } else { - throw StateException::unexpected_state(); + throw self::unexpected_access_type(); } } @@ -71,7 +75,7 @@ class ChainAccess extends AbstractAccess { return $default; } } else { - throw StateException::unexpected_state(); + throw self::unexpected_access_type(); } } @@ -95,23 +99,30 @@ class ChainAccess extends AbstractAccess { } function exists(): bool { - if ($this->key === null) return false; if (!$this->access->exists()) return false; + if ($this->key === null) return true; return $this->_has(); } function available(): bool { - if ($this->key === null) return false; if (!$this->access->available()) return false; + if ($this->key === null) return true; if (!$this->_has()) return false; return $this->isAllowEmpty() || $this->_get() !== ""; } function get($default=null) { + if ($this->key === null) { + return $this->access->get($default); + } return $this->_get($default); } function set($value): void { + if ($this->key === null) { + $this->access->set($value); + return; + } $dest = $this->access->get(); $key = $this->key; $accessType = $this->_accessType($dest, $key); @@ -121,11 +132,15 @@ class ChainAccess extends AbstractAccess { } elseif ($accessType === self::ACCESS_PROPERTY) { $this->_pset($dest, $key, $value); } else { - throw StateException::unexpected_state(); + throw self::unexpected_access_type(); } } function del(): void { + if ($this->key === null) { + $this->access->del(); + return; + } $dest = $this->access->get(); $key = $this->key; $accessType = $this->_accessType($dest, $key); @@ -135,14 +150,16 @@ class ChainAccess extends AbstractAccess { } elseif ($accessType === self::ACCESS_PROPERTY) { $this->_pset($dest, $key, null); } else { - throw StateException::unexpected_state(); + throw self::unexpected_access_type(); } } function addKey($key, ?array $params=null): IAccess { - $accessType = $params["access_type"] ?? self::ACCESS_AUTO; + if ($key === null) return $this; + $accessType = $params["access_type"] ?? $this->accessType; if ($accessType === self::ACCESS_KEY && $accessType === $this->accessType) { - return new ChainAccess($this->access, cl::merge($this->key, $key)); + if ($this->key !== null) $key = cl::merge($this->key, $key); + return new ChainAccess($this->access, $key); } else { return new ChainAccess($this, $key); } diff --git a/src/php/access/FormAccess.php b/src/php/access/FormAccess.php index 92c2a8c..2368e75 100644 --- a/src/php/access/FormAccess.php +++ b/src/php/access/FormAccess.php @@ -14,12 +14,12 @@ class FormAccess extends AbstractAccess { $this->key = $key; } - /** @var int|string|array */ + /** @var null|int|string|array */ protected $key; protected function _exists(array $first, ?array $second=null): bool { $key = $this->key; - if ($key === null) return false; + if ($key === null) return true; if (cl::phas($first, $key)) return true; return $second !== null && cl::phas($second, $key); } @@ -30,7 +30,7 @@ class FormAccess extends AbstractAccess { protected function _available(array $first, ?array $second=null): bool { $key = $this->key; - if ($key === null) return false; + if ($key === null) return true; if (cl::phas($first, $key)) { return $this->allowEmpty || cl::pget($first, $key) !== ""; } elseif ($second !== null && cl::phas($second, $key)) { @@ -46,7 +46,7 @@ class FormAccess extends AbstractAccess { protected function _get($default, array $first, ?array $second=null) { $key = $this->key; - if ($key === null) return $default; + if ($key === null) return cl::merge($first, $second); if (cl::phas($first, $key)) { $value = cl::pget($first, $key); if ($value !== "" || $this->allowEmpty) return $value; @@ -63,7 +63,10 @@ class FormAccess extends AbstractAccess { function _set($value, array &$first, ?array &$second=null): void { $key = $this->key; - if ($key === null) return; + if ($key === null) { + # interdire la modification de la destination + return; + } if ($second !== null && !cl::phas($first, $key) && cl::phas($second, $key)) { cl::pset($second, $key, $value); } else { @@ -77,7 +80,10 @@ class FormAccess extends AbstractAccess { function _del(array &$first, ?array &$second=null): void { $key = $this->key; - if ($key === null) return; + if ($key === null) { + # interdire la modification de la destination + return; + } if ($second !== null && !cl::phas($first, $key) && cl::phas($second, $key)) { cl::pdel($second, $key); } else { @@ -90,7 +96,9 @@ class FormAccess extends AbstractAccess { } function addKey($key): self { - return new static(cl::merge($this->key, $key), [ + if ($key === null) return $this; + if ($this->key !== null) $key = cl::merge($this->key, $key); + return new static($key, [ "allow_empty" => $this->allowEmpty ]); } diff --git a/src/php/access/KeyAccess.php b/src/php/access/KeyAccess.php index b65b86d..38bfee5 100644 --- a/src/php/access/KeyAccess.php +++ b/src/php/access/KeyAccess.php @@ -12,15 +12,19 @@ use nulib\cl; class KeyAccess extends AbstractAccess { const ALLOW_NULL = null; const ALLOW_FALSE = null; + const PROTECT_DEST = false; function __construct(&$dest, $key=null, ?array $params=null) { parent::__construct($params); + $this->protectDest = $params["protect_dest"] ?? static::PROTECT_DEST; $this->dest =& $dest; $this->key = $key; $this->allowNull = $params["allow_null"] ?? static::ALLOW_NULL; $this->allowFalse = $params["allow_false"] ?? static::ALLOW_FALSE; } + protected bool $protectDest; + /** @var mixed|array|ArrayAccess */ protected $dest; @@ -86,21 +90,30 @@ class KeyAccess extends AbstractAccess { function set($value): void { $key = $this->key; - if ($key === null) $this->dest = $value; - else cl::pset($this->dest, $key, $value); + if ($key === null) { + if (!$this->protectDest) $this->dest = $value; + } else { + cl::pset($this->dest, $key, $value); + } } function del(): void { $key = $this->key; - if ($key === null) $this->dest = null; - else cl::pdel($this->dest, $key); + if ($key === null) { + if (!$this->protectDest) $this->dest = null; + } else { + cl::pdel($this->dest, $key); + } } function addKey($key): self { - return new KeyAccess($this->dest, cl::merge($this->key, $key), [ + if ($key === null) return $this; + if ($this->key !== null) $key = cl::merge($this->key, $key); + return new KeyAccess($this->dest, $key, [ "allow_empty" => $this->allowEmpty, "allow_null" => $this->allowNull, "allow_false" => $this->allowFalse, + "protect_dest" => $this->protectDest, ]); } } diff --git a/src/php/access/PropertyAccess.php b/src/php/access/PropertyAccess.php index d27493f..eadf80d 100644 --- a/src/php/access/PropertyAccess.php +++ b/src/php/access/PropertyAccess.php @@ -1,44 +1,65 @@ object = $object; - $this->name = $name; - $class = new ReflectionClass($object); - try { - $property = $class->getProperty($this->name); - $property->setAccessible(true); - } catch (ReflectionException $e) { - $property = null; - } - $this->property = $property; + $this->protectDest = $params["protect_dest"] ?? static::PROTECT_DEST; + $this->mapNames = $params["map_names"] ?? static::MAP_NAMES; + $this->_setName($name); + $this->_setDest($dest); $this->allowNull = $params["allow_null"] ?? static::ALLOW_NULL; $this->allowFalse = $params["allow_false"] ?? static::ALLOW_FALSE; } - protected object $object; + protected bool $protectDest; + + protected ?object $dest; + protected bool $mapNames; protected ?string $name; protected ?ReflectionProperty $property; - function reset(object $object, ?string $name=null): self { - $this->object = $object; + private function _setName(?string $name): void { + if ($this->mapNames) $name = str::us2camel($name); $this->name = $name; + } + + private function _setDest(?object $dest): void { + $this->dest = $dest; + $property = null; + $name = $this->name; + if ($dest !== null && $name !== null) { + $class = new ReflectionClass($dest); + try { + $property = $class->getProperty($name); + $property->setAccessible(true); + } catch (ReflectionException $e) { + } + } + $this->property = $property; + } + + function reset(?object $dest, ?string $name=null): self { + $this->_setName($name); + $this->_setDest($dest); return $this; } function resetKey($name=null): self { - $this->name = $name; + $this->_setName($name); return $this; } @@ -48,20 +69,23 @@ class PropertyAccess extends AbstractAccess { function exists(): bool { $name = $this->name; + if ($this->dest === null) return false; return $name === null || $this->property !== null - || property_exists($this->object, $name); + || property_exists($this->dest, $name); } protected function _get($default=null) { $name = $this->name; $property = $this->property; - if ($name === null) { - return $this->object; + if ($this->dest === null) { + return $default; + } elseif ($name === null) { + return $this->dest; } elseif ($property !== null) { - return $property->getValue($this->object); - } elseif (property_exists($this->object, $name)) { - return $this->object->$name; + return $property->getValue($this->dest); + } elseif (property_exists($this->dest, $name)) { + return $this->dest->$name; } else { return $default; } @@ -88,20 +112,14 @@ class PropertyAccess extends AbstractAccess { protected function _set($value): void { $name = $this->name; $property = $this->property; - if ($name === null) { - $this->object = $value; - $class = new ReflectionClass($value); - try { - $property = $class->getProperty($this->name); - $property->setAccessible(true); - } catch (ReflectionException $e) { - $property = null; - } - $this->property = $property; + if ($this->dest === null) { + throw StateException::unexpected_state("dest is null"); + } elseif ($name === null) { + if (!$this->protectDest) $this->_setDest($value); } elseif ($property !== null) { - $property->setValue($this->object, $value); + $property->setValue($this->dest, $value); } else { - $this->object->$name = $value; + $this->dest->$name = $value; } } @@ -114,6 +132,7 @@ class PropertyAccess extends AbstractAccess { } function addKey($key): IAccess { + if ($key === null) return $this; return new ChainAccess($this, $key); } } diff --git a/src/schema/TODO.md b/src/schema/TODO.md index 745781f..5dce1ef 100644 --- a/src/schema/TODO.md +++ b/src/schema/TODO.md @@ -1,8 +1,5 @@ # nulib\schema -* faire PropertyAccess -* si l'argument de Input() est un objet, utiliser PropertyAccess au lieu de KeyAccess - * possibilité de forcer l'un ou l'autre (paramètre type=value|array|object) * ensureKeys() et orderKeys() se fait au niveau de access (ou input?) * valeurs composite/computed diff --git a/src/schema/_assoc/AssocWrapper.php b/src/schema/_assoc/AssocWrapper.php index 5b71446..1add86c 100644 --- a/src/schema/_assoc/AssocWrapper.php +++ b/src/schema/_assoc/AssocWrapper.php @@ -57,8 +57,9 @@ class AssocWrapper extends Wrapper { else $input = $this->newInput($value); $context->input = $input; $context->valueKey = $valueKey; - foreach ($context->keyWrappers as $key => $wrapper) { - $wrapper->reset($input->subInput($valueKey), $key); + foreach ($context->keyWrappers as $key => $keyWrapper) { + $keyInput = $input->addKey($valueKey); + $keyWrapper->reset($keyInput, $key, ["analyze" => false]); } $this->afterModify($params, true); return $this; @@ -88,25 +89,34 @@ class AssocWrapper extends Wrapper { */ static function _analyze(WrapperContext $context, Wrapper $wrapper, ?array $params): int { if ($context->ensureArray) { - $array = $context->input->get(); - if ($array === null) $context->input->set([]); + $valueKey = $context->valueKey; + $array = $context->input->get($valueKey); + if ($array === null) $context->input->set([], $valueKey); } $what = ScalarWrapper::_analyze($context, $wrapper, $params); /** @var ScalarResult $result */ $result = $context->result; if (!$result->valid) return $what; - foreach ($context->keyWrappers as $wrapper) { - $wrapper->analyze($params); - if (!$wrapper->isValid()) { + foreach ($context->keyWrappers as $keyWrapper) { + $keyWrapper->analyze($params); + if (!$keyWrapper->isValid()) { $what = ref_analyze::INVALID; - $result->addInvalidMessage($wrapper); + $result->addInvalidMessage($keyWrapper); } } return $what; } + /** + * @param AssocWrapperContext $context + * @param AssocWrapper $wrapper + */ static function _normalize(WrapperContext $context, Wrapper $wrapper, ?array $params): bool { - return ScalarWrapper::_normalize($context, $wrapper, $params); + $modified = ScalarWrapper::_normalize($context, $wrapper, $params); + foreach ($context->keyWrappers as $keyWrapper) { + if ($keyWrapper->normalize($params)) $modified = true; + } + return $modified; } function getResult($key=false): Result { diff --git a/src/schema/_scalar/ScalarWrapper.php b/src/schema/_scalar/ScalarWrapper.php index f9ba7db..588e8b3 100644 --- a/src/schema/_scalar/ScalarWrapper.php +++ b/src/schema/_scalar/ScalarWrapper.php @@ -204,7 +204,7 @@ class ScalarWrapper extends Wrapper { /** @var ScalarResult $result */ $result = $context->result; - $verifix = false; + $normalize = false; $modified = false; if ($result->resultAvailable) { if ($result->null) { @@ -215,19 +215,19 @@ class ScalarWrapper extends Wrapper { $normalizedValue = $result->normalizedValue; if ($normalizedValue !== null) { # la valeur normalisée est disponible - $input->set($normalizedValue); + $input->set($normalizedValue, $valueKey); $result->normalizedValue = null; $modified = true; } else { # normaliser la valeur - $verifix = true; + $normalize = true; } } } else { - $verifix = true; + $normalize = true; } - if ($verifix) { + if ($normalize) { $value = $input->get($valueKey); /** @var func $normalizerFunc */ $normalizerFunc = $schema->normalizerFunc; @@ -236,7 +236,7 @@ class ScalarWrapper extends Wrapper { $value = $normalizerFunc->invoke([$orig, $context, $wrapper]); $modified = $value !== $orig; } else { - $modified = $context->type->verifix($value, $result, $schema); + $modified = $context->type->normalize($value, $result, $schema); } if ($result->valid) $input->set($value, $valueKey); } diff --git a/src/schema/input/ChainInput.php b/src/schema/input/ChainInput.php deleted file mode 100644 index 6e26061..0000000 --- a/src/schema/input/ChainInput.php +++ /dev/null @@ -1,5 +0,0 @@ - $this->allowEmpty, - ]); + function __construct(&$dest=null, ?array $params=null) { + parent::__construct($dest, $params); + $this->access = new ShadowAccess($this->formAccess($this->access), $this->access); } - protected function access($key): IAccess { - return $this->keyAccess[$key] ??= new ShadowAccess($this->formAccess($key), new KeyAccess($this->value, $key, [ - "allow_empty" => $this->allowEmpty, - ])); + protected function formAccess(IAccess $access): IAccess { + return new FormAccess(null, [ + "allow_empty" => $access->isAllowEmpty(), + ]); } } diff --git a/src/schema/input/GetInput.php b/src/schema/input/GetInput.php index 766c8e5..558e1ab 100644 --- a/src/schema/input/GetInput.php +++ b/src/schema/input/GetInput.php @@ -11,9 +11,9 @@ use nur\sery\wip\php\access\IAccess; * une référence */ class GetInput extends FormInput { - protected function formAccess($key): IAccess { - return new GetAccess($key, [ - "allow_empty" => $this->allowEmpty, + protected function formAccess(IAccess $access): IAccess { + return new GetAccess(null, [ + "allow_empty" => $access->isAllowEmpty(), ]); } } diff --git a/src/schema/input/IInput.php b/src/schema/input/IInput.php deleted file mode 100644 index a25bf55..0000000 --- a/src/schema/input/IInput.php +++ /dev/null @@ -1,18 +0,0 @@ -access = new PropertyAccess($dest, null, [ - "allow_empty" => $params["allow_empty"] ?? static::ALLOW_EMPTY, + "allow_empty" => $allowEmpty, + "allow_null" => true, + ]); + } elseif ($accessType == self::ACCESS_KEY) { + $this->access = new KeyAccess($dest, null, [ + "allow_empty" => $allowEmpty, "allow_null" => true, ]); } else { - $this->access = new KeyAccess($dest, null, [ - "allow_empty" => $params["allow_empty"] ?? static::ALLOW_EMPTY, - "allow_null" => true, - ]); + throw self::unexpected_access_type(); } } @@ -51,7 +66,10 @@ class Input implements IInput { $this->access->resetKey($key)->del(); } - function addKey($key): IInput { - return new ChainInput($this, $key); + function addKey($key): Input { + if ($key === null) return $this; + $input = clone $this; + $input->access = $this->access->addKey($key); + return $input; } } diff --git a/src/schema/input/PostInput.php b/src/schema/input/PostInput.php index bf720ab..9b5fef6 100644 --- a/src/schema/input/PostInput.php +++ b/src/schema/input/PostInput.php @@ -11,9 +11,9 @@ use nur\sery\wip\php\access\PostAccess; * une référence */ class PostInput extends FormInput { - protected function formAccess($key): IAccess { - return new PostAccess($key, [ - "allow_empty" => $this->allowEmpty, + protected function formAccess(IAccess $access): IAccess { + return new PostAccess(null, [ + "allow_empty" => $access->isAllowEmpty(), ]); } } diff --git a/src/schema/types/IType.php b/src/schema/types/IType.php index 36f5fd4..9dbd48a 100644 --- a/src/schema/types/IType.php +++ b/src/schema/types/IType.php @@ -23,7 +23,7 @@ interface IType { /** * @return string la classe des objets gérés par ce format: le type attendu - * par {@link format()} et le type retourné par {@link verifix()} + * par {@link format()} et le type retourné par {@link normalize()} * * Les valeurs "mixed", "bool", "float", "int", "string" et "array" peuvent * aussi être retournées, bien qu'elles ne soient pas à proprement parler des @@ -106,7 +106,7 @@ interface IType { * si la valeur était déjà normalisée, ou si une erreur s'est produite, * retourner false. */ - function verifix(&$value, Result $result, Schema $schema): bool; + function normalize(&$value, Result $result, Schema $schema): bool; /** * formatter la valeur pour affichage. si $value n'est pas null, elle est diff --git a/src/schema/types/tarray.php b/src/schema/types/tarray.php index aba2aab..b62160d 100644 --- a/src/schema/types/tarray.php +++ b/src/schema/types/tarray.php @@ -49,7 +49,7 @@ class tarray extends _tstring { * @var ScalarResult $result * @var ScalarSchema $schema */ - function verifix(&$value, Result $result, Schema $schema): bool { + function normalize(&$value, Result $result, Schema $schema): bool { if (is_array($value)) { $result->setNormalized(); return false; diff --git a/src/schema/types/tbool.php b/src/schema/types/tbool.php index 50fa206..5e0795d 100644 --- a/src/schema/types/tbool.php +++ b/src/schema/types/tbool.php @@ -99,7 +99,7 @@ class tbool extends _tformatable { * @var ScalarResult $result * @var ScalarSchema $schema */ - function verifix(&$value, Result $result, Schema $schema): bool { + function normalize(&$value, Result $result, Schema $schema): bool { if (is_bool($value)) { $result->setNormalized(); return false; diff --git a/src/schema/types/tcallable.php b/src/schema/types/tcallable.php index f9e95d6..af6be72 100644 --- a/src/schema/types/tcallable.php +++ b/src/schema/types/tcallable.php @@ -43,7 +43,7 @@ class tcallable extends _tsimple { * @var ScalarResult $result * @var ScalarSchema $schema */ - function verifix(&$value, Result $result, Schema $schema): bool { + function normalize(&$value, Result $result, Schema $schema): bool { if ($value instanceof func) { $result->setNormalized(); return false; diff --git a/src/schema/types/tcontent.php b/src/schema/types/tcontent.php index 1dfe455..2bb2912 100644 --- a/src/schema/types/tcontent.php +++ b/src/schema/types/tcontent.php @@ -36,7 +36,7 @@ abstract class tcontent extends _tunion { * @var ScalarResult $result * @var ScalarSchema $schema */ - function verifix(&$value, Result $result, Schema $schema): bool { + function normalize(&$value, Result $result, Schema $schema): bool { if (is_string($value) || is_array($value)) { $result->setNormalized(); return false; diff --git a/src/schema/types/tfloat.php b/src/schema/types/tfloat.php index db67257..f1befd1 100644 --- a/src/schema/types/tfloat.php +++ b/src/schema/types/tfloat.php @@ -43,7 +43,7 @@ class tfloat extends _tformatable { * @var ScalarResult $result * @var ScalarSchema $schema */ - function verifix(&$value, Result $result, Schema $schema): bool { + function normalize(&$value, Result $result, Schema $schema): bool { if (is_float($value)) { $result->setNormalized(); return false; diff --git a/src/schema/types/tgeneric.php b/src/schema/types/tgeneric.php index 16f86a8..492c57e 100644 --- a/src/schema/types/tgeneric.php +++ b/src/schema/types/tgeneric.php @@ -41,7 +41,7 @@ class tgeneric extends _tsimple { * @var ScalarResult $result * @var ScalarSchema $schema */ - function verifix(&$value, Result $result, Schema $schema): bool { + function normalize(&$value, Result $result, Schema $schema): bool { $result->setNormalized(); return false; } diff --git a/src/schema/types/tint.php b/src/schema/types/tint.php index 5673ab5..d7e95df 100644 --- a/src/schema/types/tint.php +++ b/src/schema/types/tint.php @@ -45,7 +45,7 @@ class tint extends _tformatable { * @var ScalarResult $result * @var ScalarSchema $schema */ - function verifix(&$value, Result $result, Schema $schema): bool { + function normalize(&$value, Result $result, Schema $schema): bool { if (is_int($value)) { $result->setNormalized(); return false; diff --git a/src/schema/types/tkey.php b/src/schema/types/tkey.php index 0e00cb3..6f5072e 100644 --- a/src/schema/types/tkey.php +++ b/src/schema/types/tkey.php @@ -36,7 +36,7 @@ class tkey extends _tunion { * @var ScalarResult $result * @var ScalarSchema $schema */ - function verifix(&$value, Result $result, Schema $schema): bool { + function normalize(&$value, Result $result, Schema $schema): bool { if (is_string($value) || is_int($value)) { $result->setNormalized(); return false; diff --git a/src/schema/types/tmixed.php b/src/schema/types/tmixed.php index fa525eb..652e51d 100644 --- a/src/schema/types/tmixed.php +++ b/src/schema/types/tmixed.php @@ -35,7 +35,7 @@ class tmixed extends _tsimple { * @var ScalarResult $result * @var ScalarSchema $schema */ - function verifix(&$value, Result $result, Schema $schema): bool { + function normalize(&$value, Result $result, Schema $schema): bool { $result->setNormalized(); return false; } diff --git a/src/schema/types/tpkey.php b/src/schema/types/tpkey.php index 14f2e01..e099051 100644 --- a/src/schema/types/tpkey.php +++ b/src/schema/types/tpkey.php @@ -41,7 +41,7 @@ class tpkey extends _tunion { * @var ScalarResult $result * @var ScalarSchema $schema */ - function verifix(&$value, Result $result, Schema $schema): bool { + function normalize(&$value, Result $result, Schema $schema): bool { if (is_string($value) || is_int($value) || is_array($value)) { $result->setNormalized(); return false; diff --git a/src/schema/types/trawstring.php b/src/schema/types/trawstring.php index 4278008..1f09dab 100644 --- a/src/schema/types/trawstring.php +++ b/src/schema/types/trawstring.php @@ -44,7 +44,7 @@ class trawstring extends _tstring { * @var ScalarResult $result * @var ScalarSchema $schema */ - function verifix(&$value, Result $result, Schema $schema): bool { + function normalize(&$value, Result $result, Schema $schema): bool { if (is_string($value)) { $result->setNormalized(); return false; diff --git a/tests/wip/schema/_assoc/AssocSchemaTest.php b/tests/wip/schema/_assoc/AssocSchemaTest.php index 0ab3763..987e845 100644 --- a/tests/wip/schema/_assoc/AssocSchemaTest.php +++ b/tests/wip/schema/_assoc/AssocSchemaTest.php @@ -120,20 +120,36 @@ class AssocSchemaTest extends TestCase { "c" => "bool", ]); + $array = ["a" => " string "]; + $schema->getWrapper($array); + self::assertSame([ + "a" => "string", + "b" => false, + "c" => null, + ], $array); + $array = ["c" => false, "a" => " string "]; $schema->getWrapper($array); self::assertSame([ "a" => "string", - "b" => null, + "b" => false, "c" => false, ], $array); + $array = ["a" => " string "]; + $schema->getWrapper($array, null, ["ensure_order" => false]); + self::assertSame([ + "a" => "string", + "b" => false, + "c" => null, + ], $array); + $array = ["c" => false, "a" => " string "]; $schema->getWrapper($array, null, ["ensure_order" => false]); self::assertSame([ "c" => false, "a" => "string", - "b" => null, + "b" => false, ], $array); $array = ["a" => " string "]; @@ -141,5 +157,12 @@ class AssocSchemaTest extends TestCase { self::assertSame([ "a" => "string", ], $array); + + $array = ["c" => false, "a" => " string "]; + $schema->getWrapper($array, null, ["ensure_keys" => false]); + self::assertSame([ + "a" => "string", + "c" => false, + ], $array); } }