8000 feature #17728 [Yaml] add option to dump objects as maps (xabbuh) · symfony/symfony@34f3294 · GitHub
[go: up one dir, main page]

Skip to content
10000

Commit 34f3294

Browse 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

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines 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

Lines 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

Lines 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