diff --git a/src/Symfony/Component/String/AbstractString.php b/src/Symfony/Component/String/AbstractString.php index 13567c7b0f4f3..a0a801e69ab0b 100644 --- a/src/Symfony/Component/String/AbstractString.php +++ b/src/Symfony/Component/String/AbstractString.php @@ -577,8 +577,11 @@ public function toByteString(string $toEncoding = null): ByteString try { try { $b->string = mb_convert_encoding($this->string, $toEncoding, 'UTF-8'); - } catch (InvalidArgumentException $e) { + } catch (InvalidArgumentException|\ValueError $e) { if (!\function_exists('iconv')) { + if ($e instanceof \ValueError) { + throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); + } throw $e; } diff --git a/src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php b/src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php index d25fbdee57b6f..5d3127329c459 100644 --- a/src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php +++ b/src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php @@ -1583,4 +1583,22 @@ public static function provideWidth(): array [17, "\u{007f}\u{007f}f\u{001b}[0moo\u{0001}bar\u{007f}cccïf\u{008e}cy\u{0005}1", false], // f[0moobarcccïfcy1 ]; } + + /** + * @dataProvider provideToByteString + */ + public function testToByteString(string $origin, string $encoding) + { + $instance = static::createFromString($origin)->toByteString($encoding); + $this->assertInstanceOf(ByteString::class, $instance); + } + + public static function provideToByteString(): array + { + return [ + ['žsžsý', 'UTF-8'], + ['žsžsý', 'windows-1250'], + ['žsžsý', 'Windows-1252'], + ]; + } }