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

Skip to content

Commit 3334856

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

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-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: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,26 @@ 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+
restore_error_handler();
286+
287+
throw new \LogicException(sprintf('Unexpected error: "%s".', $msg));
288+
}
289+
290+
$deprecations[] = $msg;
291+
});
292+
282293
$binaryData = file_get_contents(__DIR__.'/Fixtures/arrow.gif');
283294
$expected = "{ data: '".str_replace("'", "''", $binaryData)."' }";
295+
$yaml = $this->dumper->dump(array('data' => $binaryData));
284296

285-
$this->assertSame($expected, $this->dumper->dump(array('data' => $binaryData)));
297+
restore_error_handler();
298+
299+
$this->assertCount(1, $deprecations);
300+
$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]);
301+
$this->assertSame($expected, $yaml);
286302
}
287303

288304
public function testBinaryDataIsDumpedBase64EncodedWithFlag()
@@ -293,6 +309,28 @@ public function testBinaryDataIsDumpedBase64EncodedWithFlag()
293309
$this->assertSame($expected, $this->dumper->dump(array('data' => $binaryData), 0, 0, Yaml::DUMP_BASE64_BINARY_DATA));
294310
}
295311

312+
public function testDumpingNonUtf8DataTriggersException()
313+
{
314+
$deprecations = array();
315+
set_error_handler(function ($type, $msg) use (&$deprecations) {
316+
if (E_USER_DEPRECATED !== $type) {
317+
restore_error_handler();
318+
319+
throw new \LogicException(sprintf('Unexpected error: "%s".', $msg));
320+
}
321+
322+
$deprecations[] = $msg;
323+
});
324+
325+
// "für" (ISO-8859-1 encoded)
326+
$this->dumper->dump("f\xc3\x3fr");
327+
328+
restore_error_handler();
329+
330+
$this->assertCount(1, $deprecations);
331+
$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]);
332+
}
333+
296334
/**
297335
* @dataProvider objectAsMapProvider
298336
*/

0 commit comments

Comments
 (0)