From 1f77c3e36988830c6646a1f57b1a2eac88859ec4 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Wed, 27 Dec 2023 23:59:38 +0100 Subject: [PATCH] support Stringable instances in CharsetValidator --- .../Component/Validator/Constraints/CharsetValidator.php | 2 +- .../Validator/Tests/Constraints/CharsetValidatorTest.php | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/CharsetValidator.php b/src/Symfony/Component/Validator/Constraints/CharsetValidator.php index 2a4ca66a44832..c682d67c3aa16 100644 --- a/src/Symfony/Component/Validator/Constraints/CharsetValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CharsetValidator.php @@ -31,7 +31,7 @@ public function validate(mixed $value, Constraint $constraint): void return; } - if (!\is_string($value)) { + if (!\is_string($value) && !$value instanceof \Stringable) { throw new UnexpectedValueException($value, 'string'); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CharsetValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CharsetValidatorTest.php index 1c6c4037ad7c8..f13bab00376a7 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CharsetValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CharsetValidatorTest.php @@ -26,7 +26,7 @@ protected function createValidator(): CharsetValidator /** * @dataProvider provideValidValues */ - public function testEncodingIsValid(string $value, array $encodings) + public function testEncodingIsValid(string|\Stringable $value, array $encodings) { $this->validator->validate($value, new Charset(encodings: $encodings)); @@ -66,6 +66,12 @@ public static function provideValidValues() yield ['my ûtf 8', ['ASCII', 'UTF-8']]; yield ['my ûtf 8', ['UTF-8']]; yield ['string', ['ISO-8859-1']]; + yield [new class() implements \Stringable { + public function __toString(): string + { + return 'my ûtf 8'; + } + }, ['UTF-8']]; } public static function provideInvalidValues()