8000 bug #22409 [Yaml] respect inline level when dumping objects as maps (… · symfony/symfony@8f4eb92 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8f4eb92

Browse files
committed
bug #22409 [Yaml] respect inline level when dumping objects as maps (goetas, xabbuh)
This PR was merged into the 3.2 branch. Discussion ---------- [Yaml] respect inline level when dumping objects as maps | Q | A | ------------- | --- | Branch? | 3.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #22392 | License | MIT | Doc PR | Commits ------- 3cca48c respect inline level when dumping objects as maps 4f5c149 Test case for not in-lined map-objects
2 parents 329e96c + 3cca48c commit 8f4eb92

File tree

3 files changed

+110
-8
lines changed

3 files changed

+110
-8
lines changed

src/Symfony/Component/Yaml/Dumper.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,20 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
8181

8282
$output = '';
8383
$prefix = $indent ? str_repeat(' ', $indent) : '';
84+
$dumpObjectAsInlineMap = true;
8485

85-
if ($inline <= 0 || !is_array($input) || empty($input)) {
86+
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) {
87+
$dumpObjectAsInlineMap = empty((array) $input);
88+
}
89+
90+
if ($inline <= 0 || (!is_array($input) && $dumpObjectAsInlineMap) || empty($input)) {
8691
$output .= $prefix.Inline::dump($input, $flags);
8792
} else {
88-
$isAHash = Inline::isHash($input);
93+
$dumpAsMap = Inline::isHash($input);
8994

9095
foreach ($input as $key => $value) {
9196
if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && is_string($value) && false !== strpos($value, "\n")) {
92-
$output .= sprintf("%s%s%s |\n", $prefix, $isAHash ? Inline::dump($key, $flags).':' : '-', '');
97+
$output .= sprintf("%s%s%s |\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '');
9398

9499
foreach (preg_split('/\n|\r\n/', $value) as $row) {
95100
$output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
@@ -98,11 +103,17 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
98103
continue;
99104
}
100105

101-
$willBeInlined = $inline - 1 <= 0 || !is_array($value) || empty($value);
106+
$dumpObjectAsInlineMap = true;
107+
108+
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
109+
$dumpObjectAsInlineMap = empty((array) $value);
110+
}
111+
112+
$willBeInlined = $inline - 1 <= 0 || !is_array($value) && $dumpObjectAsInlineMap || empty($value);
102113

103114
$output .= sprintf('%s%s%s%s',
104115
$prefix,
105-
$isAHash ? Inline::dump($key, $flags).':' : '-',
116+
$dumpAsMap ? Inline::dump($key, $flags).':' : '-',
106117
$willBeInlined ? ' ' : "\n",
107118
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
108119
).($willBeInlined ? "\n" : '');

src/Symfony/Component/Yaml/Inline.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public static function dump($value, $flags = 0)
164164
}
165165

166166
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \stdClass || $value instanceof \ArrayObject)) {
167-
return self::dumpArray((array) $value, $flags);
167+
return self::dumpArray($value, $flags);
168168
}
169169

170170
if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
@@ -224,12 +224,16 @@ public static function dump($value, $flags = 0)
224224
*
225225
* @internal
226226
*
227-
* @param array $value The PHP array to check
227+
* @param array|\ArrayObject|\stdClass $value The PHP array or array-like object to check
228228
*
229229
* @return bool true if value is hash array, false otherwise
230230
*/
231-
public static function isHash(array $value)
231+
public static function isHash($value)
232232
{
233+
if ($value instanceof \stdClass || $value instanceof \ArrayObject) {
234+
return true;
235+
}
236+
233237
$expectedKey = 0;
234238

235239
foreach ($value as $key => $val) {

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

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,93 @@ public function objectAsMapProvider()
333333
return $tests;
334334
}
335335

336+
public function testDumpingArrayObjectInstancesRespectsInlineLevel()
337+
{
338+
$deep = new \ArrayObject(array('deep1' => 'd', 'deep2' => 'e'));
339+
$inner = new \ArrayObject(array('inner1' => 'b', 'inner2' => 'c', 'inner3' => $deep));
340+
$outer = new \ArrayObject(array('outer1' => 'a', 'outer2' => $inner));
341+
342+
$yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
343+
344+
$expected = <<<YAML
345+
outer1: a
346+
outer2:
347+
inner1: b
348+
inner2: c
349+
inner3: { deep1: d, deep2: e }
350+
351+
YAML;
352+
$this->assertSame($expected, $yaml);
353+
}
354+
355+
public function testDumpingArrayObjectInstancesWithNumericKeysInlined()
356+
{
357+
$deep = new \ArrayObject(array('d', 'e'));
358+
$inner = new \ArrayObject(array('b', 'c', $deep));
359+
$outer = new \ArrayObject(array('a', $inner));
360+
361+
$yaml = $this->dumper->dump($outer, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
362+
$expected = <<<YAML
363+
{ 0: a, 1: { 0: b, 1: c, 2: { 0: d, 1: e } } }
364+
YAML;
365+
$this->assertSame($expected, $yaml);
366+
}
367+
368+
public function testDumpingArrayObjectInstancesWithNumericKeysRespectsInlineLevel()
369+
{
370+
$deep = new \ArrayObject(array('d', 'e'));
371+
$inner = new \ArrayObject(array('b', 'c', $deep));
372+
$outer = new \ArrayObject(array('a', $inner));
373+
$yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
374+
$expected = <<<YAML
375+
0: a
376+
1:
377+
0: b
378+
1: c
379+
2: { 0: d, 1: e }
380+
381+
YAML;
382+
$this->assertEquals($expected, $yaml);
383+
}
384+
385+
public function testDumpEmptyArrayObjectInstanceAsMap()
386+
{
387+
$this->assertSame('{ }', $this->dumper->dump(new \ArrayObject(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
388+
}
389+
390+
public function testDumpEmptyStdClassInstanceAsMap()
391+
{
392+
$this->assertSame('{ }', $this->dumper->dump(new \stdClass(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
393+
}
394+
395+
public function testDumpingStdClassInstancesRespectsInlineLevel()
396+
{
397+
$deep = new \stdClass();
398+
$deep->deep1 = 'd';
399+
$deep->deep2 = 'e';
400+
401+
$inner = new \stdClass();
402+
$inner->inner1 = 'b';
403+
$inner->inner2 = 'c';
404+
$inner->inner3 = $deep;
405+
406+
$outer = new \stdClass();
407+
$outer->outer1 = 'a';
408+
$outer->outer2 = $inner;
409+
410+
$yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
411+
412+
$expected = <<<YAML
413+
outer1: a
414+
outer2:
415+
inner1: b
416+
inner2: c
417+
inner3: { deep1: d, deep2: e }
418+
419+
YAML;
420+
$this->assertSame($expected, $yaml);
421+
}
422+
336423
public function testDumpMultiLineStringAsScalarBlock()
337424
{
338425
$data = array(

0 commit comments

Comments
 (0)
0