From 8cf225ea1e201556987b9320769034c2d35dee71 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Wed, 27 Dec 2023 11:38:57 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- php/src_base/ValueException.php | 9 ++++++++- php/tests/ValueExceptionTest.php | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 php/tests/ValueExceptionTest.php diff --git a/php/src_base/ValueException.php b/php/src_base/ValueException.php index 665549c..51e35c8 100644 --- a/php/src_base/ValueException.php +++ b/php/src_base/ValueException.php @@ -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")); } diff --git a/php/tests/ValueExceptionTest.php b/php/tests/ValueExceptionTest.php new file mode 100644 index 0000000..d325159 --- /dev/null +++ b/php/tests/ValueExceptionTest.php @@ -0,0 +1,14 @@ +getMessage()); + + $e = ValueException::unexpected_class(ValueException::class, self::class); + self::assertSame(ValueException::class.": class is invalid, expected ".self::class, $e->getMessage()); + } +}