Skip to content

Commit 2fa4bc6

Browse files
[Yaml] Add support for dumping null as an empty string by using the Yaml::DUMP_NULL_AS_EMPTY flag
1 parent e63495e commit 2fa4bc6

File tree

5 files changed

+23
-0
lines changed

5 files changed

+23
-0
lines changed

src/Symfony/Component/Yaml/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Deprecate parsing duplicate mapping keys whose value is `null`
8+
* Add support for dumping `null` as an empty string by using the `Yaml::DUMP_NULL_AS_EMPTY` flag
89

910
7.1
1011
---

src/Symfony/Component/Yaml/Dumper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public function __construct(int $indentation = 4)
4646
*/
4747
public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags = 0): string
4848
{
49+
if ($flags & Yaml::DUMP_NULL_AS_TILDE && $flags & Yaml::DUMP_NULL_AS_EMPTY) {
50+
throw new \InvalidArgumentException('The Yaml::DUMP_NULL_AS_TILDE and Yaml::DUMP_NULL_AS_EMPTY flags cannot be used together.');
51+
}
52+
4953
$output = '';
5054
$prefix = $indent ? str_repeat(' ', $indent) : '';
5155
$dumpObjectAsInlineMap = true;

src/Symfony/Component/Yaml/Inline.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ private static function dumpNull(int $flags): string
259259
return '~';
260260
}
261261

262+
if (Yaml::DUMP_NULL_AS_EMPTY & $flags) {
263+
return '';
264+
}
265+
262266
return 'null';
263267
}
264268

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,11 @@ public function testDumpNullAsTilde()
853853
$this->assertSame('{ foo: ~ }', $this->dumper->dump(['foo' => null], 0, 0, Yaml::DUMP_NULL_AS_TILDE));
854854
}
855855

856+
public function testDumpNullAsEmpty()
857+
{
858+
$this->assertSame('{ foo: }', $this->dumper->dump(['foo' => null], 0, 0, Yaml::DUMP_NULL_AS_EMPTY));
859+
}
860+
856861
/**
857862
* @dataProvider getNumericKeyData
858863
*/
@@ -1012,6 +1017,14 @@ private function assertSameData($expected, $actual)
10121017
var_export($actual, true)
10131018
);
10141019
}
1020+
1021+
public function testUseDumpAsTildeAndDumpAsEmptyFlagsTogether()
1022+
{
1023+
$this->expectException(\InvalidArgumentException::class);
1024+
$this->expectExceptionMessage('The Yaml::DUMP_NULL_AS_TILDE and Yaml::DUMP_NULL_AS_EMPTY flags cannot be used together.');
1025+
1026+
$this->dumper->dump(['foo' => null], 0, 0, Yaml::DUMP_NULL_AS_TILDE | Yaml::DUMP_NULL_AS_EMPTY);
1027+
}
10151028
}
10161029

10171030
class A

src/Symfony/Component/Yaml/Yaml.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Yaml
3535
public const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 1024;
3636
public const DUMP_NULL_AS_TILDE = 2048;
3737
public const DUMP_NUMERIC_KEY_AS_STRING = 4096;
38+
public const DUMP_NULL_AS_EMPTY = 8192;
3839

3940
/**
4041
* 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