From c2ec23be30b8ef6c2624420be46772f7142ad80b Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Thu, 20 Mar 2025 09:57:07 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- src/schema/types/IType.php | 13 ++++++++----- src/schema/types/tarray.php | 15 +++------------ src/schema/types/tbool.php | 13 ++----------- src/schema/types/tcallable.php | 15 +++------------ src/schema/types/tcontent.php | 11 ++++------- src/schema/types/tfloat.php | 13 ++----------- src/schema/types/tgeneric.php | 6 +++--- src/schema/types/tint.php | 13 ++----------- src/schema/types/tkey.php | 11 ++++------- src/schema/types/tmixed.php | 2 +- src/schema/types/tpkey.php | 11 ++++------- src/schema/types/trawstring.php | 9 +++------ 12 files changed, 39 insertions(+), 93 deletions(-) diff --git a/src/schema/types/IType.php b/src/schema/types/IType.php index 9dbd48a..0fbe948 100644 --- a/src/schema/types/IType.php +++ b/src/schema/types/IType.php @@ -98,13 +98,16 @@ interface IType { function parse(string $value); /** - * analyser, corriger éventuellement et normaliser la valeur - * - * NB: si $value est un string. elle doit avoir déjà été traitée au préalable + * normaliser la valeur. elle *doit* déjà être valide. + * Si $value est un string. elle *doit* avoir déjà été traitée au préalable * par extract() et parse() * - * si la valeur était déjà normalisée, ou si une erreur s'est produite, - * retourner false. + * - si $result indique que la valeur est déjà normalisée, cette méthode ne + * fait rien + * - si la valeur était déjà normalisée, mettre à jour $result pour indiquer + * que la valeur est normalisée et retourner false + * - sinon, retourner true pour indiquer qu'il a fallut normaliser la valeur. + * $result n'est pas modifié */ function normalize(&$value, Result $result, Schema $schema): bool; diff --git a/src/schema/types/tarray.php b/src/schema/types/tarray.php index b62160d..a6b2bf9 100644 --- a/src/schema/types/tarray.php +++ b/src/schema/types/tarray.php @@ -37,7 +37,7 @@ class tarray extends _tstring { function isValid($value, ?bool &$normalized=null): bool { $normalized = is_array($value); - return is_scalar($value) || is_array($value); + return $normalized || is_scalar($value); } function parse(string $value) { @@ -50,22 +50,13 @@ class tarray extends _tstring { * @var ScalarSchema $schema */ function normalize(&$value, Result $result, Schema $schema): bool { - if (is_array($value)) { + if ($result->normalized) { + } elseif (is_array($value)) { $result->setNormalized(); - return false; - } elseif (is_string($value)) { - try { - $value = $this->parse($value); - $result->setValid(); - return true; - } catch (ValueException $e) { - } } elseif (is_scalar($value)) { $value = cl::with($value); - $result->setValid(); return true; } - $result->setInvalid($value, $schema); return false; } diff --git a/src/schema/types/tbool.php b/src/schema/types/tbool.php index 5e0795d..dffa9c4 100644 --- a/src/schema/types/tbool.php +++ b/src/schema/types/tbool.php @@ -100,22 +100,13 @@ class tbool extends _tformatable { * @var ScalarSchema $schema */ function normalize(&$value, Result $result, Schema $schema): bool { - if (is_bool($value)) { + if ($result->normalized) { + } elseif (is_bool($value)) { $result->setNormalized(); - return false; - } elseif (is_string($value)) { - try { - $value = $this->parse($value); - $result->setValid(); - return true; - } catch (ValueException $e) { - } } elseif (is_scalar($value)) { $value = boolval($value); - $result->setValid(); return true; } - $result->setInvalid($value, $schema); return false; } diff --git a/src/schema/types/tcallable.php b/src/schema/types/tcallable.php index af6be72..76e2656 100644 --- a/src/schema/types/tcallable.php +++ b/src/schema/types/tcallable.php @@ -27,7 +27,7 @@ class tcallable extends _tsimple { } function isValid($value, ?bool &$normalized=null): bool { - $normalized = is_callable($value); + $normalized = $value instanceof func; return func::check($value); } @@ -44,22 +44,13 @@ class tcallable extends _tsimple { * @var ScalarSchema $schema */ function normalize(&$value, Result $result, Schema $schema): bool { - if ($value instanceof func) { + if ($result->normalized) { + } elseif ($value instanceof func) { $result->setNormalized(); - return false; } elseif (is_callable($value)) { $value = func::with($value); - $result->setNormalized(); return true; - } elseif (is_string($value)) { - try { - $value = $this->parse($value); - $result->setValid(); - return true; - } catch (ValueException $e) { - } } - $result->setInvalid($value, $schema); return false; } diff --git a/src/schema/types/tcontent.php b/src/schema/types/tcontent.php index 2bb2912..0a600f4 100644 --- a/src/schema/types/tcontent.php +++ b/src/schema/types/tcontent.php @@ -25,7 +25,7 @@ abstract class tcontent extends _tunion { function isValid($value, ?bool &$normalized=null): bool { $normalized = is_string($value) || is_array($value); - return is_scalar($value) || is_array($value); + return $normalized || is_scalar($value); } function parse(string $value) { @@ -37,17 +37,14 @@ abstract class tcontent extends _tunion { * @var ScalarSchema $schema */ function normalize(&$value, Result $result, Schema $schema): bool { - if (is_string($value) || is_array($value)) { + if ($result->normalized) { + } elseif (is_string($value) || is_array($value)) { $result->setNormalized(); - return false; } elseif (is_scalar($value)) { $value = strval($value); - $result->setValid(); return true; - } else { - $result->setInvalid($value, $schema); - return false; } + return false; } function format($value, $format=null): string { diff --git a/src/schema/types/tfloat.php b/src/schema/types/tfloat.php index f1befd1..85767b6 100644 --- a/src/schema/types/tfloat.php +++ b/src/schema/types/tfloat.php @@ -44,22 +44,13 @@ class tfloat extends _tformatable { * @var ScalarSchema $schema */ function normalize(&$value, Result $result, Schema $schema): bool { - if (is_float($value)) { + if ($result->normalized) { + } elseif (is_float($value)) { $result->setNormalized(); - return false; - } elseif (is_string($value)) { - try { - $value = $this->parse($value); - $result->setValid(); - return true; - } catch (ValueException $e) { - } } elseif (is_scalar($value)) { $value = floatval($value); - $result->setValid(); return true; } - $result->setInvalid($value, $schema); return false; } } diff --git a/src/schema/types/tgeneric.php b/src/schema/types/tgeneric.php index 492c57e..fe069b8 100644 --- a/src/schema/types/tgeneric.php +++ b/src/schema/types/tgeneric.php @@ -29,8 +29,8 @@ class tgeneric extends _tsimple { } function isValid($value, ?bool &$normalized=null): bool { - $normalized = true; - return $value instanceof $this->class; + $normalized = $value instanceof $this->class; + return $normalized; } function parse(string $value) { @@ -42,7 +42,7 @@ class tgeneric extends _tsimple { * @var ScalarSchema $schema */ function normalize(&$value, Result $result, Schema $schema): bool { - $result->setNormalized(); + if (!$result->normalized) $result->setNormalized(); return false; } diff --git a/src/schema/types/tint.php b/src/schema/types/tint.php index d7e95df..eddab99 100644 --- a/src/schema/types/tint.php +++ b/src/schema/types/tint.php @@ -46,22 +46,13 @@ class tint extends _tformatable { * @var ScalarSchema $schema */ function normalize(&$value, Result $result, Schema $schema): bool { - if (is_int($value)) { + if ($result->normalized) { + } elseif (is_int($value)) { $result->setNormalized(); - return false; - } elseif (is_string($value)) { - try { - $value = $this->parse($value); - $result->setValid(); - return true; - } catch (ValueException $e) { - } } elseif (is_scalar($value)) { $value = intval($value); - $result->setValid(); return true; } - $result->setInvalid($value, $schema); return false; } } diff --git a/src/schema/types/tkey.php b/src/schema/types/tkey.php index 6f5072e..f1cca27 100644 --- a/src/schema/types/tkey.php +++ b/src/schema/types/tkey.php @@ -25,7 +25,7 @@ class tkey extends _tunion { function isValid($value, ?bool &$normalized=null): bool { $normalized = is_string($value) || is_int($value); - return is_scalar($value); + return $normalized || is_scalar($value); } function parse(string $value) { @@ -37,17 +37,14 @@ class tkey extends _tunion { * @var ScalarSchema $schema */ function normalize(&$value, Result $result, Schema $schema): bool { - if (is_string($value) || is_int($value)) { + if ($result->normalized) { + } elseif (is_string($value) || is_int($value)) { $result->setNormalized(); - return false; } elseif (is_scalar($value)) { $value = strval($value); - $result->setValid(); return true; - } else { - $result->setInvalid($value, $schema); - return false; } + return false; } function format($value, $format=null): string { diff --git a/src/schema/types/tmixed.php b/src/schema/types/tmixed.php index 652e51d..f549204 100644 --- a/src/schema/types/tmixed.php +++ b/src/schema/types/tmixed.php @@ -36,7 +36,7 @@ class tmixed extends _tsimple { * @var ScalarSchema $schema */ function normalize(&$value, Result $result, Schema $schema): bool { - $result->setNormalized(); + if (!$result->normalized) $result->setNormalized(); return false; } diff --git a/src/schema/types/tpkey.php b/src/schema/types/tpkey.php index e099051..7b2234b 100644 --- a/src/schema/types/tpkey.php +++ b/src/schema/types/tpkey.php @@ -30,7 +30,7 @@ class tpkey extends _tunion { function isValid($value, ?bool &$normalized=null): bool { $normalized = is_string($value) || is_int($value) || is_array($value); - return is_scalar($value) || is_array($value); + return $normalized || is_scalar($value); } function parse(string $value) { @@ -42,17 +42,14 @@ class tpkey extends _tunion { * @var ScalarSchema $schema */ function normalize(&$value, Result $result, Schema $schema): bool { - if (is_string($value) || is_int($value) || is_array($value)) { + if ($result->normalized) { + } elseif (is_string($value) || is_int($value) || is_array($value)) { $result->setNormalized(); - return false; } elseif (is_scalar($value)) { $value = strval($value); - $result->setValid(); return true; - } else { - $result->setInvalid($value, $schema); - return false; } + return false; } function format($value, $format=null): string { diff --git a/src/schema/types/trawstring.php b/src/schema/types/trawstring.php index 1f09dab..674d430 100644 --- a/src/schema/types/trawstring.php +++ b/src/schema/types/trawstring.php @@ -45,17 +45,14 @@ class trawstring extends _tstring { * @var ScalarSchema $schema */ function normalize(&$value, Result $result, Schema $schema): bool { - if (is_string($value)) { + if ($result->normalized) { + } elseif (is_string($value)) { $result->setNormalized(); - return false; } elseif (is_scalar($value)) { $value = strval($value); - $result->setValid(); return true; - } else { - $result->setInvalid($value, $schema); - return false; } + return false; } function format($value, $format=null): string {