Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 86e4a6f

Browse filesBrowse files
committed
dump non UTF-8 encoded strings as binary data
1 parent 243e59c commit 86e4a6f
Copy full SHA for 86e4a6f

File tree

4 files changed

+12
-15
lines changed
Filter options

4 files changed

+12
-15
lines changed

‎src/Symfony/Component/Yaml/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/CHANGELOG.md
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ CHANGELOG
44
3.1.0
55
-----
66

7+
* Strings that are not UTF-8 encoded will be dumped as base64 encoded binary
8+
data.
9+
710
* Added support for dumping multi line strings as literal blocks.
811

912
* Added support for parsing base64 encoded binary data when they are tagged
1013
with the `!!binary` tag.
1114

12-
* Added support for dumping binary data as base64 encoded strings by passing
13-
the `Yaml::DUMP_BASE64_BINARY_DATA` flag.
14-
1515
* Added support for parsing timestamps as `\DateTime` objects:
1616

1717
```php

‎src/Symfony/Component/Yaml/Inline.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Inline.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public static function dump($value, $flags = 0)
201201
return $repr;
202202
case '' == $value:
203203
return "''";
204-
case Yaml::DUMP_BASE64_BINARY_DATA & $flags && self::isBinaryString($value):
204+
case self::isBinaryString($value):
205205
return '!!binary '.base64_encode($value);
206206
case Escaper::requiresDoubleQuoting($value):
207207
return Escaper::escapeWithDoubleQuotes($value);
@@ -627,7 +627,7 @@ public static function evaluateBinaryScalar($scalar)
627627

628628
private static function isBinaryString($value)
629629
{
630-
return preg_match('/[^\x09-\x0d\x20-\xff]/', $value);
630+
return !preg_match('//u', $value) || preg_match('/[^\x09-\x0d\x20-\xff]/', $value);
631631
}
632632

633633
/**

‎src/Symfony/Component/Yaml/Tests/DumperTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/DumperTest.php
+5-7Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,20 +277,18 @@ public function getEscapeSequences()
277277
);
278278
}
279279

280-
public function testBinaryDataIsDumpedAsIsWithoutFlag()
280+
public function testBinaryDataIsDumpedBase64Encoded()
281281
{
282282
$binaryData = file_get_contents(__DIR__.'/Fixtures/arrow.gif');
283-
$expected = "{ data: '".str_replace("'", "''", $binaryData)."' }";
283+
$expected = '{ data: !!binary '.base64_encode($binaryData).' }';
284284

285285
$this->assertSame($expected, $this->dumper->dump(array('data' => $binaryData)));
286286
}
287287

288-
public function testBinaryDataIsDumpedBase64EncodedWithFlag()
288+
public function testNonUtf8DataIsDumpedBase64Encoded()
289289
{
290-
$binaryData = file_get_contents(__DIR__.'/Fixtures/arrow.gif');
291-
$expected = '{ data: !!binary '.base64_encode($binaryData).' }';
292-
293-
$this->assertSame($expected, $this->dumper->dump(array('data' => $binaryData), 0, 0, Yaml::DUMP_BASE64_BINARY_DATA));
290+
// "für" (ISO-8859-1 encoded)
291+
$this->assertSame('!!binary ZsM/cg==', $this->dumper->dump("f\xc3\x3fr"));
294292
}
295293

296294
/**

‎src/Symfony/Component/Yaml/Yaml.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Yaml.php
+2-3Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ class Yaml
2626
const PARSE_OBJECT_FOR_MAP = 8;
2727
const DUMP_EXCEPTION_ON_INVALID_TYPE = 16;
2828
const PARSE_DATETIME = 32;
29-
const DUMP_BASE64_BINARY_DATA = 64;
30-
const DUMP_OBJECT_AS_MAP = 128;
31-
const DUMP_MULTI_LINE_LITERAL_BLOCK = 256;
29+
const DUMP_OBJECT_AS_MAP = 64;
30+
const DUMP_MULTI_LINE_LITERAL_BLOCK = 128;
3231

3332
/**
3433
* Parses YAML into a PHP value.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.