Skip to content

Commit 9306691

Browse files
committed
feature #32669 [Yaml] Add flag to dump NULL as ~ (OskarStark)
This PR was merged into the 4.4 branch. Discussion ---------- [Yaml] Add flag to dump NULL as ~ | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #... <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | symfony/symfony-docs#12071 This PR adds the ability to dump `null` as `~` by a new Flag `Yaml::DUMP_NULL_AS_TILDE`: ```diff - foo: null + foo: ~ ``` Todos: - [x] Fix/add tests Commits ------- 749c11d94c [Yaml] Add flag to dump NULL as ~
2 parents 915e204 + 2cf32e0 commit 9306691

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

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

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
*

Tests/DumperTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,11 @@ public function testNegativeIndentationThrowsException()
504504
$this->expectExceptionMessage('The indentation must be greater than zero');
505505
new Dumper(-4);
506506
}
507+
508+
public function testDumpNullAsTilde()
509+
{
510+
$this->assertSame('{ foo: ~ }', $this->dumper->dump(['foo' => null], 0, 0, Yaml::DUMP_NULL_AS_TILDE));
511+
}
507512
}
508513

509514
class A

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)