Skip to content

Commit 3ea0a62

Browse files
committed
[Yaml] force dumping with quotes
1 parent 329920d commit 3ea0a62

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

src/Symfony/Component/Yaml/Inline.php

+3-1
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_QUOTES & $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_QUOTES;
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

+5
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,11 @@ public function testDumpNullAsTilde()
910910
$this->assertSame('{ foo: ~ }', $this->dumper->dump(['foo' => null], 0, 0, Yaml::DUMP_NULL_AS_TILDE));
911911
}
912912

913+
public function testForceQuotes()
914+
{
915+
$this->assertSame('{ foo: "bat", bar: 23 }', $this->dumper->dump(['foo' => 'bat', 'bar' => 23], 0, 0, Yaml::DUMP_FORCE_QUOTES));
916+
}
917+
913918
/**
914919
* @dataProvider getNumericKeyData
915920
*/

src/Symfony/Component/Yaml/Yaml.php

+1
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_QUOTES = 32768;
4041

4142
/**
4243
* Parses a YAML file into a PHP value.

0 commit comments

Comments
 (0)