From 75dd942b8cf0b3040e1357fd24b74cc258d9404f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Thu, 6 Jun 2024 12:30:21 +0200 Subject: [PATCH] Add own assertions with better exception messages --- .../AbstractDataTransferObject.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/DataTransferObject/AbstractDataTransferObject.php b/src/DataTransferObject/AbstractDataTransferObject.php index 2008626..1c8546f 100644 --- a/src/DataTransferObject/AbstractDataTransferObject.php +++ b/src/DataTransferObject/AbstractDataTransferObject.php @@ -4,8 +4,6 @@ namespace Setono\PeakWMS\DataTransferObject; -use Webmozart\Assert\Assert; - abstract class AbstractDataTransferObject implements \JsonSerializable { public function jsonSerialize(): array @@ -44,10 +42,12 @@ protected static function convertDateTime(null|\DateTimeImmutable|string $value) return $value; } - $value = \DateTimeImmutable::createFromFormat(\DATE_ATOM, $value); - Assert::notFalse($value); + $res = \DateTimeImmutable::createFromFormat(\DATE_ATOM, $value); + if (false === $res) { + throw new \InvalidArgumentException(sprintf('The value %s is not a valid date time string based on the format %s', $value, \DATE_ATOM)); + } - return $value; + return $res; } /** @@ -67,7 +67,9 @@ protected static function convertEnum(null|int|string|\BackedEnum $value, string } if ($value instanceof \BackedEnum) { - Assert::isInstanceOf($value, $enumClass); + if (!is_a($value, $enumClass)) { + throw new \InvalidArgumentException(sprintf('The value %s is not an instance of the enum class %s', $value::class, $enumClass)); + } return $value; }