Skip to content

Commit

Permalink
feature #21471 [Yaml] Allow dumping empty array as YAML sequence (c96…
Browse files Browse the repository at this point in the history
…0657)

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
  • Loading branch information
fabpot committed Feb 19, 2017
2 parents 6d77cdf + 87ffaf2 commit cf91b0a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/Symfony/Component/Yaml/CHANGELOG.md
Expand Up @@ -6,6 +6,12 @@ CHANGELOG

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

* Added support for dumping empty PHP arrays as YAML sequences:

```php
Yaml::dump([], 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
```

3.2.0
-----

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Yaml/Inline.php
Expand Up @@ -174,7 +174,7 @@ public static function dump($value, $flags = 0)
}

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

if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
Expand Down Expand Up @@ -262,7 +262,7 @@ public static function isHash(array $value)
private static function dumpArray($value, $flags)
{
// array
if ($value && !self::isHash($value)) {
if (($value || Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE & $flags) && !self::isHash($value)) {
$output = array();
foreach ($value as $val) {
$output[] = self::dump($val, $flags);
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Yaml/Tests/DumperTest.php
Expand Up @@ -247,6 +247,24 @@ public function testObjectSupportDisabledWithExceptionsPassingTrue()
$this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true);
}

public function testEmptyArray()
{
$dump = $this->dumper->dump(array());
$this->assertEquals('{ }', $dump);

$dump = $this->dumper->dump(array(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
$this->assertEquals('[]', $dump);

$dump = $this->dumper->dump(array(), 9, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
$this->assertEquals('[]', $dump);

$dump = $this->dumper->dump(new \ArrayObject(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_OBJECT_AS_MAP);
$this->assertEquals('{ }', $dump);

$dump = $this->dumper->dump(new \stdClass(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_OBJECT_AS_MAP);
$this->assertEquals('{ }', $dump);
}

/**
* @dataProvider getEscapeSequences
*/
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Yaml/Yaml.php
Expand Up @@ -29,6 +29,7 @@ class Yaml
const DUMP_OBJECT_AS_MAP = 64;
const DUMP_MULTI_LINE_LITERAL_BLOCK = 128;
const PARSE_CONSTANT = 256;
const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 512;

/**
* @experimental in version 3.3
Expand Down

0 comments on commit cf91b0a

Please sign in to comment.