diff --git a/src/Validator.php b/src/Validator.php index f5961e8..b46d9cf 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -5,6 +5,7 @@ namespace Aeviiq\ValueObject; use Aeviiq\ValueObject\Exception\InvalidArgumentException; +use stdClass; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraints\Callback; use Symfony\Component\Validator\ConstraintViolation; @@ -61,7 +62,7 @@ public static function validate($value, $constraints): void $violationMessages[] = $violation->getMessage(); } - throw new InvalidArgumentException(sprintf('Invalid value: "%s"', implode('", "', $violationMessages))); + throw new InvalidArgumentException(sprintf('Invalid value: "%s". "%s"', self::toString($value), implode('", "', $violationMessages))); } private static function getValidator(): ValidatorInterface @@ -72,4 +73,13 @@ private static function getValidator(): ValidatorInterface return static::$validator; } + + private static function toString(mixed $value): string + { + if (\is_scalar($value) || \is_object($value) && \method_exists($value, '__toString')) { + return (string) $value; + } + + return ''; + } }