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 fe515b3

Browse filesBrowse files
committed
respect inline level when dumping objects as maps
1 parent 4f5c149 commit fe515b3
Copy full SHA for fe515b3

File tree

3 files changed

+90
-26
lines changed
Filter options

3 files changed

+90
-26
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Dumper.php
+13-4Lines changed: 13 additions & 4 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,15 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
98103
continue;
99104
}
100105

106+
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
107+
$value = (array) $value;
108+
}
109+
101110
$willBeInlined = $inline - 1 <= 0 || !is_array($value) || empty($value);
102111

103112
$output .= sprintf('%s%s%s%s',
104113
$prefix,
105-
$isAHash ? Inline::dump($key, $flags).':' : '-',
114+
$dumpAsMap ? Inline::dump($key, $flags).':' : '-',
106115
$willBeInlined ? ' ' : "\n",
107116
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
108117
).($willBeInlined ? "\n" : '');

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Inline.php
+7-3Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/DumperTest.php
+70-19Lines changed: 70 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -206,25 +206,6 @@ public function testInlineLevel()
206206
$this->assertEquals($expected, $this->dumper->dump($this->array, 10), '->dump() takes an inline level argument');
207207
}
208208

209-
public function testArrayObjectAsMapNotInLined()
210-
{
211-
$deep = new \ArrayObject(array('deep1' => 'd', 'deep2' => 'e'));
212-
$inner = new \ArrayObject(array('inner1' => 'b', 'inner2' => 'c', 'inner3' => $deep));
213-
$outer = new \ArrayObject(array('outer1' => 'a', 'outer1' => $inner));
214-
215-
$yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
216-
217-
$expected = <<<YAML
218-
outer1: a
219-
outer2:
220-
inner1: b
221-
inner2: c
222-
inner3: { deep1: d, deep2: e }
223-
224-
YAML;
225-
$this->assertEquals($expected, $yaml);
226-
}
227-
228209
public function testObjectSupportEnabled()
229210
{
230211
$dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_OBJECT);
@@ -352,6 +333,76 @@ public function objectAsMapProvider()
352333
return $tests;
353334
}
354335

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 testDumpEmptyArrayObjectInstanceAsMap()
369+
{
370+
$this->assertSame('{ }', $this->dumper->dump(new \ArrayObject(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
371+
}
372+
373+
public function testDumpEmptyStdClassInstanceAsMap()
374+
{
375+
$this->assertSame('{ }', $this->dumper->dump(new \stdClass(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
376+
}
377+
378+
public function testDumpingStdClassInstancesRespectsInlineLevel()
379+
{
380+
$deep = new \stdClass();
381+
$deep->deep1 = 'd';
382+
$deep->deep2 = 'e';
383+
384+
$inner = new \stdClass();
385+
$inner->inner1 = 'b';
386+
$inner->inner2 = 'c';
387+
$inner->inner3 = $deep;
388+
389+
$outer = new \stdClass();
390+
$outer->outer1 = 'a';
391+
$outer->outer2 = $inner;
392+
393+
$yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
394+
395+
$expected = <<<YAML
396+
outer1: a
397+
outer2:
398+
inner1: b
399+
inner2: c
400+
inner3: { deep1: d, deep2: e }
401+
402+
YAML;
403+
$this->assertSame($expected, $yaml);
404+
}
405+
355406
public function testDumpMultiLineStringAsScalarBlock()
356407
{
357408
$data = array(

0 commit comments

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