From c116bd31a1049a7de07fd8c66ac051314842d26f Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Wed, 8 Oct 2025 09:04:55 +0400 Subject: [PATCH] =?UTF-8?q?exceptions=20normalis=C3=A9es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/exceptions.php | 76 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 68 insertions(+), 8 deletions(-) diff --git a/src/exceptions.php b/src/exceptions.php index 5363de6..afad896 100644 --- a/src/exceptions.php +++ b/src/exceptions.php @@ -1,7 +1,7 @@ _ce(); + $msg .= " est invalide"; + if ($reason) $msg .= ": $reason"; + return new UserException($msg); } /** * spécialisation de {@link self::invalid_value()} qui permet d'indiquer les * types attendus */ - static function invalid_type($value, $expectedTypes=null): Exception { + static function invalid_type($value, $expectedTypes=null): UserException { + $pronom = self::word()->pronom(); + $expectedTypes = cl::withn($expectedTypes); + if (!$expectedTypes) { + $reason = null; + } elseif (count($expectedTypes) == 1) { + $reason = "$pronom doit être du type suivant: "; + } else { + $reason = "$pronom doit être d'un des types suivants: "; + } + $reason .= implode(", ", $expectedTypes); + return self::invalid_value($value, $reason); } - static function invalid_format($value, ?string $reason=null): Exception { + static function invalid_format($value, $expectedFormats=null): UserException { + $pronom = self::word()->pronom(); + $expectedFormats = cl::withn($expectedFormats); + if (!$expectedFormats) { + $reason = null; + } elseif (count($expectedFormats) == 1) { + $reason = "$pronom doit être au format suivant: "; + } else { + $reason = "$pronom doit être dans l'un des formats suivants: "; + } + $reason .= implode(", ", $expectedFormats); + return self::invalid_value($value, $reason); } - static function out_of_range($value, ?int $min=null, ?int $max=null, ?string $reason=null): Exception { + static function out_of_range($value, ?int $min=null, ?int $max=null): UserException { + $pronom = self::word()->pronom(); + if ($min !== null && $max !== null) { + $reason = "$pronom doit être compris entre $min et $max"; + } else if ($min !== null) { + $reason = "$pronom doit être supérieur à $min"; + } elseif ($max !== null) { + $reason = "$pronom doit être inférieur à $max"; + } else { + $reason = null; + } + return self::invalid_value($value, $reason); } - static function null_value(?string $reason=null): Exception { + static function null_value(?string $reason=null): UserException { + $msg = self::word()->_ce(); + $msg .= " ne doit pas être nulle"; + if ($reason) $msg .= ": $reason"; + return new UserException($msg); } /** * indiquer qu'une valeur est manquante */ - static function missing_value(?string $reason=null): Exception { + static function missing_value(?int $amout=null, ?string $reason=null): UserException { + $msg = "il manque "; + if ($amout !== null) { + $msg .= self::word()->q($amout); + } else { + $msg .= self::word()->_le(); + } + if ($reason) $msg .= ": $reason"; + return new UserException($msg); } /** * indiquer qu'une valeur est en trop */ - static function unexpected_value($value, ?string $reason=null): Exception { + static function unexpected_value(?string $reason=null): UserException { + $msg = "il y a trop "; + $msg .= self::word()->_de(2); + if ($reason) $msg .= ": $reason"; + return new UserException($msg); } }