@@ -127,20 +127,6 @@ error occurred:
127
127
128
128
.. _components-yaml-dump :
129
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
143
-
144
130
Writing YAML Files
145
131
~~~~~~~~~~~~~~~~~~
146
132
@@ -214,30 +200,27 @@ changed using the third argument as follows::
214
200
foo : bar
215
201
bar : baz
216
202
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::
203
+ Advanced Usage: Flags
204
+ ---------------------
225
205
226
- // throw an exception if a resource or object is encountered
227
- Yaml::dump($data, 2, 4, true);
206
+ .. versionadded :: 3.1
207
+ Flags were introduced in Symfony 3.1 and replaced the earlier boolean
208
+ arguments.
228
209
229
- // throw an exception if an encoded object is found in the YAML string
230
- Yaml::parse($yaml, true);
210
+ Object Parsing and Dumping
211
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
231
212
232
- However, you can activate object support using the next argument ::
213
+ You can dump objects by using the `` DUMP_OBJECT `` flag ::
233
214
234
215
$object = new \stdClass();
235
216
$object->foo = 'bar';
236
217
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";}
239
220
240
- $parsed = Yaml::parse($dumped, false, true);
221
+ And parse them by using the ``PARSE_OBJECT `` flag::
222
+
223
+ $parsed = Yaml::parse($dumped, Yaml::PARSE_OBJECT);
241
224
var_dump(is_object($parsed)); // true
242
225
echo $parsed->foo; // bar
243
226
@@ -250,6 +233,76 @@ representation of the object.
250
233
parsers will likely not recognize the ``php/object `` tag and non-PHP
251
234
implementations certainly won't - use with discretion!
252
235
236
+ .. _invalid-types-and-object-serialization :
237
+
238
+ Handling Invalid Types
239
+ ~~~~~~~~~~~~~~~~~~~~~~
240
+
241
+ By default the parser will encode invalid types as "null". You can make the
242
+ parser throw exceptions by using the ``PARSE_EXCEPTION_ON_INVALID_TYPE ``
243
+ flag::
244
+
245
+ $yaml = '!php/object:O:8:"stdClass":1:{s:5:"foo";s:7:"bar";}';
246
+ Yaml::parse($yaml, Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE); // throws an exception
247
+
248
+ Similarly you can use ``DUMP_EXCEPTION_ON_INVALID_TYPE `` when dumping::
249
+
250
+ $data = new \stdClass(); // by default objects are invalid.
251
+ Yaml::parse($data, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE); // throws an exception
252
+
253
+ Using Objects for Maps
254
+ ~~~~~~~~~~~~~~~~~~~~~~
255
+
256
+ Yaml :ref: `mappings <yaml-format-collections >` are basically associative
257
+ arrays. You can instruct the parser to return mappings as objects (i.e.
258
+ ``\stdClass `` instances) by using the ``PARSE_OBJECT_FOR_MAP `` flag::
259
+
260
+ $object = Yaml::parse('{"foo": "bar"}', Yaml::PARSE_OBJECT_FOR_MAP);
261
+ echo get_class($object); // stdClass
262
+ echo $object->foo; // bar
263
+
264
+ and similarly, to encode objects as maps use ``DUMP_OBJECT_AS_MAP ``::
265
+
266
+ $yaml = Yaml::dump($object, 2, 4, Yaml::DUMP_OBJECT_AS_MAP);
267
+ echo $yaml; // { foo: bar }
268
+
269
+ Date Handling
270
+ ~~~~~~~~~~~~~
271
+
272
+ By default the YAML parser will convert unquoted strings which look like a
273
+ date or a date-time into a Unix timestamp; for example ``2016-05-27 `` or
274
+ ``2016-05-27T02:59:43.1Z `` (ISO-8601 _)::
275
+
276
+ Yaml::parse('2016-05-27'); // 1464307200
277
+
278
+ You can make it convert to a ``DateTime `` instance by using the ``PARSE_DATETIME ``
279
+ flag::
280
+
281
+ $date = Yaml::parse('2016-05-27', Yaml::PARSE_DATETIME);
282
+ var_dump(get_class($date)); // DateTime
283
+
284
+ Dumping Multi-line Literal Blocks
285
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
286
+
287
+ In YAML multiple lines can be represented as literal blocks, by default the
288
+ dumper will encode multiple lines as an inline string::
289
+
290
+ $string = ["string" => "Multiple\nLine\nString"];
291
+ $yaml = Yaml::dump($string);
292
+ echo $yaml; // string: "Multiple\nLine\nString"
293
+
294
+ You can make it use a literal block with the ``DUMP_MULTI_LINE_LITERAL_BLOCK ``
295
+ flag::
296
+
297
+ $string = ["string" => "Multiple\nLine\nString"];
298
+ $yaml = Yaml::dump($string, 2, 4. Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
299
+ echo $yaml;
300
+ // string: |
301
+ // Multiple
302
+ // Line
303
+ // String
304
+
253
305
.. _YAML : http://yaml.org/
254
306
.. _Packagist : https://packagist.org/packages/symfony/yaml
255
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