exceptions normalisées
This commit is contained in:
parent
75c06e7038
commit
cf30ff6386
105
php/src/exceptions.php
Normal file
105
php/src/exceptions.php
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
<?php
|
||||||
|
namespace nulib;
|
||||||
|
|
||||||
|
use nulib\text\Word;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class exceptions: répertoire d'exceptions normalisées
|
||||||
|
*/
|
||||||
|
class exceptions {
|
||||||
|
const WORD = "la valeur#s";
|
||||||
|
|
||||||
|
protected static Word $word;
|
||||||
|
|
||||||
|
protected static function word(): Word {
|
||||||
|
return self::$word ??= new Word(static::WORD);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* indiquer qu'une valeur est invalide pour une raison générique
|
||||||
|
*/
|
||||||
|
static function invalid_value($value, ?string $reason=null): UserException {
|
||||||
|
$msg = var_export($value, true);
|
||||||
|
$msg .= self::word()->_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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user