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

Skip to content

Commit 06eb52c

Browse files
committed
feature #18294 [Yaml] dump non UTF-8 encoded strings as binary data (xabbuh)
This PR was merged into the 3.1-dev branch. Discussion ---------- [Yaml] dump non UTF-8 encoded strings as binary data | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #18241 | License | MIT | Doc PR | Commits ------- 86e4a6f dump non UTF-8 encoded strings as binary data
2 parents 86eee06 + 86e4a6f commit 06eb52c

File tree

4 files changed

+12
-15
lines changed

4 files changed

+12
-15
lines changed

src/Symfony/Component/Yaml/CHANGELOG.md

Lines 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

Lines 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

Lines 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

Lines 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)