diff --git a/src/schema/types/IType.php b/src/schema/types/IType.php index eb03408..70c4fbc 100644 --- a/src/schema/types/IType.php +++ b/src/schema/types/IType.php @@ -21,7 +21,8 @@ interface IType { /** * analyser, corriger éventuellement et normaliser la valeur * - * si la valeur était déjà normalisée, retourner false. + * 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; diff --git a/src/schema/types/_tsimple.php b/src/schema/types/_tsimple.php new file mode 100644 index 0000000..73c2e37 --- /dev/null +++ b/src/schema/types/_tsimple.php @@ -0,0 +1,14 @@ +isAvailable($destKey) && $input->get($destKey) !== false; + } + + function isNull($value): bool { + return $value === null || (is_string($value) && trim($value) === ""); + } +} diff --git a/src/schema/types/tarray.php b/src/schema/types/tarray.php index f06061a..76ad9f3 100644 --- a/src/schema/types/tarray.php +++ b/src/schema/types/tarray.php @@ -2,8 +2,10 @@ namespace nur\sery\schema\types; use nur\sery\cl; +use nur\sery\schema\Result; +use nur\sery\schema\Schema; -abstract class tarray implements IType { +class tarray extends _tsimple { static function ensure_array(&$array): void { if (!is_array($array)) $array = cl::with($array); } @@ -11,4 +13,15 @@ abstract class tarray implements IType { static function ensure_narray(&$array): void { if ($array !== null) self::ensure_array($array); } + + function isValid($value, ?bool &$normalized=null): bool { + $normalized = is_array($value); + return is_scalar($value) || is_array($value); + } + + function verifix(&$value, Result &$result, Schema $schema): bool { + } + + function format($value, $format=null): string { + } } diff --git a/src/schema/types/tbool.php b/src/schema/types/tbool.php index 08ad7ba..4461aa9 100644 --- a/src/schema/types/tbool.php +++ b/src/schema/types/tbool.php @@ -9,7 +9,7 @@ use nur\sery\schema\input\Input; use nur\sery\schema\Result; use nur\sery\schema\Schema; -class tbool implements IType { +class tbool extends _tsimple { /** liste de valeurs chaines à considérer comme 'OUI' */ const YES_VALUES = [ # IMPORTANT: ordonner par taille décroissante pour compatibilité avec parse() @@ -57,18 +57,14 @@ class tbool implements IType { return $input->isAvailable($destKey); } - function isNull($value): bool { - return $value === null || (is_string($value) && trim($value) === ""); - } - function isValid($value, ?bool &$normalized=null): bool { + $normalized = is_bool($value); if (is_string($value)) { $value = trim($value); $valid = self::is_yes($value) || self::is_no($value); } else { $valid = is_scalar($value); } - $normalized = is_bool($value); return $valid; } diff --git a/src/schema/types/tcallable.php b/src/schema/types/tcallable.php index 3bcb4d1..ed84642 100644 --- a/src/schema/types/tcallable.php +++ b/src/schema/types/tcallable.php @@ -1,9 +1,13 @@ isAvailable($destKey) && $input->get($destKey) !== false; - } - - function isNull($value): bool { - return $value === null || (is_string($value) && trim($value) === ""); - } - function isValid($value, ?bool &$normalized=null): bool { + $normalized = is_float($value); if (is_string($value)) $valid = is_numeric(trim($value)); else $valid = is_scalar($value); - $normalized = is_float($value); return $valid; } diff --git a/src/schema/types/tint.php b/src/schema/types/tint.php index a5c52cd..8fc6746 100644 --- a/src/schema/types/tint.php +++ b/src/schema/types/tint.php @@ -7,7 +7,7 @@ use nur\sery\schema\input\Input; use nur\sery\schema\Result; use nur\sery\schema\Schema; -class tint implements IType { +class tint extends _tsimple { static function ensure_int(&$int): void { if (!is_int($int)) $int = intval($int); } @@ -16,20 +16,12 @@ class tint implements IType { if ($int !== null) self::ensure_int($int); } - function isAvailable(Input $input, $destKey): bool { - return $input->isAvailable($destKey) && $input->get($destKey) !== false; - } - - function isNull($value): bool { - return $value === null || (is_string($value) && trim($value) === ""); - } - const INT_PATTERN = '/^[-+]?[0-9]+(?:\.[0-9]*)?$/'; function isValid($value, ?bool &$normalized=null): bool { + $normalized = is_int($value); if (is_string($value)) $valid = is_numeric(trim($value)); else $valid = is_scalar($value); - $normalized = is_int($value); return $valid; } diff --git a/src/schema/types/tkey.php b/src/schema/types/tkey.php index 731beb2..69b517e 100644 --- a/src/schema/types/tkey.php +++ b/src/schema/types/tkey.php @@ -1,7 +1,10 @@ isAvailable($destKey) && $input->get($destKey) !== false; - } - function isNull($value): bool { return $value === null; } function isValid($value, ?bool &$normalized=null): bool { - $valid = is_scalar($value); $normalized = is_string($value); - return $valid; + return is_scalar($value); } /** @@ -40,11 +35,12 @@ class tstring implements IType { return false; } elseif (is_scalar($value)) { $value = strval($value); + $result->setValid(); + return true; } else { - return $result->setInvalid($value, $schema); + $result->setInvalid($value, $schema); + return false; } - $result->setValid(); - return true; } function format($value, $format=null): string {