Skip to content

Commit 749c11d

Browse files
committed
[Yaml] Add flag to dump NULL as ~
1 parent b5f592e commit 749c11d

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

src/Symfony/Component/Yaml/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.4.0
5+
-----
6+
7+
* Added support to dump `null` as `~` by using the `Yaml::DUMP_NULL_AS_TILDE` flag.
8+
49
4.3.0
510
-----
611

src/Symfony/Component/Yaml/Inline.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Inline
3333
private static $objectSupport = false;
3434
private static $objectForMap = false;
3535
private static $constantSupport = false;
36+
private static $nullAsTilde = false;
3637

3738
/**
3839
* @param int $flags
@@ -45,6 +46,7 @@ public static function initialize($flags, $parsedLineNumber = null, $parsedFilen
4546
self::$objectSupport = (bool) (Yaml::PARSE_OBJECT & $flags);
4647
self::$objectForMap = (bool) (Yaml::PARSE_OBJECT_FOR_MAP & $flags);
4748
self::$constantSupport = (bool) (Yaml::PARSE_CONSTANT & $flags);
49+
self::$nullAsTilde = (bool) (Yaml::DUMP_NULL_AS_TILDE & $flags);
4850
self::$parsedFilename = $parsedFilename;
4951

5052
if (null !== $parsedLineNumber) {
@@ -129,7 +131,7 @@ public static function dump($value, int $flags = 0): string
129131
throw new DumpException(sprintf('Unable to dump PHP resources in a YAML file ("%s").', get_resource_type($value)));
130132
}
131133

132-
return 'null';
134+
return self::dumpNull($flags);
133135
case $value instanceof \DateTimeInterface:
134136
return $value->format('c');
135137
case \is_object($value):
@@ -155,11 +157,11 @@ public static function dump($value, int $flags = 0): string
155157
throw new DumpException('Object support when dumping a YAML file has been disabled.');
156158
}
157159

158-
return 'null';
160+
return self::dumpNull($flags);
159161
case \is_array($value):
160162
return self::dumpArray($value, $flags);
161163
case null === $value:
162-
return 'null';
164+
return self::dumpNull($flags);
163165
case true === $value:
164166
return 'true';
165167
case false === $value:
@@ -256,6 +258,15 @@ private static function dumpArray(array $value, int $flags): string
256258
return sprintf('{ %s }', implode(', ', $output));
257259
}
258260

261+
private static function dumpNull(int $flags): string
262+
{
263+
if (Yaml::DUMP_NULL_AS_TILDE & $flags) {
264+
return '~';
265+
}
266+
267+
return 'null';
268+
}
269+
259270
/**
260271
* Parses a YAML scalar.
261272
*

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,11 @@ public function testNegativeIndentationThrowsException()
513513
{
514514
new Dumper(-4);
515515
}
516+
517+
public function testDumpNullAsTilde()
518+
{
519+
$this->assertSame('{ foo: ~ }', $this->dumper->dump(['foo' => null], 0, 0, Yaml::DUMP_NULL_AS_TILDE));
520+
}
516521
}
517522

518523
class A

src/Symfony/Component/Yaml/Yaml.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Yaml
3333
const PARSE_CONSTANT = 256;
3434
const PARSE_CUSTOM_TAGS = 512;
3535
const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 1024;
36+
const DUMP_NULL_AS_TILDE = 2048;
3637

3738
/**
3839
* Parses a YAML file into a PHP value.

0 commit comments

Comments
 (0)
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