diff --git a/php/src/exceptions.php b/php/src/exceptions.php new file mode 100644 index 0000000..afad896 --- /dev/null +++ b/php/src/exceptions.php @@ -0,0 +1,105 @@ +_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): 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, $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): 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): 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(?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(?string $reason=null): UserException { + $msg = "il y a trop "; + $msg .= self::word()->_de(2); + if ($reason) $msg .= ": $reason"; + return new UserException($msg); + } +}