Skip to content

Commit 852ce22

Browse files
committed
feature #6622 Documentation for YAML flags added in 3.1 (dantleech)
This PR was merged into the 3.1 branch. Discussion ---------- Documentation for YAML flags added in 3.1 Following on from #6590 this PR documents the YAML flags introduced in Symfony 3.1 Commits ------- 3895fc9 Documentation for YAML flags added in 3.1
2 parents 87cbecd + 3895fc9 commit 852ce22

File tree

1 file changed

+71
-27
lines changed

1 file changed

+71
-27
lines changed

Diff for: components/yaml.rst

+71-27
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,63 @@ 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+
echo $yaml; // { foo: bar }
259+
260+
Date Handling
261+
~~~~~~~~~~~~~
262+
263+
By default the YAML parser will convert unquoted strings which look like a
264+
date or a date-time into a Unix timestamp; for example ``2016-05-27`` or
265+
``2016-05-27T02:59:43.1Z`` (ISO-8601_)::
266+
267+
Yaml::parse('2016-05-27'); // 1464307200
268+
269+
You can make it convert to a ``DateTime`` instance by using the ``PARSE_DATETIME``
270+
flag::
271+
272+
$date = Yaml::parse('2016-05-27', Yaml::PARSE_DATETIME);
273+
var_dump(get_class($date)); // DateTime
274+
275+
Dumping Multi-line Literal Blocks
276+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
277+
278+
In YAML multiple lines can be represented as literal blocks, by default the
279+
dumper will encode multiple lines as an inline string::
280+
281+
$string = array("string" => "Multiple\nLine\nString");
282+
$yaml = Yaml::dump($string);
283+
echo $yaml; // string: "Multiple\nLine\nString"
284+
285+
You can make it use a literal block with the ``DUMP_MULTI_LINE_LITERAL_BLOCK``
286+
flag::
287+
288+
$string = array("string" => "Multiple\nLine\nString");
289+
$yaml = Yaml::dump($string, 2, 4, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
290+
echo $yaml;
291+
// string: |
292+
// Multiple
293+
// Line
294+
// String
295+
253296
Learn More
254297
----------
255298

@@ -262,3 +305,4 @@ Learn More
262305
.. _YAML: http://yaml.org/
263306
.. _Packagist: https://packagist.org/packages/symfony/yaml
264307
.. _`YAML 1.2 version specification`: http://yaml.org/spec/1.2/spec.html
308+
.. _ISO-8601: http://www.iso.org/iso/iso8601

0 commit comments

Comments
 (0)