@@ -127,17 +127,6 @@ error occurred:
127
127
128
128
.. _components-yaml-dump :
129
129
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
-
141
130
Writing YAML Files
142
131
~~~~~~~~~~~~~~~~~~
143
132
@@ -214,30 +203,27 @@ changed using the third argument as follows::
214
203
foo : bar
215
204
bar : baz
216
205
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
+ ---------------------
222
208
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.
225
212
226
- // throw an exception if a resource or object is encountered
227
- Yaml::dump($data, 2, 4, true);
213
+ Object Parsing and Dumping
214
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
228
215
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::
233
217
234
218
$object = new \stdClass();
235
219
$object->foo = 'bar';
236
220
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::
239
225
240
- $parsed = Yaml::parse($dumped, false, true );
226
+ $parsed = Yaml::parse($dumped, Yaml::PARSE_OBJECT );
241
227
var_dump(is_object($parsed)); // true
242
228
echo $parsed->foo; // bar
243
229
@@ -250,6 +236,63 @@ representation of the object.
250
236
parsers will likely not recognize the ``php/object `` tag and non-PHP
251
237
implementations certainly won't - use with discretion!
252
238
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
+
253
296
Learn More
254
297
----------
255
298
@@ -262,3 +305,4 @@ Learn More
262
305
.. _YAML : http://yaml.org/
263
306
.. _Packagist : https://packagist.org/packages/symfony/yaml
264
307
.. _`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