Skip to content

Commit d682ede

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

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
@@ -127,20 +127,6 @@ error occurred:
127127
128128
.. _components-yaml-dump:
129129

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
143-
144130
Writing YAML Files
145131
~~~~~~~~~~~~~~~~~~
146132

@@ -214,30 +200,27 @@ changed using the third argument as follows::
214200
foo: bar
215201
bar: baz
216202
217-
Invalid Types and Object Serialization
218-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
203+
Advanced Usage: Flags
204+
---------------------
219205

220-
By default the YAML component will encode any "unsupported" type (i.e.
221-
resources and objects) as ``null``.
206+
.. versionadded:: 3.1
207+
Flags were added in version 3.1 and replaced the earlier boolean
208+
arguments.
222209

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::
210+
Object Parsing and Dumping
211+
..........................
225212

226-
// throw an exception if a resource or object is encountered
227-
Yaml::dump($data, 2, 4, true);
228-
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::
213+
You can dump objects by using the ``DUMP_OBJECT`` flag::
233214

234215
$object = new \stdClass();
235216
$object->foo = 'bar';
236217

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

240-
$parsed = Yaml::parse($dumped, false, true);
223+
$parsed = Yaml::parse($yaml, Yaml::PARSE_OBJECT);
241224
var_dump(is_object($parsed)); // true
242225
echo $parsed->foo; // bar
243226

@@ -250,6 +233,77 @@ representation of the object.
250233
parsers will likely not recognize the ``php/object`` tag and non-PHP
251234
implementations certainly won't - use with discretion!
252235

236+
Handling Invalid Types
237+
......................
238+
239+
By default the parser will encode invalid types as "null". You can make the
240+
parser throw exceptions by using the ``PARSE_EXCEPTION_ON_INVALID_TYPE``
241+
flag::
242+
243+
$yaml = '!php/object:O:8:"stdClass":1:{s:5:"foo";s:7:"bar";}';
244+
Yaml::parse($yaml, Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE); // throws an exception
245+
246+
Similarly you can use ``DUMP_EXCEPTION_ON_INVALID_TYPE`` when dumping::
247+
248+
$data = new \stdClass(); // by default objects are invalid.
249+
Yaml::parse($data, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE); // throws an exception
250+
251+
Using Objects for Maps
252+
......................
253+
254+
.. versionadded:: 2.7
255+
Support for parsing mappings as objects was introduced in Symfony 2.7.
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)