10000 [Yaml] Add the `Yaml::DUMP_FORCE_DOUBLE_QUOTES_ON_VALUES` flag to enforce double quotes around string values by dkarlovi · Pull Request #59880 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Yaml] Add the Yaml::DUMP_FORCE_DOUBLE_QUOTES_ON_VALUES flag to enforce double quotes around string values #59880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Yaml/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

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

7.2
---
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public static function dump(mixed $value, int $flags = 0, bool $rootLevel = fals
case self::isBinaryString($value):
return '!!binary '.base64_encode($value);
case Escaper::requiresDoubleQuoting($value):
case Yaml::DUMP_FORCE_DOUBLE_QUOTES_ON_VALUES & $flags:
return Escaper::escapeWithDoubleQuotes($value);
case Escaper::requiresSingleQuoting($value):
$singleQuoted = Escaper::escapeWithSingleQuotes($value);
Expand Down Expand Up @@ -242,12 +243,13 @@ private static function dumpArray(array $value, int $flags): string
private static function dumpHashArray(array|\ArrayObject|\stdClass $value, int $flags): string
{
$output = [];
$keyFlags = $flags &~ Yaml::DUMP_FORCE_DOUBLE_QUOTES_ON_VALUES;
foreach ($value as $key => $val) {
if (\is_int($key) && Yaml::DUMP_NUMERIC_KEY_AS_STRING & $flags) {
$key = (string) $key;
}

$output[] = \sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
$output[] = \sprintf('%s: %s', self::dump($key, $keyFlags), self::dump($val, $flags));
}

return \sprintf('{ %s }', implode(', ', $output));
Expand Down
81 changes: 81 additions & 0 deletions src/Symfony/Component/Yaml/Tests/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,87 @@ public function testDumpNullAsTilde()
$this->assertSame('{ foo: ~ }', $this->dumper->dump(['foo' => null], 0, 0, Yaml::DUMP_NULL_AS_TILDE));
}

/**
* @dataProvider getForceQuotesOnValuesData
*/
public function testCanForceQuotesOnValues(array $input, string $expected)
{
$this->assertSame($expected, $this->dumper->dump($input, 0, 0, Yaml::DUMP_FORCE_DOUBLE_QUOTES_ON_VALUES));
}

public function getForceQuotesOnValuesData(): iterable
{
yield 'empty string' => [
['foo' => ''],
'{ foo: \'\' }',
];

yield 'double quote' => [
['foo' => '"'],
'{ foo: "\"" }',
];

yield 'single quote' => [
['foo' => "'"],
'{ foo: "\'" }',
];

yield 'line break' => [
['foo' => "line\nbreak"],
'{ foo: "line\nbreak" }',
];

yield 'tab character' => [
['foo' => "tab\tcharacter"],
'{ foo: "tab\tcharacter" }',
];

yield 'backslash' => [
['foo' => "back\\slash"],
'{ foo: "back\\\\slash" }',
];

yield 'colon' => [
['foo' => 'colon: value'],
'{ foo: "colon: value" }',
];

yield 'dash' => [
['foo' => '- dash'],
'{ foo: "- dash" }',
];

yield 'numeric' => [
['foo' => 23],
'{ foo: 23 }',
];

yield 'boolean' => [
['foo' => true],
'{ foo: true }',
];

yield 'null' => [
['foo' => null],
'{ foo: null }',
];

yield 'nested' => [
['foo' => ['bar' => 'bat', 'baz' => 23]],
'{ foo: { bar: "bat", baz: 23 } }',
];

yield 'mix of values' => [
['foo' => 'bat', 'bar' => 23, 'baz' => true, 'qux' => "line\nbreak"],
'{ foo: "bat", bar: 23, baz: true, qux: "line\nbreak" }',
];

yield 'special YAML characters' => [
['foo' => 'colon: value', 'bar' => '- dash', 'baz' => '? question', 'qux' => '# hash'],
'{ foo: "colon: value", bar: "- dash", baz: "? question", qux: "# hash" }',
];
}

/**
* @dataProvider getNumericKeyData
*/
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Yaml/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Yaml
public const DUMP_NUMERIC_KEY_AS_STRING = 4096;
public const DUMP_NULL_AS_EMPTY = 8192;
public const DUMP_COMPACT_NESTED_MAPPING = 16384;
public const DUMP_FORCE_DOUBLE_QUOTES_ON_VALUES = 32768;

/**
* Parses a YAML file into a PHP value.
Expand Down
Loading
0