diff --git a/src/Symfony/Component/Yaml/CHANGELOG.md b/src/Symfony/Component/Yaml/CHANGELOG.md index a2da8ad964bba..aa0c3fa565894 100644 --- a/src/Symfony/Component/Yaml/CHANGELOG.md +++ b/src/Symfony/Component/Yaml/CHANGELOG.md @@ -4,14 +4,14 @@ CHANGELOG 3.1.0 ----- + * Strings that are not UTF-8 encoded will be dumped as base64 encoded binary + data. + * Added support for dumping multi line strings as literal blocks. * Added support for parsing base64 encoded binary data when they are tagged with the `!!binary` tag. - * Added support for dumping binary data as base64 encoded strings by passing - the `Yaml::DUMP_BASE64_BINARY_DATA` flag. - * Added support for parsing timestamps as `\DateTime` objects: ```php diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index 2a7c649970a81..cc994b95626e5 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -201,7 +201,7 @@ public static function dump($value, $flags = 0) return $repr; case '' == $value: return "''"; - case Yaml::DUMP_BASE64_BINARY_DATA & $flags && self::isBinaryString($value): + case self::isBinaryString($value): return '!!binary '.base64_encode($value); case Escaper::requiresDoubleQuoting($value): return Escaper::escapeWithDoubleQuotes($value); @@ -627,7 +627,7 @@ public static function evaluateBinaryScalar($scalar) private static function isBinaryString($value) { - return preg_match('/[^\x09-\x0d\x20-\xff]/', $value); + return !preg_match('//u', $value) || preg_match('/[^\x09-\x0d\x20-\xff]/', $value); } /** diff --git a/src/Symfony/Component/Yaml/Tests/DumperTest.php b/src/Symfony/Component/Yaml/Tests/DumperTest.php index 99496cf9d2f80..5c18568f94c12 100644 --- a/src/Symfony/Component/Yaml/Tests/DumperTest.php +++ b/src/Symfony/Component/Yaml/Tests/DumperTest.php @@ -277,20 +277,18 @@ public function getEscapeSequences() ); } - public function testBinaryDataIsDumpedAsIsWithoutFlag() + public function testBinaryDataIsDumpedBase64Encoded() { $binaryData = file_get_contents(__DIR__.'/Fixtures/arrow.gif'); - $expected = "{ data: '".str_replace("'", "''", $binaryData)."' }"; + $expected = '{ data: !!binary '.base64_encode($binaryData).' }'; $this->assertSame($expected, $this->dumper->dump(array('data' => $binaryData))); } - public function testBinaryDataIsDumpedBase64EncodedWithFlag() + public function testNonUtf8DataIsDumpedBase64Encoded() { - $binaryData = file_get_contents(__DIR__.'/Fixtures/arrow.gif'); - $expected = '{ data: !!binary '.base64_encode($binaryData).' }'; - - $this->assertSame($expected, $this->dumper->dump(array('data' => $binaryData), 0, 0, Yaml::DUMP_BASE64_BINARY_DATA)); + // "für" (ISO-8859-1 encoded) + $this->assertSame('!!binary ZsM/cg==', $this->dumper->dump("f\xc3\x3fr")); } /** diff --git a/src/Symfony/Component/Yaml/Yaml.php b/src/Symfony/Component/Yaml/Yaml.php index be21f2c3b7cc3..fd96b94c4cb0e 100644 --- a/src/Symfony/Component/Yaml/Yaml.php +++ b/src/Symfony/Component/Yaml/Yaml.php @@ -26,9 +26,8 @@ class Yaml const PARSE_OBJECT_FOR_MAP = 8; const DUMP_EXCEPTION_ON_INVALID_TYPE = 16; const PARSE_DATETIME = 32; - const DUMP_BASE64_BINARY_DATA = 64; - const DUMP_OBJECT_AS_MAP = 128; - const DUMP_MULTI_LINE_LITERAL_BLOCK = 256; + const DUMP_OBJECT_AS_MAP = 64; + const DUMP_MULTI_LINE_LITERAL_BLOCK = 128; /** * Parses YAML into a PHP value.