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 1 commit
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
43 changes: 43 additions & 0 deletions components/yaml.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

objects


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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data:\n foo:\nbar because of the indentation, right?


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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if $dumped has the same value as above, we would need to account for data here or omit it above while dumping


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 +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
~~~~~~~~~~~~~~~~~

Expand Down