Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cf91b0a

Browse files
committed
feature #21471 [Yaml] Allow dumping empty array as YAML sequence (c960657)
This PR was squashed before being merged into the 3.3-dev branch (closes #21471). Discussion ---------- [Yaml] Allow dumping empty array as YAML sequence | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #9870, #15937, #16266 | License | MIT | Doc PR | PHP arrays are dumped as either YAML sequences or mappings, depending on whether the array has continuos integer keys or not. An empty array is always dumped as a YAML mapping. Sometimes you want it dumped as a YAML sequence instead. Commits ------- 87ffaf2 Bump version number af7067c Dump empty object as mapping a6d94c1 [Yaml] Allow dumping empty array as YAML sequence
2 parents 6d77cdf + 87ffaf2 commit cf91b0a

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

src/Symfony/Component/Yaml/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ CHANGELOG
66

77
* Omitting the key of a mapping is deprecated and will throw a `ParseException` in Symfony 4.0.
88

9+
* Added support for dumping empty PHP arrays as YAML sequences:
10+
11+
```php
12+
Yaml::dump([], 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
13+
```
14+
915
3.2.0
1016
-----
1117

src/Symfony/Component/Yaml/Inline.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public static function dump($value, $flags = 0)
174174
}
175175

176176
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \stdClass || $value instanceof \ArrayObject)) {
177-
return self::dumpArray((array) $value, $flags);
177+
return self::dumpArray((array) $value, $flags & ~Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
178178
}
179179

180180
if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
@@ -262,7 +262,7 @@ public static function isHash(array $value)
262262
private static function dumpArray($value, $flags)
263263
{
264264
// array
265-
if ($value && !self::isHash($value)) {
265+
if (($value || Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE & $flags) && !self::isHash($value)) {
266266
$output = array();
267267
foreach ($value as $val) {
268268
$output[] = self::dump($val, $flags);

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,24 @@ public function testObjectSupportDisabledWithExceptionsPassingTrue()
247247
$this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true);
248248
}
249249

250+
public function testEmptyArray()
251+
{
252+
$dump = $this->dumper->dump(array());
253+
$this->assertEquals('{ }', $dump);
254+
255+
$dump = $this->dumper->dump(array(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
256+
$this->assertEquals('[]', $dump);
257+
258+
$dump = $this->dumper->dump(array(), 9, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
259+
$this->assertEquals('[]', $dump);
260+
261+
$dump = $this->dumper->dump(new \ArrayObject(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_OBJECT_AS_MAP);
262+
$this->assertEquals('{ }', $dump);
263+
264+
$dump = $this->dumper->dump(new \stdClass(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_OBJECT_AS_MAP);
265+
$this->assertEquals('{ }', $dump);
266+
}
267+
250268
/**
251269
* @dataProvider getEscapeSequences
252270
*/

src/Symfony/Component/Yaml/Yaml.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Yaml
2929
const DUMP_OBJECT_AS_MAP = 64;
3030
const DUMP_MULTI_LINE_LITERAL_BLOCK = 128;
3131
const PARSE_CONSTANT = 256;
32+
const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 512;
3233

3334
/**
3435
* @experimental in version 3.3

0 commit comments

Comments
 (0)