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 44adf72

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

File tree

Expand file treeCollapse file tree

3 files changed

+74
-23
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+74
-23
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Dumper.php
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ 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+
}
88+
8589
if ($inline <= 0 || !is_array($input) || empty($input)) {
8690
$output .= $prefix.Inline::dump($input, $flags);
8791
} else {
@@ -111,4 +115,17 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
111115

112116
return $output;
113117
}
118+
119+
private function castObjectToArray($object)
120+
{
121+
$array = (array) $object;
122+
123+
foreach ($array as $key => $value) {
124+
if ($value instanceof \ArrayObject || $value instanceof \stdClass) {
125+
$array[$key] = $this->castObjectToArray($value);
126+
}
127+
}
128+
129+
return $array;
130+
}
114131
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Inline.php
-4Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,6 @@ 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::dumpArray((array) $value, $flags);
168-
}
169-
170166
if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
171167
throw new DumpException('Object support when dumping a YAML file has been disabled.');
172168
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/DumperTest.php
+57-19Lines changed: 57 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,63 @@ public function objectAsMapProvider()
352333
return $tests;
353334
}
354335

336+
public function testDumpEmptyArrayObjectInstanceAsMap()
337+
{
338+
$this->assertSame('{ }', $this->dumper->dump(new \ArrayObject(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
339+
}
340+
341+
public function testDumpingArrayObjectInstancesRespectsInlineLevel()
342+
{
343+
$deep = new \ArrayObject(array('deep1' => 'd', 'deep2' => 'e'));
344+
$inner = new \ArrayObject(array('inner1' => 'b', 'inner2' => 'c', 'inner3' => $deep));
345+
$outer = new \ArrayObject(array('outer1' => 'a', 'outer2' => $inner));
346+
347+
$yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
348+
349+
$expected = <<<YAML
350+
outer1: a
351+
outer2:
352+
inner1: b
353+
inner2: c
354+
inner3: { deep1: d, deep2: e }
355+
356+
YAML;
357+
$this->assertSame($expected, $yaml);
358+
}
359+
360+
public function testDumpEmptyStdClassInstanceAsMap()
361+
{
362+
$this->assertSame('{ }', $this->dumper->dump(new \stdClass(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
363+
}
364+
365+
public function testDumpingStdClassInstancesRespectsInlineLevel()
366+
{
367+
$deep = new \stdClass();
368+
$deep->deep1 = 'd';
369+
$deep->deep2 = 'e';
370+
371+
$inner = new \stdClass();
372+
$inner->inner1 = 'b';
373+
$inner->inner2 = 'c';
374+
$inner->inner3 = $deep;
375+
376+
$outer = new \stdClass();
377+
$outer->outer1 = 'a';
378+
$outer->outer2 = $inner;
379+
380+
$yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
381+
382+
$expected = <<<YAML
383+
outer1: a
384+
outer2:
385+
inner1: b
386+
inner2: c
387+
inner3: { deep1: d, deep2: e }
388+
389+
YAML;
390+
$this->assertSame($expected, $yaml);
391+
}
392+
355393
public function testDumpMultiLineStringAsScalarBlock()
356394
{
357395
$data = array(

0 commit comments

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