Skip to content

Commit c96c24e

Browse files
committed
[Yaml] deprecate dumping non UTF-8 strings
1 parent 243e59c commit c96c24e

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@ public static function dump($value, $flags = 0)
203203
return "''";
204204
case Yaml::DUMP_BASE64_BINARY_DATA & $flags && self::isBinaryString($value):
205205
return '!!binary '.base64_encode($value);
206+
}
207+
208+
if (self::isBinaryString($value)) {
209+
@trigger_error('Dumping non UTF-8 data without passing the DUMP_BASE64_BINARY_DATA flag is deprecated since Symfony 3.1 and will be removed in 4.0.', E_USER_DEPRECATED);
210+
}
211+
212+
switch (true) {
206213
case Escaper::requiresDoubleQuoting($value):
207214
return Escaper::escapeWithDoubleQuotes($value);
208215
case Escaper::requiresSingleQuoting($value):
@@ -627,7 +634,7 @@ public static function evaluateBinaryScalar($scalar)
627634

628635
private static function isBinaryString($value)
629636
{
630-
return preg_match('/[^\x09-\x0d\x20-\xff]/', $value);
637+
return !preg_match('//u', $value);
631638
}
632639

633640
/**

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,24 @@ public function getEscapeSequences()
279279

280280
public function testBinaryDataIsDumpedAsIsWithoutFlag()
281281
{
282+
$deprecations = array();
283+
set_error_handler(function ($type, $msg) use (&$deprecations) {
284+
if (E_USER_DEPRECATED === $type) {
285+
$deprecations[] = $msg;
286+
} else {
287+
throw new \RuntimeException($msg);
288+
}
289+
});
290+
282291
$binaryData = file_get_contents(__DIR__.'/Fixtures/arrow.gif');
283292
$expected = "{ data: '".str_replace("'", "''", $binaryData)."' }";
293+
$yaml = $this->dumper->dump(array('data' => $binaryData));
294+
295+
restore_error_handler();
284296

285-
$this->assertSame($expected, $this->dumper->dump(array('data' => $binaryData)));
297+
$this->assertCount(1, $deprecations);
298+
$this->assertSame('Dumping non UTF-8 data without passing the DUMP_BASE64_BINARY_DATA flag is deprecated since Symfony 3.1 and will be removed in 4.0.', $deprecations[0]);
299+
$this->assertSame($expected, $yaml);
286300
}
287301

288302
public function testBinaryDataIsDumpedBase64EncodedWithFlag()
@@ -293,6 +307,26 @@ public function testBinaryDataIsDumpedBase64EncodedWithFlag()
293307
$this->assertSame($expected, $this->dumper->dump(array('data' => $binaryData), 0, 0, Yaml::DUMP_BASE64_BINARY_DATA));
294308
}
295309

310+
public function testDumpingNonUtf8DataTriggersException()
311+
{
312+
$deprecations = array();
313+
set_error_handler(function ($type, $msg) use (&$deprecations) {
314+
if (E_USER_DEPRECATED === $type) {
315+
$deprecations[] = $msg;
316+
} else {
317+
throw new \RuntimeException($msg);
318+
}
319+
});
320+
321+
// "für" (ISO-8859-1 encoded)
322+
$this->dumper->dump("f\xc3\x3fr");
323+
324+
restore_error_handler();
325+
326+
$this->assertCount(1, $deprecations);
327+
$this->assertSame('Dumping non UTF-8 data without passing the DUMP_BASE64_BINARY_DATA flag is deprecated since Symfony 3.1 and will be removed in 4.0.', $deprecations[0]);
328+
}
329+
296330
/**
297331
* @dataProvider objectAsMapProvider
298332
*/

0 commit comments

Comments
 (0)