From d148850c1273ebfcf006dce3e94f387dd4a978ef Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Tue, 18 Mar 2025 17:37:55 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- src/schema/Schema.php | 10 +- src/schema/TODO.md | 5 + src/schema/Wrapper.php | 139 +++++++++++++++--- src/schema/WrapperContext.php | 35 +++-- src/schema/_assoc/AssocSchema.php | 16 +- src/schema/_assoc/AssocWrapper.php | 69 +-------- src/schema/_list/ListSchema.php | 4 +- src/schema/_scalar/ScalarSchema.php | 16 +- src/schema/_scalar/ScalarWrapper.php | 101 ++++--------- tests/wip/schema/_assoc/AssocSchemaTest.php | 4 +- tests/wip/schema/_scalar/ScalarSchemaTest.php | 32 ++-- .../wip/schema/_scalar/ScalarWrapperTest.php | 12 +- 12 files changed, 223 insertions(+), 220 deletions(-) diff --git a/src/schema/Schema.php b/src/schema/Schema.php index a7721f4..4d1a422 100644 --- a/src/schema/Schema.php +++ b/src/schema/Schema.php @@ -80,7 +80,7 @@ abstract class Schema implements ArrayAccess { # schéma d'un scalaire quelconque), on ne l'autorise pas ici throw SchemaException::invalid_schema("definition is required"); } - return self::ns($schema, $definition)->getWrapper($value, $valueKey, $wrapper); + return self::ns($schema, $definition)->getWrapper($value, $valueKey, null, $wrapper); } protected static function have_nature(array $definition, ?string &$nature=null): bool { @@ -98,7 +98,7 @@ abstract class Schema implements ArrayAccess { return false; } - protected static function _normalize(&$definition, $definitionKey=null): void { + protected static function _normalize_definition(&$definition, $definitionKey=null): void { if (!is_array($definition)) $definition = [$definition]; # s'assurer que toutes les clés existent avec leur valeur par défaut $index = 0; @@ -192,11 +192,11 @@ abstract class Schema implements ArrayAccess { switch ($nature[0] ?? null) { case "assoc": foreach ($definition["schema"] as $key => &$keydef) { - self::_normalize($keydef, $key); + self::_normalize_definition($keydef, $key); }; unset($keydef); break; case "list": - self::_normalize($definition["schema"]); + self::_normalize_definition($definition["schema"]); break; } } @@ -285,7 +285,7 @@ abstract class Schema implements ArrayAccess { abstract protected function newWrapper(): Wrapper; - abstract function getWrapper(&$value=null, $valueKey=null, ?Wrapper &$wrapper=null): Wrapper; + abstract function getWrapper(&$value=null, $valueKey=null, ?array $params=null, ?Wrapper &$wrapper=null): Wrapper; ############################################################################# # key & properties diff --git a/src/schema/TODO.md b/src/schema/TODO.md index 73b4aca..b41abbe 100644 --- a/src/schema/TODO.md +++ b/src/schema/TODO.md @@ -1,5 +1,10 @@ # 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 +* ensureKeys() et orderKeys() se fait au niveau de access + * valeurs composite/computed * analyse / vérification de la valeur complète après calcul du résultat, si tous les résultats sont bons diff --git a/src/schema/Wrapper.php b/src/schema/Wrapper.php index f567702..7d6361f 100644 --- a/src/schema/Wrapper.php +++ b/src/schema/Wrapper.php @@ -6,7 +6,9 @@ use IteratorAggregate; use nulib\php\func; use nur\sery\wip\schema\_assoc\AssocWrapper; use nur\sery\wip\schema\_list\ListWrapper; +use nur\sery\wip\schema\_scalar\ScalarResult; use nur\sery\wip\schema\_scalar\ScalarWrapper; +use nur\sery\wip\schema\input\Input; use nur\sery\wip\schema\types\IType; abstract class Wrapper implements ArrayAccess, IteratorAggregate { @@ -14,8 +16,81 @@ abstract class Wrapper implements ArrayAccess, IteratorAggregate { function isList(?ListWrapper &$wrapper=null): bool { return false; } function isScalar(?ScalarWrapper &$wrapper=null): bool { return false; } - /** spécifier la valeur destination gérée par cet objet */ - abstract function reset(&$value, $valueKey=null, ?bool $verifix=null): self; + protected WrapperContext $context; + + /** changer les paramètres de gestion des valeurs */ + function resetParams(?array $params): void { + $this->context->resetParams($params); + } + + protected function afterModify(?array $params, bool $reset=true): void { + $context = $this->context; + if ($reset) { + $context->type = null; + $context->result->reset(); + $context->analyzed = false; + $context->normalized = false; + } + if ($params["analyze"] ?? $context->analyze) { + $this->analyze($params); + } + if ($context->analyzed) { + if ($params["normalize"] ?? $context->normalize) { + $this->normalize($params); + } + } + } + + protected function newInput(&$value): Input { + return new Input($value); + } + + /** + * spécifier la valeur destination gérée par cet objet. + * + * @param ?array $params paramètres spécifique à cet appel, qui peuvent être + * différent des paramètres par défaut + */ + abstract function reset(&$value, $valueKey=null, ?array $params=null): self; + + /** analyser la valeur */ + abstract protected function _analyze(?array $params): int; + + function analyze(?array $params=null): bool { + $context = $this->context; + $reanalyze = $params["reanalyze"] ?? false; + if ($context->analyzed && !$reanalyze) return false; + + $this->_analyze($params); + $context->analyzed = true; + return true; + } + + /** normaliser la valeur */ + abstract protected function _normalize(?array $params): bool; + + function normalize(?array $params=null): bool { + $context = $this->context; + + // il faut que la valeur soit analysée avant de la normaliser + $this->analyze($params); + if (!$context->analyzed) return false; + + $renormalize = $params["renormalize"] ?? false; + if ($renormalize || !$context->normalized) { + $modified = $this->_normalize($params); + $context->normalized = true; + } else { + $modified = false; + } + + /** @var ScalarResult $result */ + $result = $context->result; + if (!$result->valid) { + $result->throw($params["throw"] ?? $context->throw); + } + return $modified; + } /** * Obtenir la liste des clés valides pour les valeurs accessibles via cet @@ -39,34 +114,60 @@ abstract class Wrapper implements ArrayAccess, IteratorAggregate { } /** - * obtenir le résultat de l'appel d'une des fonctions {@link set()} ou - * {@link unset()} + * obtenir le résultat de l'analyse de la valeur du wrapper sélectionné + * + * cette fonction doit être appelée après {@link set()} ou {@link unset()} et + * après que le wrapper aie été sélectionné avec {@link select()} */ - abstract function getResult(): Result; + function getResult(): Result { + return $this->context->result; + } /** retourner true si la valeur existe */ - abstract function isPresent(): bool; + function isPresent(): bool { + return $this->context->result->present; + } /** retourner le type associé à la valeur */ - abstract function getType(): IType; + function getType(): IType { + return $this->context->type; + } /** retourner true si la valeur est disponible */ - abstract function isAvailable(): bool; + function isAvailable(): bool { + return $this->context->result->available; + } /** retourner true si la valeur est valide */ - abstract function isValid(): bool; + function isValid(): bool { + return $this->context->result->valid; + } /** retourner true si la valeur est dans sa forme normalisée */ - abstract function isNormalized(): bool; + function isNormalized(): bool { + return $this->context->result->normalized; + } - /** obtenir la valeur */ - abstract function get($default=null); - /** remplacer la valeur */ - abstract function set($value): self; + function get($default=null) { + $context = $this->context; + if ($context->result->available) return $context->input->get($context->valueKey); + else return $default; + } - /** supprimer la valeur */ - abstract function unset(): self; + function set($value, ?array $params=null): self { + $context = $this->context; + $context->input->set($value, $context->valueKey); + $this->afterModify($params); + return $this; + } + + function unset(?array $params=null): self { + $context = $this->context; + $context->input->unset($context->valueKey); + $this->afterModify($params); + return $this; + } protected function _format(WrapperContext $context, $format=null): string { $value = $context->input->get($context->valueKey); @@ -85,7 +186,9 @@ abstract class Wrapper implements ArrayAccess, IteratorAggregate { } /** formatter la valeur pour affichage */ - abstract function format($format=null): string; + function format($format=null): string { + return $this->_format($this->context, $format); + } ############################################################################# # key & properties @@ -95,7 +198,7 @@ abstract class Wrapper implements ArrayAccess, IteratorAggregate { } function offsetGet($offset) { - return $this->select($offset); + return $this->select($offset)->get(); } function offsetSet($offset, $value): void { diff --git a/src/schema/WrapperContext.php b/src/schema/WrapperContext.php index 440b8c6..6e523e4 100644 --- a/src/schema/WrapperContext.php +++ b/src/schema/WrapperContext.php @@ -5,39 +5,44 @@ use nur\sery\wip\schema\input\Input; use nur\sery\wip\schema\types\IType; class WrapperContext { - function __construct(Schema $schema, ?Input $input, $valueKey, ?Result $result, ?array $params) { - $this->params = $params; - $this->verifix = $params["verifix"] ?? true; - $this->throw = $params["throw"] ?? null; - + function __construct(Schema $schema, ?Input $input, $valueKey, ?array $params) { + $this->resetParams($params); $this->schema = $schema; - //$this->wrapper = $wrapper; if ($input !== null) $this->input = $input; $this->valueKey = $valueKey; - $this->type = null; - if ($result !== null) $this->result = $result; $this->origValue = null; $this->value = null; + $this->type = null; + $this->result = null; } public ?array $params; - public bool $verifix; + public bool $analyze, $analyzed; + public bool $normalize, $normalized; public ?bool $throw; + function resetParams(?array $params): void { + $this->params = $params; + $this->analyze = $params["analyze"] ?? true; + $this->normalize = $params["normalize"] ?? true; + $this->throw = $params["throw"] ?? true; + } + /** schéma de la valeur */ public Schema $schema; - /** instance de Wrapper associé à ce contexte */ - //public Wrapper $wrapper; /** source et destination de la valeur */ public Input $input; /** @var string|int|null clé de la valeur dans le tableau destination */ public $valueKey; - /** type de la valeur après analyse */ - public ?IType $type; - /** résultat de l'analyse de la valeur */ - public Result $result; /** @var mixed */ public $origValue; /** @var mixed */ public $value; + + /** @var string|int|null clé sélectionnée */ + public $selectedKey; + /** type de la valeur de la clé sélectionnée après analyse */ + public ?IType $type; + /** résultat de l'analyse de la valeur de la clé sélectionnée */ + public ?Result $result; } diff --git a/src/schema/_assoc/AssocSchema.php b/src/schema/_assoc/AssocSchema.php index 017a6fd..d7bb88e 100644 --- a/src/schema/_assoc/AssocSchema.php +++ b/src/schema/_assoc/AssocSchema.php @@ -4,7 +4,6 @@ namespace nur\sery\wip\schema\_assoc; use nulib\cl; use nulib\ref\schema\ref_schema; use nulib\ValueException; -use nur\sery\wip\schema\_scalar\ScalarWrapper; use nur\sery\wip\schema\Schema; use nur\sery\wip\schema\Wrapper; @@ -17,7 +16,7 @@ class AssocSchema extends Schema { /** * indiquer si $definition est une définition de schéma de nature tableau - * associatif que {@link normalize()} pourrait normaliser + * associatif que {@link normalize_definition()} pourrait normaliser */ static function isa_definition($definition): bool { if (!is_array($definition)) return false; @@ -29,7 +28,7 @@ class AssocSchema extends Schema { return !cl::have_num_keys($definition); } - static function normalize($definition, $definitionKey=null): array { + static function normalize_definition($definition, $definitionKey=null): array { if (!is_array($definition)) $definition = [$definition]; if (!self::have_nature($definition)) { $definition = [ @@ -38,7 +37,7 @@ class AssocSchema extends Schema { "schema" => $definition, ]; } - self::_normalize($definition, $definitionKey); + self::_normalize_definition($definition, $definitionKey); self::_ensure_nature($definition, "assoc", "array"); return $definition; } @@ -46,7 +45,7 @@ class AssocSchema extends Schema { function __construct($definition=null, $definitionKey=null, bool $normalize=true) { if ($definition === null) $definition = static::SCHEMA; if ($normalize) { - $definition = self::normalize($definition, $definitionKey); + $definition = self::normalize_definition($definition, $definitionKey); $this->_definition = $definition; self::_ensure_type($definition); self::_ensure_schema_instances($definition); @@ -81,11 +80,12 @@ class AssocSchema extends Schema { return new AssocWrapper($this); } - function getWrapper(&$array=null, $arrayKey=null, ?Wrapper &$wrapper=null): AssocWrapper { + function getWrapper(&$value=null, $valueKey=null, ?array $params=null, ?Wrapper &$wrapper=null): AssocWrapper { # si pas de valeur ni de wrapper, pas de vérification et donc pas d'exception # cf le code similaire dans ScalarWrapper::__construct() - $verifix = $array !== null || $wrapper !== null; + $verifix = $value !== null || $wrapper !== null; if (!($wrapper instanceof AssocWrapper)) $wrapper = $this->newWrapper(); - return $wrapper->reset($array, $arrayKey, $verifix); + if ($params !== null) $wrapper->resetParams($params); + return $wrapper->reset($value, $valueKey, $verifix); } } diff --git a/src/schema/_assoc/AssocWrapper.php b/src/schema/_assoc/AssocWrapper.php index 5ce88b1..f7a3a38 100644 --- a/src/schema/_assoc/AssocWrapper.php +++ b/src/schema/_assoc/AssocWrapper.php @@ -10,7 +10,6 @@ use nur\sery\wip\schema\WrapperContext; class AssocWrapper extends Wrapper { function __construct(AssocSchema $schema, &$array=null, $arrayKey=null, ?array $params=null) { - $definitionSchema = $schema->getDefinition()["schema"]; $keys = $schema->getKeys(); $keyTypes = []; $keyWrappers = []; @@ -18,7 +17,7 @@ class AssocWrapper extends Wrapper { $keyTypes[$key] = null; $keyWrappers[$key] = $schema->getSchema($key)->getWrapper(); } - $this->context = $context = new AssocWrapperContext($schema, null, null, null, $params); + $this->context = $context = new AssocWrapperContext($schema, null, null, $params); $context->arrayResult = new ScalarResult(); $context->keys = $keys; $context->keyTypes = $keyTypes; @@ -41,18 +40,13 @@ class AssocWrapper extends Wrapper { /** @var AssocWrapperContext */ protected WrapperContext $context; - protected function newInput(&$value): Input { - return new Input($value); - } - - function reset(&$array, $arrayKey=null, ?bool $verifix=null): Wrapper { + function reset(&$array, $arrayKey=null, ?array $params=null): Wrapper { if ($array instanceof Input) $input = $array; else $input = $this->newInput($array); $context = $this->context; $context->input = $input; $context->valueKey = $arrayKey; - $this->analyze(); - if ($verifix ?? $context->verifix) $this->verifix(); + $this->afterModify($params); return $this; } @@ -62,72 +56,17 @@ class AssocWrapper extends Wrapper { /** @param string|int|null $key */ function select($key=null): Wrapper { + #XXX il faut que context contiennent les informations pour la clé sélectionnée if ($key === null) return $this; $wrapper = $this->context->keyWrappers[$key] ?? null; if ($wrapper === null) throw ValueException::invalid_key($key); return $wrapper; } - protected function analyze(): int { - return 0; #XXX - } - - function verifix(?bool $throw=null): bool { - return false; #XXX - } - - function getResult(): AssocResult { - /** @var AssocResult $result */ - $result = $this->context->result; - return $result; - } - - function isPresent(): bool { - return $this->context->result->present; - } - - function getType(): IType { - return $this->context->type; - } - - function isAvailable(): bool { - return $this->context->result->available; - } - - function isValid(): bool { - return $this->context->result->valid; - } - function isNormalized(): bool { return $this->context->result->normalized; } - function get($default=null) { - $context = $this->context; - if ($context->result->available) return $context->input->get($context->valueKey); - else return $default; - } - - function set($value, ?bool $verifix=null): AssocWrapper { - $context = $this->context; - $context->input->set($value, $context->valueKey); - $this->analyze(); - if ($verifix ?? $context->verifix) $this->verifix(); - return $this; - } - - function unset(?bool $verifix=null): AssocWrapper { - $context = $this->context; - $context->input->unset($context->valueKey); - $this->analyze(); - if ($verifix ?? $context->verifix) $this->verifix(); - return $this; - } - - function format($format = null): string { - return $this->_format($this->context, $format); - } - function ensureKeys(): bool { } function orderKeys(): bool { diff --git a/src/schema/_list/ListSchema.php b/src/schema/_list/ListSchema.php index 24dadda..4887e95 100644 --- a/src/schema/_list/ListSchema.php +++ b/src/schema/_list/ListSchema.php @@ -35,7 +35,7 @@ class ListSchema extends Schema { "schema" => $definition[0], ]; } - self::_normalize($definition, $definitionKey); + self::_normalize_definition($definition, $definitionKey); self::_ensure_nature($definition, "list", "array"); return $definition; } @@ -71,7 +71,7 @@ class ListSchema extends Schema { return new ListWrapper($this); } - function getWrapper(&$value=null, $valueKey=null, ?Wrapper &$wrapper=null): ListWrapper { + function getWrapper(&$value=null, $valueKey=null, ?array $params = null, ?Wrapper &$wrapper=null): ListWrapper { # si pas de valeur ni de wrapper, pas de vérification et donc pas d'exception # cf le code similaire dans ScalarWrapper::__construct() $verifix = $value !== null || $wrapper !== null; diff --git a/src/schema/_scalar/ScalarSchema.php b/src/schema/_scalar/ScalarSchema.php index 8ff264b..1621e5b 100644 --- a/src/schema/_scalar/ScalarSchema.php +++ b/src/schema/_scalar/ScalarSchema.php @@ -4,7 +4,6 @@ namespace nur\sery\wip\schema\_scalar; use nulib\ref\schema\ref_schema; use nulib\ValueException; use nur\sery\wip\schema\Schema; -use nur\sery\wip\schema\types\IType; use nur\sery\wip\schema\Wrapper; /** @@ -16,7 +15,7 @@ class ScalarSchema extends Schema { /** * indiquer si $definition est une définition de schéma scalaire que - * {@link normalize()} pourrait normaliser + * {@link normalize_definition()} pourrait normaliser */ static function isa_definition($definition): bool { # chaine ou null @@ -45,8 +44,8 @@ class ScalarSchema extends Schema { return $haveIndex0 && $count > 1; } - static function normalize($definition, $definitionKey=null): array { - self::_normalize($definition, $definitionKey); + static function normalize_definition($definition, $definitionKey=null): array { + self::_normalize_definition($definition, $definitionKey); self::_ensure_nature($definition, "scalar"); return $definition; } @@ -54,7 +53,7 @@ class ScalarSchema extends Schema { function __construct($definition=null, $definitionKey=null, bool $normalize=true) { if ($definition === null) $definition = static::SCHEMA; if ($normalize) { - $definition = self::normalize($definition, $definitionKey); + $definition = self::normalize_definition($definition, $definitionKey); $this->_definition = $definition; self::_ensure_type($definition); self::_ensure_schema_instances($definition); @@ -82,11 +81,12 @@ class ScalarSchema extends Schema { return new ScalarWrapper($this); } - function getWrapper(&$value=null, $valueKey=null, ?Wrapper &$wrapper=null): ScalarWrapper { + function getWrapper(&$value=null, $valueKey=null, ?array $params=null, ?Wrapper &$wrapper=null): ScalarWrapper { # si pas de valeur ni de wrapper, pas de vérification et donc pas d'exception # cf le code similaire dans ScalarWrapper::__construct() - $verifix = $value !== null || $wrapper !== null; + $dontNormalize = $value === null && $wrapper === null; if (!($wrapper instanceof ScalarWrapper)) $wrapper = $this->newWrapper(); - return $wrapper->reset($value, $valueKey, $verifix); + if ($params !== null) $wrapper->resetParams($params); + return $wrapper->reset($value, $valueKey, $dontNormalize? ["normalize" => false]: null); } } diff --git a/src/schema/_scalar/ScalarWrapper.php b/src/schema/_scalar/ScalarWrapper.php index 3ff48fb..3a33684 100644 --- a/src/schema/_scalar/ScalarWrapper.php +++ b/src/schema/_scalar/ScalarWrapper.php @@ -4,22 +4,31 @@ namespace nur\sery\wip\schema\_scalar; use nulib\php\func; use nulib\ref\schema\ref_analyze; use nulib\ValueException; -use nur\sery\wip\schema\WrapperContext; use nur\sery\wip\schema\input\Input; use nur\sery\wip\schema\types; use nur\sery\wip\schema\types\IType; use nur\sery\wip\schema\Wrapper; +use nur\sery\wip\schema\WrapperContext; +/** + * Class ScalarWrapper + * + * @method ScalarResult getResult() + * @method self set() + * @method self unset() + */ class ScalarWrapper extends Wrapper { function __construct(ScalarSchema $schema, &$value=null, $valueKey=null, ?array $params=null) { - $this->context = $context = new WrapperContext($schema, null, null, new ScalarResult(), $params); + $this->context = $context = new WrapperContext($schema, null, null, $params); + $context->result = new ScalarResult(); - $throw = $context->throw; - if ($value !== null && $throw === null) { - # Si $value est null, ne pas lancer d'exception, parce qu'on considère que - # c'est une initialisation sans conséquences - $throw = true; - } + # calculer manuellemet throw ici parce que WrapperContext le met à true par + # défaut. on veut pouvoir mettre temporairement throw à false si jamais il + # n'est pas spécifié par l'utilisateur + $throw = $params["throw"] ?? null; + # Si $value est null, ne pas lancer d'exception, parce qu'on considère que + # c'est une initialisation sans conséquences + if ($throw === null && $value !== null) $throw = true; $context->throw = $throw ?? false; $this->reset($value, $valueKey); $context->throw = $throw ?? true; @@ -29,19 +38,13 @@ class ScalarWrapper extends Wrapper { protected WrapperContext $context; - protected function newInput(&$value): Input { - return new Input($value); - } - - function reset(&$value, $valueKey=null, ?bool $verifix=null): Wrapper { + function reset(&$value, $valueKey=null, ?array $params=null): Wrapper { + $context = $this->context; if ($value instanceof Input) $input = $value; else $input = $this->newInput($value); - $context = $this->context; $context->input = $input; $context->valueKey = $valueKey; - $context->type = null; - $this->analyze(); - if ($verifix ?? $context->verifix) $this->verifix(); + $this->afterModify($params); return $this; } @@ -56,7 +59,7 @@ class ScalarWrapper extends Wrapper { } /** analyser la valeur et résoudre son type */ - protected function analyze0(): int { + protected function _analyze0(): int { $context = $this->context; /** @var ScalarSchema $schema */ $schema = $context->schema; @@ -123,7 +126,7 @@ class ScalarWrapper extends Wrapper { $type = $firstType; } } - $context->type = $this->type = $type; + $context->type = $type; if (!$type->isAvailable($input, $valueKey)) { if ($default !== null) { @@ -148,7 +151,7 @@ class ScalarWrapper extends Wrapper { } } - protected function analyze(): int { + protected function _analyze(?array $params): int { $context = $this->context; /** @var ScalarSchema $schema */ $schema = $context->schema; @@ -156,12 +159,11 @@ class ScalarWrapper extends Wrapper { $valueKey = $context->valueKey; /** @var ScalarResult $result */ $result = $context->result; - $result->reset(); /** @var func $analyzerFunc */ $analyzerFunc = $schema->analyzerFunc; if ($analyzerFunc !== null) $what = $analyzerFunc->invoke([$context, $this]); - else $what = $this->analyze0(); + else $what = $this->_analyze0(); if ($what !== ref_analyze::STRING) return $what; $value = $context->value; @@ -196,7 +198,7 @@ class ScalarWrapper extends Wrapper { } } - function verifix(?bool $throw=null): bool { + protected function _normalize(?array $params): bool { $context = $this->context; /** @var ScalarSchema $schema */ $schema = $context->schema; @@ -241,59 +243,6 @@ class ScalarWrapper extends Wrapper { } if ($result->valid) $input->set($value, $valueKey); } - if (!$result->valid) $result->throw($throw ?? $context->throw); return $modified; } - - function getResult(): ScalarResult { - /** @var ScalarResult $result */ - $result = $this->context->result; - return $result; - } - - function isPresent(): bool { - return $this->context->result->present; - } - - function getType(): IType { - return $this->context->type; - } - - function isAvailable(): bool { - return $this->context->result->available; - } - - function isValid(): bool { - return $this->context->result->valid; - } - - function isNormalized(): bool { - return $this->context->result->normalized; - } - - function get($default=null) { - $context = $this->context; - if ($context->result->available) return $context->input->get($context->valueKey); - else return $default; - } - - function set($value, ?bool $verifix=null): ScalarWrapper { - $context = $this->context; - $context->input->set($value, $context->valueKey); - $this->analyze(); - if ($verifix ?? $context->verifix) $this->verifix(); - return $this; - } - - function unset(?bool $verifix=null): ScalarWrapper { - $context = $this->context; - $context->input->unset($context->valueKey); - $this->analyze(); - if ($verifix ?? $context->verifix) $this->verifix(); - return $this; - } - - function format($format=null): string { - return $this->_format($this->context, $format); - } } diff --git a/tests/wip/schema/_assoc/AssocSchemaTest.php b/tests/wip/schema/_assoc/AssocSchemaTest.php index 8f78dd4..384f5d6 100644 --- a/tests/wip/schema/_assoc/AssocSchemaTest.php +++ b/tests/wip/schema/_assoc/AssocSchemaTest.php @@ -44,7 +44,7 @@ class AssocSchemaTest extends TestCase { "type" => ["string"], "nullable" => false, "name" => "a", "pkey" => "a", "header" => "a", ], - ]), AssocSchema::normalize(["a" => "string"])); + ]), AssocSchema::normalize_definition(["a" => "string"])); self::assertSame(self::schema([ "type" => ["array"], "nullable" => true, @@ -61,7 +61,7 @@ class AssocSchemaTest extends TestCase { "type" => ["bool"], "nullable" => false, "name" => "c", "pkey" => "c", "header" => "c", ], - ]), AssocSchema::normalize([ + ]), AssocSchema::normalize_definition([ "a" => "string", "b" => "int", "c" => "bool", diff --git a/tests/wip/schema/_scalar/ScalarSchemaTest.php b/tests/wip/schema/_scalar/ScalarSchemaTest.php index c862fa5..a5b6cee 100644 --- a/tests/wip/schema/_scalar/ScalarSchemaTest.php +++ b/tests/wip/schema/_scalar/ScalarSchemaTest.php @@ -32,33 +32,33 @@ class ScalarSchemaTest extends TestCase { } function testNormalize() { - self::assertSame(self::NULL_SCHEMA, ScalarSchema::normalize(null)); - self::assertSame(self::NULL_SCHEMA, ScalarSchema::normalize([])); - self::assertSame(self::NULL_SCHEMA, ScalarSchema::normalize([null])); + self::assertSame(self::NULL_SCHEMA, ScalarSchema::normalize_definition(null)); + self::assertSame(self::NULL_SCHEMA, ScalarSchema::normalize_definition([])); + self::assertSame(self::NULL_SCHEMA, ScalarSchema::normalize_definition([null])); self::assertException(SchemaException::class, function () { - ScalarSchema::normalize([[]]); + ScalarSchema::normalize_definition([[]]); }); self::assertException(SchemaException::class, function () { - ScalarSchema::normalize([[null]]); + ScalarSchema::normalize_definition([[null]]); }); $string = self::schema(["type" => ["string"], "nullable" => false]); - self::assertSame($string, ScalarSchema::normalize("string")); - self::assertSame($string, ScalarSchema::normalize(["string"])); + self::assertSame($string, ScalarSchema::normalize_definition("string")); + self::assertSame($string, ScalarSchema::normalize_definition(["string"])); $nstring = self::schema(["type" => ["string"]]); - self::assertSame($nstring, ScalarSchema::normalize(["?string"])); - self::assertSame($nstring, ScalarSchema::normalize(["?string|null"])); - self::assertSame($nstring, ScalarSchema::normalize(["string|null"])); - self::assertSame($nstring, ScalarSchema::normalize([["?string", "null"]])); - self::assertSame($nstring, ScalarSchema::normalize([["string", "null"]])); - self::assertSame($nstring, ScalarSchema::normalize([["string", null]])); + self::assertSame($nstring, ScalarSchema::normalize_definition(["?string"])); + self::assertSame($nstring, ScalarSchema::normalize_definition(["?string|null"])); + self::assertSame($nstring, ScalarSchema::normalize_definition(["string|null"])); + self::assertSame($nstring, ScalarSchema::normalize_definition([["?string", "null"]])); + self::assertSame($nstring, ScalarSchema::normalize_definition([["string", "null"]])); + self::assertSame($nstring, ScalarSchema::normalize_definition([["string", null]])); $key = self::schema(["type" => ["string", "int"], "nullable" => false]); - self::assertSame($key, ScalarSchema::normalize("string|int")); + self::assertSame($key, ScalarSchema::normalize_definition("string|int")); $nkey = self::schema(["type" => ["string", "int"], "nullable" => true]); - self::assertSame($nkey, ScalarSchema::normalize("?string|int")); - self::assertSame($nkey, ScalarSchema::normalize("string|?int")); + self::assertSame($nkey, ScalarSchema::normalize_definition("?string|int")); + self::assertSame($nkey, ScalarSchema::normalize_definition("string|?int")); } } diff --git a/tests/wip/schema/_scalar/ScalarWrapperTest.php b/tests/wip/schema/_scalar/ScalarWrapperTest.php index 6e9326b..a78e548 100644 --- a/tests/wip/schema/_scalar/ScalarWrapperTest.php +++ b/tests/wip/schema/_scalar/ScalarWrapperTest.php @@ -15,19 +15,21 @@ class ScalarWrapperTest extends TestCase { self::assertSame($normalized, $wrapper->isNormalized(), "normalized"); } - function checkVerifix(ScalarSchema $schema, $orig, bool $verifix, $value, bool $present, bool $available, bool $valid, bool $normalized, ?array $inputParams=null): void { + function checkVerifix(ScalarSchema $schema, $orig, bool $normalize, $value, bool $present, bool $available, bool $valid, bool $normalized, ?array $inputParams=null): void { $wrapper = $schema->getWrapper(); + $wrapper->resetParams(["normalize" => $normalize]); if ($inputParams !== null) $input = new Input($orig, $inputParams); else $input = $orig; - $wrapper->reset($input, null, $verifix); + $wrapper->reset($input); $this->checkValue($wrapper, $value, $present, $available, $valid, $normalized); } - function checkException(ScalarSchema $schema, $orig, bool $verifix, string $exceptionClass, ?array $inputParams=null) { + function checkException(ScalarSchema $schema, $orig, bool $normalize, string $exceptionClass, ?array $inputParams=null) { $wrapper = $schema->getWrapper(); if ($inputParams !== null) $orig = new Input($orig, $inputParams); - self::assertException($exceptionClass, function() use ($wrapper, &$orig, $verifix) { - $wrapper->reset($orig, null, $verifix); + self::assertException($exceptionClass, function() use ($wrapper, &$orig, $normalize) { + $wrapper->resetParams(["normalize" => $normalize]); + $wrapper->reset($orig); }); }