Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d53fe49

Browse filesBrowse files
committed
Make sure that array/object maps are always dumped as maps and indentation is respected
1 parent 44adf72 commit d53fe49
Copy full SHA for d53fe49

File tree

3 files changed

+69
-14
lines changed
Filter options

3 files changed

+69
-14
lines changed

‎src/Symfony/Component/Yaml/Dumper.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Dumper.php
+19-13Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,16 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
8282
$output = '';
8383
$prefix = $indent ? str_repeat(' ', $indent) : '';
8484

85-
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) {
86-
$input = $this->castObjectToArray($input);
87-
}
85+
$isObjectMap = self::isNotEmptyMap($input, $flags);
8886

89-
if ($inline <= 0 || !is_array($input) || empty($input)) {
87+
if ($inline <= 0 || (!is_array($input) && !$isObjectMap) || empty($input)) {
9088
$output .= $prefix.Inline::dump($input, $flags);
9189
} else {
92-
$isAHash = Inline::isHash($input);
90+
$isAHash = $isObjectMap || Inline::isHash($input, $flags);
91+
92+
if ($isObjectMap) {
93+
$input = (array) $input;
94+
}
9395

9496
foreach ($input as $key => $value) {
9597
if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && is_string($value) && false !== strpos($value, "\n")) {
@@ -102,7 +104,7 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
102104
continue;
103105
}
104106

105-
$willBeInlined = $inline - 1 <= 0 || !is_array($value) || empty($value);
107+
$willBeInlined = $inline - 1 <= 0 || (!is_array($value) && !self::isNotEmptyMap($value, $flags)) || empty($value);
106108

107109
$output .= sprintf('%s%s%s%s',
108110
$prefix,
@@ -116,16 +118,20 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
116118
return $output;
117119
}
118120

119-
private function castObjectToArray($object)
121+
private static function isNotEmptyMap($input, $flags)
120122
{
121-
$array = (array) $object;
123+
if ($flags & ~Yaml::DUMP_OBJECT_AS_MAP) {
124+
return false;
125+
}
122126

123-
foreach ($array as $key => $value) {
124-
if ($value instanceof \ArrayObject || $value instanceof \stdClass) {
125-
$array[$key] = $this->castObjectToArray($value);
126-
}
127+
if ($input instanceof \ArrayObject && 0 < count($input)) {
128+
return true;
129+
}
130+
131+
if ($input instanceof \stdClass && 0 < count((array) $input)) {
132+
return true;
127133
}
128134

129-
return $array;
135+
return false;
130136
}
131137
}

‎src/Symfony/Component/Yaml/Inline.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Inline.php
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ public static function dump($value, $flags = 0)
163163
return '!php/object:'.serialize($value);
164164
}
165165

166+
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \stdClass || $value instanceof \ArrayObject)) {
167+
return self::dumpMap((array) $value, $flags);
168+
}
169+
166170
if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
167171
throw new DumpException('Object support when dumping a YAML file has been disabled.');
168172
}
@@ -257,6 +261,19 @@ private static function dumpArray($value, $flags)
257261
return sprintf('[%s]', implode(', ', $output));
258262
}
259263

264+
return self::dumpMap($value, $flags);
265+
}
266+
267+
/**
268+
* Dumps a PHP array to a YAML string as map.
269+
*
270+
* @param array $value The PHP array to dump
271+
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
272+
*
273+
* @return string The YAML string representing the PHP array as map
274+
*/
275+
private static function dumpMap($value, $flags)
276+
{
260277
// hash
261278
$output = array();
262279
foreach ($value as $key => $val) {

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/DumperTest.php
+33-1Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
namespace Symfony\Component\Yaml\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\Yaml\Parser;
1615
use Symfony\Component\Yaml\Dumper;
16+
use Symfony\Component\Yaml\Parser;
1717
use Symfony\Component\Yaml\Yaml;
1818

1919
class DumperTest extends TestCase
@@ -390,6 +390,38 @@ public function testDumpingStdClassInstancesRespectsInlineLevel()
390390
$this->assertSame($expected, $yaml);
391391
}
392392

393+
public function testDumpingArrayObjectInstancesWithNumericKeysRespectsInlineLevel()
394+
{
395+
$deep = new \ArrayObject(array('d', 'e'));
396+
$inner = new \ArrayObject(array('b', 'c', $deep));
397+
$outer = new \ArrayObject(array('a', $inner));
398+
399+
$yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
400+
401+
$expected = <<<YAML
402+
0: a
403+
1:
404+
0: b
405+
1: c
406+
2: { 0: d, 1: e }
407+
408+
YAML;
409+
$this->assertEquals($expected, $yaml);
410+
}
411+
412+
public function testDumpingArrayObjectInstancesWithNumericKeysInlined()
413+
{
414+
$deep = new \ArrayObject(array('d', 'e'));
415+
$inner = new \ArrayObject(array('b', 'c', $deep));
416+
$outer = new \ArrayObject(array('a', $inner));
417+
418+
$yaml = $this->dumper->dump($outer, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
419+
$expected = <<<YAML
420+
{ 0: a, 1: { 0: b, 1: c, 2: { 0: d, 1: e } } }
421+
YAML;
422+
$this->assertEquals($expected, $yaml);
423+
}
424+
393425
public function testDumpMultiLineStringAsScalarBlock()
394426
{
395427
$data = array(

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.