Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 393d06d

Browse files
committed
[Yaml] add option to dump objects as maps
1 parent 33c797e commit 393d06d

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ public static function dump($value, $exceptionOnInvalidType = false, $flags = 0)
144144
return '!php/object:'.serialize($value);
145145
}
146146

147+
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && $value instanceof \stdClass) {
148+
return self::dumpArray((array) $value, $exceptionOnInvalidType, $flags);
149+
}
150+
147151
if ($exceptionOnInvalidType) {
148152
throw new DumpException('Object support when dumping a YAML file has been disabled.');
149153
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,23 @@ public function getEscapeSequences()
239239
'paragraph-separator' => array("\t\\P", '"\t\\\\P"'),
240240
);
241241
}
242+
243+
public function testDumpObjectAsMap()
244+
{
245+
$bar = new \stdClass();
246+
$bar->class = 'classBar';
247+
$bar->args = array('bar');
248+
$zar = new \stdClass();
249+
$foo = new \stdClass();
250+
$foo->bar = $bar;
251+
$foo->zar = $zar;
252+
$object = new \stdClass();
253+
$object->foo = $foo;
254+
255+
$yaml = $this->dumper->dump($object, 0, 0, false, Yaml::DUMP_OBJECT_AS_MAP);
256+
257+
$this->assertEquals($object, Yaml::parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP));
258+
}
242259
}
243260

244261
class A

src/Symfony/Component/Yaml/Yaml.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Yaml
2424
const PARSE_EXCEPTION_ON_INVALID_TYPE = 2;
2525
const PARSE_OBJECT = 4;
2626
const PARSE_OBJECT_FOR_MAP = 8;
27+
const DUMP_OBJECT_AS_MAP = 16;
2728

2829
/**
2930
* Parses YAML into a PHP value.

0 commit comments

Comments
 (0)