Skip to content

Commit 2d8680c

Browse files
committed
Documentation for YAML flags added in 3.1
1 parent 0e71066 commit 2d8680c

File tree

1 file changed

+84
-30
lines changed

1 file changed

+84
-30
lines changed

components/yaml/introduction.rst

Lines changed: 84 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,7 @@ error occurred:
126126
}
127127
128128
.. _components-yaml-dump:
129-
130-
Objects for Mappings
131-
....................
132-
133-
.. versionadded:: 2.7
134-
Support for parsing mappings as objects was introduced in Symfony 2.7.
135-
136-
Yaml :ref:`mappings <yaml-format-collections>` are basically associative
137-
arrays. You can instruct the parser to return mappings as objects (i.e.
138-
``\stdClass`` instances) by setting the fourth argument to ``true``::
139-
140-
$object = Yaml::parse('{"foo": "bar"}', false, false, true);
141-
echo get_class($object); // stdClass
142-
echo $object->foo; // bar
129+
.. _objects-for-mappings:
143130

144131
Writing YAML Files
145132
~~~~~~~~~~~~~~~~~~
@@ -214,30 +201,27 @@ changed using the third argument as follows::
214201
foo: bar
215202
bar: baz
216203
217-
Invalid Types and Object Serialization
218-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
219-
220-
By default the YAML component will encode any "unsupported" type (i.e.
221-
resources and objects) as ``null``.
222-
223-
Instead of encoding as ``null`` you can choose to throw an exception if an invalid
224-
type is encountered in either the dumper or parser as follows::
204+
Advanced Usage: Flags
205+
---------------------
225206

226-
// throw an exception if a resource or object is encountered
227-
Yaml::dump($data, 2, 4, true);
207+
.. versionadded:: 3.1
208+
Flags were introduced in Symfony 3.1 and replaced the earlier boolean
209+
arguments.
228210

229-
// throw an exception if an encoded object is found in the YAML string
230-
Yaml::parse($yaml, true);
211+
Object Parsing and Dumping
212+
~~~~~~~~~~~~~~~~~~~~~~~~~~
231213

232-
However, you can activate object support using the next argument::
214+
You can dump objects by using the ``DUMP_OBJECT`` flag::
233215

234216
$object = new \stdClass();
235217
$object->foo = 'bar';
236218

237-
$dumped = Yaml::dump($object, 2, 4, false, true);
238-
// !!php/object:O:8:"stdClass":1:{s:5:"foo";s:7:"bar";}
219+
$dumped = Yaml::dump($object, 2, 4, Yaml::DUMP_OBJECT);
220+
// !php/object:O:8:"stdClass":1:{s:5:"foo";s:7:"bar";}
239221

240-
$parsed = Yaml::parse($dumped, false, true);
222+
And parse them by using the ``PARSE_OBJECT`` flag::
223+
224+
$parsed = Yaml::parse($dumped, Yaml::PARSE_OBJECT);
241225
var_dump(is_object($parsed)); // true
242226
echo $parsed->foo; // bar
243227

@@ -250,6 +234,76 @@ representation of the object.
250234
parsers will likely not recognize the ``php/object`` tag and non-PHP
251235
implementations certainly won't - use with discretion!
252236

237+
.. _invalid-types-and-object-serialization:
238+
239+
Handling Invalid Types
240+
~~~~~~~~~~~~~~~~~~~~~~
241+
242+
By default the parser will encode invalid types as "null". You can make the
243+
parser throw exceptions by using the ``PARSE_EXCEPTION_ON_INVALID_TYPE``
244+
flag::
245+
246+
$yaml = '!php/object:O:8:"stdClass":1:{s:5:"foo";s:7:"bar";}';
247+
Yaml::parse($yaml, Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE); // throws an exception
248+
249+
Similarly you can use ``DUMP_EXCEPTION_ON_INVALID_TYPE`` when dumping::
250+
251+
$data = new \stdClass(); // by default objects are invalid.
252+
Yaml::parse($data, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE); // throws an exception
253+
254+
Using Objects for Maps
255+
~~~~~~~~~~~~~~~~~~~~~~
256+
257+
Yaml :ref:`mappings <yaml-format-collections>` are basically associative
258+
arrays. You can instruct the parser to return mappings as objects (i.e.
259+
``\stdClass`` instances) by using the ``PARSE_OBJECT_FOR_MAP`` flag::
260+
261+
$object = Yaml::parse('{"foo": "bar"}', Yaml::PARSE_OBJECT_FOR_MAP);
262+
echo get_class($object); // stdClass
263+
echo $object->foo; // bar
264+
265+
and similarly, to encode objects as maps use ``DUMP_OBJECT_AS_MAP``::
266+
267+
$yaml = Yaml::dump($object, 2, 4, Yaml::DUMP_OBJECT_AS_MAP);
268+
echo $yaml; // { foo: bar }
269+
270+
Date Handling
271+
~~~~~~~~~~~~~
272+
273+
By default the YAML parser will convert unquoted strings which look like a
274+
date or a date-time into a Unix timestamp; for example ``2016-05-27`` or
275+
``2016-05-27T02:59:43.1Z`` (ISO-8601_)::
276+
277+
Yaml::parse('2016-05-27'); // 1464307200
278+
279+
You can make it convert to a ``DateTime`` instance by using the ``PARSE_DATETIME``
280+
flag::
281+
282+
$date = Yaml::parse('2016-05-27', Yaml::PARSE_DATETIME);
283+
var_dump(get_class($date)); // DateTime
284+
285+
Dumping Multi-line Literal Blocks
286+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
287+
288+
In YAML multiple lines can be represented as literal blocks, by default the
289+
dumper will encode multiple lines as an inline string::
290+
291+
$string = ["string" => "Multiple\nLine\nString"];
292+
$yaml = Yaml::dump($string);
293+
echo $yaml; // string: "Multiple\nLine\nString"
294+
295+
You can make it use a literal block with the ``DUMP_MULTI_LINE_LITERAL_BLOCK``
296+
flag::
297+
298+
$string = ["string" => "Multiple\nLine\nString"];
299+
$yaml = Yaml::dump($string, 2, 4. Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
300+
echo $yaml;
301+
// string: |
302+
// Multiple
303+
// Line
304+
// String
305+
253306
.. _YAML: http://yaml.org/
254307
.. _Packagist: https://packagist.org/packages/symfony/yaml
255308
.. _`YAML 1.2 version specification`: http://yaml.org/spec/1.2/spec.html
309+
.. _ISO-8601: http://www.iso.org/iso/iso8601

0 commit comments

Comments
 (0)