modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2023-12-27 11:38:57 +04:00
parent 3def66748b
commit 8cf225ea1e
2 changed files with 22 additions and 1 deletions

View File

@ -16,7 +16,9 @@ class ValueException extends Exception {
foreach ($values as $value) {
$parts[] = self::value($value);
}
return "[".implode(", ", $parts)."]";
return "[" . implode(", ", $parts) . "]";
} elseif (is_string($value)) {
return $value;
} else {
return var_export($value, true);
}
@ -42,6 +44,11 @@ class ValueException extends Exception {
return new static(self::message($value, $message, $kind, $prefix, " is invalid"));
}
static final function unexpected_class($class, string $expected_class) {
if (is_object($class)) $class = get_class($class);
return new static(self::message($class, null, "class", null, " is invalid, expected $expected_class"));
}
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"));
}

View File

@ -0,0 +1,14 @@
<?php
namespace nulib;
use nulib\tests\TestCase;
class ValueExceptionTest extends TestCase {
function testUnexpectedClass() {
$e = ValueException::unexpected_class(null, self::class);
self::assertSame("class is invalid, expected ".self::class, $e->getMessage());
$e = ValueException::unexpected_class(ValueException::class, self::class);
self::assertSame(ValueException::class.": class is invalid, expected ".self::class, $e->getMessage());
}
}