Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 269ab1a

Browse files
committed
feature #53249 [Validator] support Stringable instances in CharsetValidator (xabbuh)
This PR was merged into the 7.1 branch. Discussion ---------- [Validator] support `Stringable` instances in `CharsetValidator` | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | | License | MIT Commits ------- 1f77c3e support Stringable instances in CharsetValidator
2 parents 922d21c + 1f77c3e commit 269ab1a

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/Symfony/Component/Validator/Constraints/CharsetValidator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function validate(mixed $value, Constraint $constraint): void
3131
return;
3232
}
3333

34-
if (!\is_string($value)) {
34+
if (!\is_string($value) && !$value instanceof \Stringable) {
3535
throw new UnexpectedValueException($value, 'string');
3636
}
3737

src/Symfony/Component/Validator/Tests/Constraints/CharsetValidatorTest.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected function createValidator(): CharsetValidator
2626
/**
2727
* @dataProvider provideValidValues
2828
*/
29-
public function testEncodingIsValid(string $value, array $encodings)
29+
public function testEncodingIsValid(string|\Stringable $value, array $encodings)
3030
{
3131
$this->validator->validate($value, new Charset(encodings: $encodings));
3232

@@ -66,6 +66,12 @@ public static function provideValidValues()
6666
yield ['my ûtf 8', ['ASCII', 'UTF-8']];
6767
yield ['my ûtf 8', ['UTF-8']];
6868
yield ['string', ['ISO-8859-1']];
69+
yield [new class() implements \Stringable {
70+
public function __toString(): string
71+
{
72+
return 'my ûtf 8';
73+
}
74+
}, ['UTF-8']];
6975
}
7076

7177
public static function provideInvalidValues()

0 commit comments

Comments
 (0)