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 34f3294

Browse filesBrowse files
committed
feature #17728 [Yaml] add option to dump objects as maps (xabbuh)
This PR was merged into the 3.1-dev branch. Discussion ---------- [Yaml] add option to dump objects as maps | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #9870, #12860, #15781, #15937, #16266 | License | MIT | Doc PR | TODO Commits ------- 3941d2e [Yaml] add option to dump objects as maps
2 parents c74f1aa + 3941d2e commit 34f3294
Copy full SHA for 34f3294

File tree

3 files changed

+45
-0
lines changed
Filter options

3 files changed

+45
-0
lines changed

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

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

162+
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \stdClass || $value instanceof \ArrayObject)) {
163+
return self::dumpArray((array) $value, $flags);
164+
}
165+
162166
if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
163167
throw new DumpException('Object support when dumping a YAML file has been disabled.');
164168
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/DumperTest.php
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,46 @@ public function testBinaryDataIsDumpedBase64EncodedWithFlag()
292292

293293
$this->assertSame($expected, $this->dumper->dump(array('data' => $binaryData), 0, 0, Yaml::DUMP_BASE64_BINARY_DATA));
294294
}
295+
296+
/**
297+
* @dataProvider objectAsMapProvider
298+
*/
299+
public function testDumpObjectAsMap($object, $expected)
300+
{
301+
302+
$yaml = $this->dumper->dump($object, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
303+
304+
$this->assertEquals($expected, Yaml::parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
305+
}
306+
307+
public function objectAsMapProvider()
308+
{
309+
$tests = array();
310+
311+
$bar = new \stdClass();
312+
$bar->class = 'classBar';
313+
$bar->args = array('bar');
314+
$zar = new \stdClass();
315+
$foo = new \stdClass();
316+
$foo->bar = $bar;
317+
$foo->zar = $zar;
318+
$object = new \stdClass();
319+
$object->foo = $foo;
320+
$tests['stdClass'] = array($object, $object);
321+
322+
$arrayObject = new \ArrayObject();
323+
$arrayObject['foo'] = 'bar';
324+
$arrayObject['baz'] = 'foobar';
325+
$parsedArrayObject = new \stdClass();
326+
$parsedArrayObject->foo = 'bar';
327+
$parsedArrayObject->baz = 'foobar';
328+
$tests['ArrayObject'] = array($arrayObject, $parsedArrayObject);
329+
330+
$a = new A();
331+
$tests['arbitrary-object'] = array($a, null);
332+
333+
return $tests;
334+
}
295335
}
296336

297337
class A

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Yaml.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Yaml
2727
const DUMP_EXCEPTION_ON_INVALID_TYPE = 16;
2828
const PARSE_DATETIME = 32;
2929
const DUMP_BASE64_BINARY_DATA = 64;
30+
const DUMP_OBJECT_AS_MAP = 128;
3031

3132
/**
3233
* Parses YAML into a PHP value.

0 commit comments

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