diff --git a/src/Symfony/Component/Yaml/CHANGELOG.md b/src/Symfony/Component/Yaml/CHANGELOG.md index 70d392da343ee..a375859ada3b8 100644 --- a/src/Symfony/Component/Yaml/CHANGELOG.md +++ b/src/Symfony/Component/Yaml/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +4.1.0 +----- + + * Added support to dump `null` values as `~` through the `Yaml::DUMP_NULL_AS_TILDE` flag. + 4.0.0 ----- diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index abab86903b8d3..3b85fda7cd88d 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -127,7 +127,7 @@ public static function dump($value, int $flags = 0): string throw new DumpException(sprintf('Unable to dump PHP resources in a YAML file ("%s").', get_resource_type($value))); } - return 'null'; + return self::getNullRepresentationValue($flags); case $value instanceof \DateTimeInterface: return $value->format('c'); case is_object($value): @@ -153,11 +153,11 @@ public static function dump($value, int $flags = 0): string throw new DumpException('Object support when dumping a YAML file has been disabled.'); } - return 'null'; + return self::getNullRepresentationValue($flags); case is_array($value): return self::dumpArray($value, $flags); case null === $value: - return 'null'; + return self::getNullRepresentationValue($flags); case true === $value: return 'true'; case false === $value: @@ -738,4 +738,13 @@ private static function getHexRegex(): string { return '~^0x[0-9a-f_]++$~i'; } + + private static function getNullRepresentationValue(int $flags): string + { + if (Yaml::DUMP_NULL_AS_TILDE & $flags) { + return '~'; + } + + return 'null'; + } } diff --git a/src/Symfony/Component/Yaml/Tests/DumperTest.php b/src/Symfony/Component/Yaml/Tests/DumperTest.php index 0a8c023d017fe..d3b4afe4008a4 100644 --- a/src/Symfony/Component/Yaml/Tests/DumperTest.php +++ b/src/Symfony/Component/Yaml/Tests/DumperTest.php @@ -421,6 +421,11 @@ public function testNegativeIndentationThrowsException() { new Dumper(-4); } + + public function testDumpNullAsTilde() + { + $this->assertSame('{ foo: ~ }', $this->dumper->dump(array('foo' => null), 0, 0, Yaml::DUMP_NULL_AS_TILDE)); + } } class A diff --git a/src/Symfony/Component/Yaml/Yaml.php b/src/Symfony/Component/Yaml/Yaml.php index 80de7e877723c..28f4b163d0431 100644 --- a/src/Symfony/Component/Yaml/Yaml.php +++ b/src/Symfony/Component/Yaml/Yaml.php @@ -33,6 +33,7 @@ class Yaml const PARSE_CONSTANT = 256; const PARSE_CUSTOM_TAGS = 512; const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 1024; + const DUMP_NULL_AS_TILDE = 2048; /** * Parses a YAML file into a PHP value. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy