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 1c06770

Browse filesBrowse files
committed
[Yaml] Add the Yaml::DUMP_FORCE_DOUBLE_QUOTES_ON_VALUES flag to enforce double quotes around string values
1 parent 329920d commit 1c06770
Copy full SHA for 1c06770

File tree

Expand file treeCollapse file tree

4 files changed

+86
-1
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+86
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add compact nested mapping support by using the `Yaml::DUMP_COMPACT_NESTED_MAPPING` flag
8+
* Add the `Yaml::DUMP_FORCE_DOUBLE_QUOTES_ON_VALUES` flag to enforce double quotes around string values
89

910
7.2
1011
---

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Inline.php
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ public static function dump(mixed $value, int $flags = 0, bool $rootLevel = fals
173173
case self::isBinaryString($value):
174174
return '!!binary '.base64_encode($value);
175175
case Escaper::requiresDoubleQuoting($value):
176+
case Yaml::DUMP_FORCE_DOUBLE_QUOTES_ON_VALUES & $flags:
176177
return Escaper::escapeWithDoubleQuotes($value);
177178
case Escaper::requiresSingleQuoting($value):
178179
$singleQuoted = Escaper::escapeWithSingleQuotes($value);
@@ -242,12 +243,13 @@ private static function dumpArray(array $value, int $flags): string
242243
private static function dumpHashArray(array|\ArrayObject|\stdClass $value, int $flags): string
243244
{
244245
$output = [];
246+
$keyFlags = $flags &~ Yaml::DUMP_FORCE_DOUBLE_QUOTES_ON_VALUES;
245247
foreach ($value as $key => $val) {
246248
if (\is_int($key) && Yaml::DUMP_NUMERIC_KEY_AS_STRING & $flags) {
247249
$key = (string) $key;
248250
}
249251

250-
$output[] = \sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
252+
$output[] = \sprintf('%s: %s', self::dump($key, $keyFlags), self::dump($val, $flags));
251253
}
252254

253255
return \sprintf('{ %s }', implode(', ', $output));

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/DumperTest.php
+81Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,87 @@ public function testDumpNullAsTilde()
910910
$this->assertSame('{ foo: ~ }', $this->dumper->dump(['foo' => null], 0, 0, Yaml::DUMP_NULL_AS_TILDE));
911911
}
912912

913+
/**
914+
* @dataProvider getForceQuotesOnValuesData
915+
*/
916+
public function testCanForceQuotesOnValues(array $input, string $expected)
917+
{
918+
$this->assertSame($expected, $this->dumper->dump($input, 0, 0, Yaml::DUMP_FORCE_DOUBLE_QUOTES_ON_VALUES));
919+
}
920+
921+
public function getForceQuotesOnValuesData(): iterable
922+
{
923+
yield 'empty string' => [
924+
['foo' => ''],
925+
'{ foo: \'\' }',
926+
];
927+
928+
yield 'double quote' => [
929+
['foo' => '"'],
930+
'{ foo: "\"" }',
931+
];
932+
933+
yield 'single quote' => [
934+
['foo' => "'"],
935+
'{ foo: "\'" }',
936+
];
937+
938+
yield 'line break' => [
939+
['foo' => "line\nbreak"],
940+
'{ foo: "line\nbreak" }',
941+
];
942+
943+
yield 'tab character' => [
944+
['foo' => "tab\tcharacter"],
945+
'{ foo: "tab\tcharacter" }',
946+
];
947+
948+
yield 'backslash' => [
949+
['foo' => "back\\slash"],
950+
'{ foo: "back\\\\slash" }',
951+
];
952+
953+
yield 'colon' => [
954+
['foo' => 'colon: value'],
955+
'{ foo: "colon: value" }',
956+
];
957+
958+
yield 'dash' => [
959+
['foo' => '- dash'],
960+
'{ foo: "- dash" }',
961+
];
962+
963+
yield 'numeric' => [
964+
['foo' => 23],
965+
'{ foo: 23 }',
966+
];
967+
968+
yield 'boolean' => [
969+
['foo' => true],
970+
'{ foo: true }',
971+
];
972+
973+
yield 'null' => [
974+
['foo' => null],
975+
'{ foo: null }',
976+
];
977+
978+
yield 'nested' => [
979+
['foo' => ['bar' => 'bat', 'baz' => 23]],
980+
'{ foo: { bar: "bat", baz: 23 } }',
981+
];
982+
983+
yield 'mix of values' => [
984+
['foo' => 'bat', 'bar' => 23, 'baz' => true, 'qux' => "line\nbreak"],
985+
'{ foo: "bat", bar: 23, baz: true, qux: "line\nbreak" }',
986+
];
987+
988+
yield 'special YAML characters' => [
989+
['foo' => 'colon: value', 'bar' => '- dash', 'baz' => '? question', 'qux' => '# hash'],
990+
'{ foo: "colon: value", bar: "- dash", baz: "? question", qux: "# hash" }',
991+
];
992+
}
993+
913994
/**
914995
* @dataProvider getNumericKeyData
915996
*/

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Yaml.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Yaml
3737
public const DUMP_NUMERIC_KEY_AS_STRING = 4096;
3838
public const DUMP_NULL_AS_EMPTY = 8192;
3939
public const DUMP_COMPACT_NESTED_MAPPING = 16384;
40+
public const DUMP_FORCE_DOUBLE_QUOTES_ON_VALUES = 32768;
4041

4142
/**
4243
* Parses a YAML file 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.