[Yaml] Allow dumping empty array as YAML sequence

This commit is contained in:
Christian Schmidt 2017-01-31 12:07:40 +01:00
parent 2183f98f54
commit a6d94c1b53
4 changed files with 29 additions and 1 deletions

View File

@ -1,6 +1,15 @@
CHANGELOG CHANGELOG
========= =========
3.2.3
-----
* Added support for dumping empty PHP arrays as YAML sequences:
```php
Yaml::dump([], 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
```
3.2.0 3.2.0
----- -----

View File

@ -252,7 +252,7 @@ class Inline
private static function dumpArray($value, $flags) private static function dumpArray($value, $flags)
{ {
// array // array
if ($value && !self::isHash($value)) { if (($value || Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE & $flags) && !self::isHash($value)) {
$output = array(); $output = array();
foreach ($value as $val) { foreach ($value as $val) {
$output[] = self::dump($val, $flags); $output[] = self::dump($val, $flags);

View File

@ -246,6 +246,24 @@ EOF;
$this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true); $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 * @dataProvider getEscapeSequences
*/ */

View File

@ -29,6 +29,7 @@ class Yaml
const DUMP_OBJECT_AS_MAP = 64; const DUMP_OBJECT_AS_MAP = 64;
const DUMP_MULTI_LINE_LITERAL_BLOCK = 128; const DUMP_MULTI_LINE_LITERAL_BLOCK = 128;
const PARSE_CONSTANT = 256; const PARSE_CONSTANT = 256;
const DUMP_EMPTY_ARRAY_AS_SEQUENCE = 512;
/** /**
* Parses YAML into a PHP value. * Parses YAML into a PHP value.