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
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ValueException: indiquer qu'une valeur est invalide
|
|
|
|
*/
|
2023-12-28 12:33:06 +04:00
|
|
|
class ValueException extends UserException {
|
2023-10-03 04:17:01 +04:00
|
|
|
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);
|
|
|
|
}
|
2023-12-27 11:38:57 +04:00
|
|
|
return "[" . implode(", ", $parts) . "]";
|
|
|
|
} elseif (is_string($value)) {
|
|
|
|
return $value;
|
2023-10-03 04:17:01 +04:00
|
|
|
} 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 {
|
2023-12-31 18:54:23 +04:00
|
|
|
return new static(self::message(null, $message, $kind, $prefix, " should not be null"));
|
2023-10-03 04:17:01 +04:00
|
|
|
}
|
|
|
|
|
2023-12-28 12:33:06 +04:00
|
|
|
static final function invalid_kind($value=null, ?string $kind=null, ?string $prefix=null, ?string $message=null): self {
|
2023-10-03 04:17:01 +04:00
|
|
|
return new static(self::message($value, $message, $kind, $prefix, " is invalid"));
|
|
|
|
}
|
|
|
|
|
2023-12-28 12:33:06 +04:00
|
|
|
static final function invalid_key($value, ?string $prefix=null, ?string $message=null): self {
|
|
|
|
return self::invalid_kind($value, "key", $prefix, $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
static final function invalid_value($value, ?string $prefix=null, ?string $message=null): self {
|
|
|
|
return self::invalid_kind($value, "value", $prefix, $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
static final function invalid_type($value, string $expected_type): self {
|
|
|
|
return new static(self::message($value, null, "type", null, " is invalid, expected $expected_type"));
|
|
|
|
}
|
|
|
|
|
|
|
|
static final function invalid_class($class, string $expected_class): self {
|
2023-12-27 11:38:57 +04:00
|
|
|
if (is_object($class)) $class = get_class($class);
|
|
|
|
return new static(self::message($class, null, "class", null, " is invalid, expected $expected_class"));
|
|
|
|
}
|
|
|
|
|
2023-10-03 04:17:01 +04:00
|
|
|
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"));
|
|
|
|
}
|
|
|
|
}
|