diff --git a/src/schema/Result.php b/src/schema/Result.php index 2a63685..3ccb261 100644 --- a/src/schema/Result.php +++ b/src/schema/Result.php @@ -1,14 +1,35 @@ reset(); } @@ -23,8 +44,21 @@ abstract class Result { */ abstract function getKeys(): array; - /** obtenir un objet pour gérer la valeur spécifiée */ - abstract function getResult($key=null): Result; + /** + * sélectionner le résultat associé à la clé spécifiée + * + * @param string|int|null $key + * @return Result $this + */ + abstract function select($key): Result; + function getIterator() { + foreach ($this->getKeys() as $key) { + yield $key => $this->select($key); + } + $this->select(null); + } + + /** réinitialiser tous les objets résultats accessibles via cet objet */ abstract function reset(): void; } diff --git a/src/schema/Wrapper.php b/src/schema/Wrapper.php index f373213..d8cafed 100644 --- a/src/schema/Wrapper.php +++ b/src/schema/Wrapper.php @@ -22,13 +22,19 @@ abstract class Wrapper implements ArrayAccess, IteratorAggregate { */ abstract function getKeys(): array; - /** obtenir un objet pour gérer la valeur spécifiée */ - abstract function getWrapper($key=null): Wrapper; + /** + * sélectionner le wrapper associé à la clé spécifiée + * + * @param string|int|null $key + * @return Wrapper $this + */ + abstract function select($key): Wrapper; function getIterator() { foreach ($this->getKeys() as $key) { - yield $key => $this->getWrapper($key); + yield $key => $this->select($key); } + $this->select(null); } /** @@ -72,14 +78,14 @@ abstract class Wrapper implements ArrayAccess, IteratorAggregate { } function offsetGet($offset) { - return $this->getWrapper($offset); + return $this->select($offset); } function offsetSet($offset, $value): void { - $this->getWrapper($offset)->set($value); + $this->select($offset)->set($value); } function offsetUnset($offset): void { - $this->getWrapper($offset)->unset(); + $this->select($offset)->unset(); } } diff --git a/src/schema/_assoc/AssocResult.php b/src/schema/_assoc/AssocResult.php index acada76..8ac0c77 100644 --- a/src/schema/_assoc/AssocResult.php +++ b/src/schema/_assoc/AssocResult.php @@ -1,8 +1,53 @@ arrayResult = $arrayResult; + $this->keyResults =& $keyResults; + $this->result =& $this->arrayResult; + parent::__construct(); + } + function isAssoc(?AssocResult &$result=null): bool { $result = $this; return true;} + + protected Result $arrayResult; + + /** @var Result[] */ + protected array $keyResults; + + function getKeys(): array { + return array_keys($this->keyResults); + } + + protected Result $result; + + function select($key): Result { + if ($key === null) { + $this->result =& $this->arrayResult; + } elseif (array_key_exists($key, $this->keyResults)) { + $this->result =& $this->keyResults[$key]; + } else { + throw ValueException::invalid_key($key); + } + return $this; + } + + function reset(): void { + $this->arrayResult->reset(); + foreach ($this->keyResults as $result) { + $result->reset(); + } + } + + function __get(string $name) { + return $this->result[$name]; + } + + function __set(string $name, $value): void { + $this->result[$name] = $value; + } } diff --git a/src/schema/_assoc/AssocWrapper.php b/src/schema/_assoc/AssocWrapper.php index a120ef3..ea2981c 100644 --- a/src/schema/_assoc/AssocWrapper.php +++ b/src/schema/_assoc/AssocWrapper.php @@ -21,7 +21,7 @@ class AssocWrapper extends Wrapper { $this->schema = $schema; $this->verifix = $verifix; $this->throw = $throw ?? false; - $this->result = new ScalarResult(); + $this->result = new AssocResult(); $this->reset($array, $arrayKey); $this->throw = $throw ?? true; } @@ -41,17 +41,22 @@ class AssocWrapper extends Wrapper { /** @var string|int|null clé du tableau dans le tableau destination */ protected $arrayKey; - protected IType $type; + protected IType $arrayType; - protected ScalarResult $result; + protected ScalarResult $arrayResult; + + /** @var IType[] */ + protected array $keyTypes; + + /** @var Result[] */ + protected array $keyResults; + + protected AssocResult $result; protected ?array $keys; protected ?array $wrappers; - /** résultat de l'analyse de la valeur */ - protected ?array $results; - protected function newInput(&$value): Input { return new Input($value); } @@ -70,7 +75,7 @@ class AssocWrapper extends Wrapper { return $this->keys; } - function getWrapper($key=null): ScalarWrapper { + function select($key=null): ScalarWrapper { $wrapper = $this->wrappers[$key] ?? null; if ($key !== null) return $wrapper; throw ValueException::invalid_key($key); @@ -81,7 +86,7 @@ class AssocWrapper extends Wrapper { } - function getResult(): ScalarResult { + function getResult(): AssocResult { return $this->result; } @@ -90,7 +95,7 @@ class AssocWrapper extends Wrapper { } function getType(): IType { - return $this->type; + return $this->arrayType; } function isAvailable(): bool { diff --git a/src/schema/_list/ListResult.php b/src/schema/_list/ListResult.php index 5aeb5ac..b40aadb 100644 --- a/src/schema/_list/ListResult.php +++ b/src/schema/_list/ListResult.php @@ -1,8 +1,53 @@ arrayResult = $arrayResult; + $this->keyResults =& $keyResults; + $this->result =& $this->arrayResult; + parent::__construct(); + } + function isList(?ListResult &$result=null): bool { $result = $this; return true;} + + protected Result $arrayResult; + + /** @var Result[] */ + protected array $keyResults; + + function getKeys(): array { + return array_keys($this->keyResults); + } + + protected Result $result; + + function select($key): Result { + if ($key === null) { + $this->result =& $this->arrayResult; + } elseif (array_key_exists($key, $this->keyResults)) { + $this->result =& $this->keyResults[$key]; + } else { + throw ValueException::invalid_key($key); + } + return $this; + } + + function reset(): void { + $this->arrayResult->reset(); + foreach ($this->keyResults as $result) { + $result->reset(); + } + } + + function __get(string $name) { + return $this->result[$name]; + } + + function __set(string $name, $value): void { + $this->result[$name] = $value; + } } diff --git a/src/schema/_scalar/ScalarResult.php b/src/schema/_scalar/ScalarResult.php index 09be6be..ee28c05 100644 --- a/src/schema/_scalar/ScalarResult.php +++ b/src/schema/_scalar/ScalarResult.php @@ -11,37 +11,17 @@ use Throwable; /** * Class ScalarResult: résultat de l'analyse ou de la normalisation d'une valeur - * - * @property bool $resultAvailable le résultat est-il disponible? - * @property bool $present la valeur existe-t-elle? - * @property bool $available si la valeur existe, est-elle disponible? - * @property bool $null si la valeur est disponible, est-elle nulle? - * @property bool $valid si la valeur est disponible, est-elle valide? - * @property bool $normalized si la valeur est valide, est-elle normalisée? - * @property string|null $messageKey clé de message si la valeur n'est pas valide - * @property string|null $message message si la valeur n'est pas valide - * @property string|null $origValue valeur originale avant extraction et analyse - * @property mixed|null $normalizedValue la valeur normalisée si elle est - * disponible, null sinon. ce champ est utilisé comme optimisation si la valeur - * normalisée a déjà été calculée */ class ScalarResult extends Result { - const KEYS = [ - "resultAvailable", - "present", "available", "null", "valid", "normalized", - "messageKey", "message", - "origValue", "normalizedValue", - ]; - function isScalar(?ScalarResult &$result=null): bool { $result = $this; return true; } function getKeys(): array { return [null]; } - function getResult($key=null): Result { - if ($key === null) return $this; - else throw ValueException::invalid_key($key); + function select($key): Result { + if ($key !== null) throw ValueException::invalid_key($key); + return $this; } /** @var array */ diff --git a/src/schema/_scalar/ScalarWrapper.php b/src/schema/_scalar/ScalarWrapper.php index b90da28..bfd0a81 100644 --- a/src/schema/_scalar/ScalarWrapper.php +++ b/src/schema/_scalar/ScalarWrapper.php @@ -19,9 +19,9 @@ class ScalarWrapper extends Wrapper { # c'est une initialisation sans conséquences $throw = true; } - $this->schema = $schema; $this->verifix = $verifix; $this->throw = $throw ?? false; + $this->schema = $schema; $this->result = new ScalarResult(); $this->reset($value, $valueKey); $this->throw = $throw ?? true; @@ -29,6 +29,10 @@ class ScalarWrapper extends Wrapper { function isScalar(?ScalarWrapper &$wrapper=null): bool { $wrapper = $this; return true; } + protected bool $verifix; + + protected bool $throw; + /** schéma de cette valeur */ protected ScalarSchema $schema; @@ -38,10 +42,6 @@ class ScalarWrapper extends Wrapper { /** @var string|int|null clé de la valeur dans le tableau destination */ protected $valueKey; - protected bool $verifix; - - protected bool $throw; - /** type de la valeur après analyse */ protected ?IType $type; @@ -67,9 +67,10 @@ class ScalarWrapper extends Wrapper { return [null]; } - function getWrapper($key=null): ScalarWrapper { - if ($key === null) return $this; - throw ValueException::invalid_key($key); + /** @param string|int|null $key */ + function select($key): ScalarWrapper { + if ($key !== null) throw ValueException::invalid_key($key); + return $this; } /** analyser la valeur et résoudre son type */