2023-10-03 04:17:01 +04:00
|
|
|
<?php
|
2023-10-20 09:59:43 +04:00
|
|
|
namespace nulib;
|
2023-10-03 04:17:01 +04:00
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ValueException: indiquer qu'une valeur est invalide
|
|
|
|
*/
|
|
|
|
class ValueException extends Exception {
|
|
|
|
private static function value($value): string {
|
|
|
|
if (is_object($value)) {
|
|
|
|
return "<".get_class($value).">";
|
|
|
|
} elseif (is_array($value)) {
|
|
|
|
$values = $value;
|
|
|
|
$parts = [];
|
|
|
|
foreach ($values as $value) {
|
|
|
|
$parts[] = self::value($value);
|
|
|
|
}
|
|
|
|
return "[".implode(", ", $parts)."]";
|
|
|
|
} else {
|
|
|
|
return var_export($value, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function message($value, ?string $message, ?string $kind, ?string $prefix, ?string $suffix): string {
|
|
|
|
if ($kind === null) $kind = "value";
|
|
|
|
if ($message === null) $message = "$kind$suffix";
|
|
|
|
if ($value !== null) {
|
|
|
|
$value = self::value($value);
|
|
|
|
if ($prefix) $prefix = "$prefix: $value";
|
|
|
|
else $prefix = $value;
|
|
|
|
}
|
|
|
|
if ($prefix) $prefix = "$prefix: ";
|
|
|
|
return $prefix.$message;
|
|
|
|
}
|
|
|
|
|
|
|
|
static final function null(?string $kind=null, ?string $prefix=null, ?string $message=null): self {
|
|
|
|
return new static(self::message(null, $message, $kind, $prefix, " is null"));
|
|
|
|
}
|
|
|
|
|
|
|
|
static final function invalid($value=null, ?string $kind=null, ?string $prefix=null, ?string $message=null): self {
|
|
|
|
return new static(self::message($value, $message, $kind, $prefix, " is invalid"));
|
|
|
|
}
|
|
|
|
|
|
|
|
static final function forbidden($value=null, ?string $kind=null, ?string $prefix=null, ?string $message=null): self {
|
|
|
|
return new static(self::message($value, $message, $kind, $prefix, " is forbidden"));
|
|
|
|
}
|
|
|
|
}
|