Skip to content

Documented the missing YAML flags #9205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions components/yaml.rst
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,30 @@ 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 objects 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:\n foo: bar"

And parse them by using the ``PARSE_OBJECT_FOR_MAP`` flag::

$parsed = Yaml::parse($dumped, Yaml::PARSE_OBJECT_FOR_MAP);
var_dump(is_object($parsed)); // true
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.

.. _invalid-types-and-object-serialization:

Handling Invalid Types
Expand Down Expand Up @@ -333,6 +357,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
~~~~~~~~~~~~~~~~~

Expand Down