@@ -126,20 +126,7 @@ error occurred:
126
126
}
127
127
128
128
.. _components-yaml-dump :
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
129
+ .. _objects-for-mappings :
143
130
144
131
Writing YAML Files
145
132
~~~~~~~~~~~~~~~~~~
@@ -214,30 +201,27 @@ changed using the third argument as follows::
214
201
foo : bar
215
202
bar : baz
216
203
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::
204
+ Advanced Usage: Flags
205
+ ---------------------
225
206
226
- // throw an exception if a resource or object is encountered
227
- Yaml::dump($data, 2, 4, true);
207
+ .. versionadded :: 3.1
208
+ Flags were introduced in Symfony 3.1 and replaced the earlier boolean
209
+ arguments.
228
210
229
- // throw an exception if an encoded object is found in the YAML string
230
- Yaml::parse($yaml, true);
211
+ Object Parsing and Dumping
212
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
231
213
232
- However, you can activate object support using the next argument ::
214
+ You can dump objects by using the `` DUMP_OBJECT `` flag ::
233
215
234
216
$object = new \stdClass();
235
217
$object->foo = 'bar';
236
218
237
- $dumped = Yaml::dump($object, 2, 4, false, true );
238
- // !! php/object:O:8:"stdClass":1:{s:5:"foo";s:7:"bar";}
219
+ $dumped = Yaml::dump($object, 2, 4, Yaml::DUMP_OBJECT );
220
+ // !php/object:O:8:"stdClass":1:{s:5:"foo";s:7:"bar";}
239
221
240
- $parsed = Yaml::parse($dumped, false, true);
222
+ And parse them by using the ``PARSE_OBJECT `` flag::
223
+
224
+ $parsed = Yaml::parse($dumped, Yaml::PARSE_OBJECT);
241
225
var_dump(is_object($parsed)); // true
242
226
echo $parsed->foo; // bar
243
227
@@ -250,6 +234,76 @@ representation of the object.
250
234
parsers will likely not recognize the ``php/object `` tag and non-PHP
251
235
implementations certainly won't - use with discretion!
252
236
237
+ .. _invalid-types-and-object-serialization :
238
+
239
+ Handling Invalid Types
240
+ ~~~~~~~~~~~~~~~~~~~~~~
241
+
242
+ By default the parser will encode invalid types as "null". You can make the
243
+ parser throw exceptions by using the ``PARSE_EXCEPTION_ON_INVALID_TYPE ``
244
+ flag::
245
+
246
+ $yaml = '!php/object:O:8:"stdClass":1:{s:5:"foo";s:7:"bar";}';
247
+ Yaml::parse($yaml, Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE); // throws an exception
248
+
249
+ Similarly you can use ``DUMP_EXCEPTION_ON_INVALID_TYPE `` when dumping::
250
+
251
+ $data = new \stdClass(); // by default objects are invalid.
252
+ Yaml::parse($data, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE); // throws an exception
253
+
254
+ Using Objects for Maps
255
+ ~~~~~~~~~~~~~~~~~~~~~~
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
+
253
306
.. _YAML : http://yaml.org/
254
307
.. _Packagist : https://packagist.org/packages/symfony/yaml
255
308
.. _`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