diff --git a/src/schema/Value.php b/src/schema/Value.php index 02a8c54..bc8de23 100644 --- a/src/schema/Value.php +++ b/src/schema/Value.php @@ -35,7 +35,10 @@ abstract class Value implements ArrayAccess, IteratorAggregate { abstract function getType(): IType; /** retourner true si la valeur existe */ - abstract function available(): bool; + abstract function isPresent(): bool; + + /** retourner true si la valeur est disponible */ + abstract function isAvailable(): bool; /** supprimer la valeur */ abstract function unset(): void; @@ -44,16 +47,16 @@ abstract class Value implements ArrayAccess, IteratorAggregate { abstract function set($value): self; /** obtenir le résultat de l'appel de la fonction {@link set()} */ - abstract function result(): Result; + abstract function getResult(): Result; /** retourner true si la valeur est valide */ - abstract function valid(): bool; + abstract function isValid(): bool; /** obtenir la valeur */ abstract function get($default=null); /** retourner true si la valeur est dans sa forme normalisée */ - abstract function normalized(): bool; + abstract function isNormalized(): bool; /** formatter la valeur pour affichage */ abstract function format($format=null): string; diff --git a/src/schema/_scalar/ScalarResult.php b/src/schema/_scalar/ScalarResult.php index 44f07b9..e5c5419 100644 --- a/src/schema/_scalar/ScalarResult.php +++ b/src/schema/_scalar/ScalarResult.php @@ -9,15 +9,16 @@ use nur\sery\schema\ValueException; /** * Class ScalarResult: résultat de l'analyse ou de la normalisation d'une valeur * - * @property bool $missing la valeur est-elle inexistante? - * @property bool $null si elle existe, la valeur est-nulle? - * @property bool $valid si elle existe, la valeur est-elle valide? + * @property bool $present la valeur existe-t-elle? + * @property bool $available si la valeur existe, est-elle disponible? + * @property bool $null si la valeur existe, est-elle nulle? + * @property bool $valid si la valeur existe, est-elle valide? * @property bool $normalized si la valeur est valide, est-elle normalisée? * @property string|null $orig valeur originale avant analyse avec parse() * @property string|null $message message si la valeur n'est pas valide */ class ScalarResult extends Result { - const KEYS = ["missing", "null", "valid", "normalized", "orig", "message"]; + const KEYS = ["present", "available", "null", "valid", "normalized", "orig", "message"]; function isScalar(?ScalarResult &$scalar=null): bool { $scalar = $this; return true; } @@ -36,7 +37,8 @@ class ScalarResult extends Result { function reset(): void { $this->result = array_merge( array_fill_keys(static::KEYS, null), [ - "missing" => true, + "present" => false, + "available" => false, "null" => false, "valid" => false, "normalized" => false, @@ -57,9 +59,10 @@ class ScalarResult extends Result { } function setMissing(ScalarSchema $schema): int { - $this->missing = true; + $this->present = false; + $this->available = false; if (!$schema->required) { - $this->null = true; + $this->null = false; $this->valid = true; $this->normalized = true; return ref_analyze::NORMALIZED; @@ -71,8 +74,26 @@ class ScalarResult extends Result { } } + function setUnavailable(ScalarSchema $schema): int { + $this->present = true; + $this->available = false; + if (!$schema->required) { + $this->null = false; + $this->valid = true; + $this->normalized = true; + return ref_analyze::NORMALIZED; + } else { + $message = cl::get($schema->messages, "unavailable"); + self::replace_key($message, $schema->name); + $this->message = $message; + return ref_analyze::UNAVAILABLE; + } + } + function setNull(ScalarSchema $schema): int { - $this->missing = true; + $this->present = true; + $this->available = true; + $this->null = true; if ($schema->nullable) { $this->valid = true; $this->normalized = true; diff --git a/src/schema/_scalar/ScalarValue.php b/src/schema/_scalar/ScalarValue.php index c35d746..dcbbadb 100644 --- a/src/schema/_scalar/ScalarValue.php +++ b/src/schema/_scalar/ScalarValue.php @@ -61,23 +61,23 @@ class ScalarValue extends Value { $result = $this->result; $result->reset(); #XXX résoudre le type - if (!$input->available()) return $result->setMissing($schema); + if (!$input->isAvailable()) return $result->setUnavailable($schema); $value = $input->get($destKey); if ($value === null) return $result->setNull($schema); } - function exists(): bool { - return $this->input->exits($this->destKey); + function isPresent(): bool { + return $this->input->isPresent($this->destKey); } - function available(): bool { - return $this->input->available($this->destKey); + function isAvailable(): bool { + return $this->input->isAvailable($this->destKey); } function get($default=null) { $destKey = $this->destKey; $input = $this->input; - if ($input->available($destKey)) return $input->get($destKey); + if ($input->isAvailable($destKey)) return $input->get($destKey); else return $default; } @@ -92,10 +92,10 @@ class ScalarValue extends Value { return $this->type; } - function valid(): bool { + function isValid(): bool { } - function normalized(): bool { + function isNormalized(): bool { } /** diff --git a/src/schema/input/FormInput.php b/src/schema/input/FormInput.php index fcd0b22..0013d68 100644 --- a/src/schema/input/FormInput.php +++ b/src/schema/input/FormInput.php @@ -14,12 +14,12 @@ namespace nur\sery\schema\input; class FormInput extends Input { const ALLOW_EMPTY = false; - function exists($key=null): bool { + function isPresent($key=null): bool { if ($key === null) return false; return array_key_exists($key, $_POST) || array_key_exists($key, $_GET); } - function available($key=null): bool { + function isAvailable($key=null): bool { if ($key === null) return false; if (array_key_exists($key, $_POST)) { return $this->allowEmpty || $_POST[$key] !== ""; diff --git a/src/schema/input/GetInput.php b/src/schema/input/GetInput.php index 1d74d84..1319bf7 100644 --- a/src/schema/input/GetInput.php +++ b/src/schema/input/GetInput.php @@ -8,12 +8,12 @@ namespace nur\sery\schema\input; * une référence */ class GetInput extends FormInput { - function exists($key=null): bool { + function isPresent($key=null): bool { if ($key === null) return false; return array_key_exists($key, $_GET); } - function available($key=null): bool { + function isAvailable($key=null): bool { if ($key === null) return false; if (array_key_exists($key, $_GET)) { return $this->allowEmpty || $_GET[$key] !== ""; diff --git a/src/schema/input/Input.php b/src/schema/input/Input.php index 7f19fff..bbfb540 100644 --- a/src/schema/input/Input.php +++ b/src/schema/input/Input.php @@ -24,7 +24,7 @@ class Input { protected $dest; /** tester si la valeur existe sans tenir compte de $allowEmpty */ - function exits($key=null): bool { + function isPresent($key=null): bool { if ($key === null) return true; $dest = $this->dest; return $dest !== null && array_key_exists($key, $dest); @@ -36,7 +36,7 @@ class Input { protected $allowEmpty; /** tester si la valeur est disponible en tenant compte de $allowEmpty */ - function available($key=null): bool { + function isAvailable($key=null): bool { if ($key === null) return true; $dest = $this->dest; if ($dest === null || !array_key_exists($key, $dest)) return false; diff --git a/src/schema/input/PostInput.php b/src/schema/input/PostInput.php index 9cebe08..e76ec8e 100644 --- a/src/schema/input/PostInput.php +++ b/src/schema/input/PostInput.php @@ -8,12 +8,12 @@ namespace nur\sery\schema\input; * une référence */ class PostInput extends FormInput { - function exists($key=null): bool { + function isPresent($key=null): bool { if ($key === null) return false; return array_key_exists($key, $_POST); } - function available($key=null): bool { + function isAvailable($key=null): bool { if ($key === null) return false; if (array_key_exists($key, $_POST)) { return $this->allowEmpty || $_POST[$key] !== ""; diff --git a/src/schema/ref/ref_analyze.php b/src/schema/ref/ref_analyze.php index a640ba1..c95b3e6 100644 --- a/src/schema/ref/ref_analyze.php +++ b/src/schema/ref/ref_analyze.php @@ -4,19 +4,21 @@ namespace nur\sery\schema\ref; class ref_analyze { /** @var int résultat de l'analyse: valeur inexistante */ const MISSING = 0; + /** @var int résultat de l'analyse: valeur non disponible */ + const UNAVAILABLE = 1; /** @var int résultat de l'analyse: valeur nulle */ - const NULL = 1; + const NULL = 2; /** @var int résultat de l'analyse: valeur chaine à parser */ - const STRING = 2; + const STRING = 3; /** @var int résultat de l'analyse: valeur invalide */ - const INVALID = 3; + const INVALID = 4; /** @var int résultat de l'analyse: valeur valide mais pas normalisée */ - const VALID = 4; + const VALID = 5; /** @var int résultat de l'analyse: valeur valide normalisée */ - const NORMALIZED = 5; + const NORMALIZED = 6; } diff --git a/src/schema/ref/ref_schema.php b/src/schema/ref/ref_schema.php index 3d443ac..e81df51 100644 --- a/src/schema/ref/ref_schema.php +++ b/src/schema/ref/ref_schema.php @@ -42,6 +42,7 @@ class ref_schema { const MESSAGES = [ "missing" => "{key}: Vous devez spécifier cette valeur", + "unavailable" => "{key}: Vous devez spécifier cette valeur", "null" => "{key}: Cette valeur ne doit pas être nulle", "empty" => "{key}: Cette valeur ne doit pas être vide", "invalid" => "{key}: {orig}: cette valeur est invalide",