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

Skip to content

[Yaml] dump non UTF-8 encoded strings as binary data #18294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Symfony/Component/Yaml/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

/**
Expand Down
12 changes: 5 additions & 7 deletions src/Symfony/Component/Yaml/Tests/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/Symfony/Component/Yaml/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down