Skip to content

Commit 2bb32cf

Browse files
committed
Documentation for YAML flags added in 3.1
1 parent feb03f3 commit 2bb32cf

File tree

1 file changed

+85
-27
lines changed

1 file changed

+85
-27
lines changed

components/yaml/introduction.rst

Lines changed: 85 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,6 @@ error occurred:
127127
128128
.. _components-yaml-dump:
129129

130-
Objects for Mappings
131-
....................
132-
133-
Yaml :ref:`mappings <yaml-format-collections>` are basically associative
134-
arrays. You can instruct the parser to return mappings as objects (i.e.
135-
``\stdClass`` instances) by setting the fourth argument to ``true``::
136-
137-
$object = Yaml::parse('{"foo": "bar"}', false, false, true);
138-
echo get_class($object); // stdClass
139-
echo $object->foo; // bar
140-
141130
Writing YAML Files
142131
~~~~~~~~~~~~~~~~~~
143132

@@ -214,30 +203,27 @@ changed using the third argument as follows::
214203
foo: bar
215204
bar: baz
216205
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``.
206+
Advanced Usage: Flags
207+
---------------------
222208

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::
209+
.. versionadded:: 3.1
210+
Flags were introduced in Symfony 3.1 and replaced the earlier boolean
211+
arguments.
225212

226-
// throw an exception if a resource or object is encountered
227-
Yaml::dump($data, 2, 4, true);
213+
Object Parsing and Dumping
214+
~~~~~~~~~~~~~~~~~~~~~~~~~~
228215

229-
// throw an exception if an encoded object is found in the YAML string
230-
Yaml::parse($yaml, true);
231-
232-
However, you can activate object support using the next argument::
216+
You can dump objects by using the ``DUMP_OBJECT`` flag::
233217

234218
$object = new \stdClass();
235219
$object->foo = 'bar';
236220

237-
$dumped = Yaml::dump($object, 2, 4, false, true);
238-
// !!php/object:O:8:"stdClass":1:{s:5:"foo";s:7:"bar";}
221+
$dumped = Yaml::dump($object, 2, 4, Yaml::DUMP_OBJECT);
222+
// !php/object:O:8:"stdClass":1:{s:5:"foo";s:7:"bar";}
223+
224+
And parse them by using the ``PARSE_OBJECT`` flag::
239225

240-
$parsed = Yaml::parse($dumped, false, true);
226+
$parsed = Yaml::parse($dumped, Yaml::PARSE_OBJECT);
241227
var_dump(is_object($parsed)); // true
242228
echo $parsed->foo; // bar
243229

@@ -250,6 +236,78 @@ representation of the object.
250236
parsers will likely not recognize the ``php/object`` tag and non-PHP
251237
implementations certainly won't - use with discretion!
252238

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

0 commit comments

Comments
 (0)