From 486c0c338dd870a448d1feca4a3cf05be3f195b3 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 5 Feb 2018 11:22:51 +0100 Subject: [PATCH 1/2] Documented the missing YAML flags --- components/yaml.rst | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/components/yaml.rst b/components/yaml.rst index 2c1d5cd1df7..84b3acc9b1e 100644 --- a/components/yaml.rst +++ b/components/yaml.rst @@ -267,6 +267,29 @@ representation of the object. parsers will likely not recognize the ``php/object`` tag and non-PHP implementations certainly won't - use with discretion! +Parsing and Dumping Objects as Maps +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. versionadded:: 3.2 + Support for parsing and dumping objectas as maps was introduced in Symfony 3.2. + +You can dump objects as Yaml maps by using the ``DUMP_OBJECT_AS_MAP`` flag:: + + $object = new \stdClass(); + $object->foo = 'bar'; + + $dumped = Yaml::dump(array('data' => $object), 2, 4, Yaml::DUMP_OBJECT_AS_MAP); + // $dumped = "data:\nfoo:\nSbar" + +And parse them by using the ``PARSE_OBJECT_FOR_MAP`` flag:: + + $parsed = Yaml::parse($dumped, Yaml::PARSE_OBJECT); + var_dump(is_object($parsed)); // true + echo $parsed->foo; // bar + +The YAML component uses PHP's ``(array)`` casting to generate a string +representation of the object as a map. + .. _invalid-types-and-object-serialization: Handling Invalid Types @@ -333,6 +356,26 @@ syntax to parse them as proper PHP constants:: $parameters = Yaml::parse($yaml, Yaml::PARSE_CONSTANT); // $parameters = array('foo' => 'PHP_INT_SIZE', 'bar' => 8); +Parsing and Dumping of Binary Data +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. versionadded:: 3.2 + Support for parsing and dumping binary data was introduced in Symfony 3.2. + +You can dump binary data by using the ``DUMP_BASE64_BINARY_DATA`` flag:: + + $imageContents = file_get_contents(__DIR__.'/images/logo.png'); + + $dumped = Yaml::dump(array('logo' => $imageContents), 2, 4, Yaml::DUMP_BASE64_BINARY_DATA); + // logo: !!binary iVBORw0KGgoAAAANSUhEUgAAA6oAAADqCAY... + +Binary data is automatically parsed if they include the ``!!binary`` YAML tag +(there's no need to pass any flag to the Yaml parser):: + + $dumped = 'logo: !!binary iVBORw0KGgoAAAANSUhEUgAAA6oAAADqCAY...'; + $parsed = Yaml::parse($dumped); + $imageContents = $parsed['logo']; + Syntax Validation ~~~~~~~~~~~~~~~~~ From a235d7110f1e2dc75b484dc9608ed9b64e88df6f Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 6 Feb 2018 10:14:47 +0100 Subject: [PATCH 2/2] Fixed issues --- components/yaml.rst | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/components/yaml.rst b/components/yaml.rst index 84b3acc9b1e..408ff7a615c 100644 --- a/components/yaml.rst +++ b/components/yaml.rst @@ -271,7 +271,7 @@ Parsing and Dumping Objects as Maps ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. versionadded:: 3.2 - Support for parsing and dumping objectas as maps was introduced in Symfony 3.2. + Support for parsing and dumping objects as maps was introduced in Symfony 3.2. You can dump objects as Yaml maps by using the ``DUMP_OBJECT_AS_MAP`` flag:: @@ -279,13 +279,14 @@ You can dump objects as Yaml maps by using the ``DUMP_OBJECT_AS_MAP`` flag:: $object->foo = 'bar'; $dumped = Yaml::dump(array('data' => $object), 2, 4, Yaml::DUMP_OBJECT_AS_MAP); - // $dumped = "data:\nfoo:\nSbar" + // $dumped = "data:\n foo: bar" And parse them by using the ``PARSE_OBJECT_FOR_MAP`` flag:: - $parsed = Yaml::parse($dumped, Yaml::PARSE_OBJECT); + $parsed = Yaml::parse($dumped, Yaml::PARSE_OBJECT_FOR_MAP); var_dump(is_object($parsed)); // true - echo $parsed->foo; // bar + var_dump(is_object($parsed->data)); // true + echo $parsed->data->foo; // bar The YAML component uses PHP's ``(array)`` casting to generate a string representation of the object as a map.