@@ -169,12 +169,6 @@ array to its YAML representation:
169
169
170
170
file_put_contents('/path/to/file.yml', $yaml);
171
171
172
- .. note ::
173
-
174
- Of course, the Symfony Yaml dumper is not able to dump resources. Also,
175
- even if the dumper is able to dump PHP objects, it is considered to be a
176
- not supported feature.
177
-
178
172
If an error occurs during the dump, the parser throws a
179
173
:class: `Symfony\\ Component\\ Yaml\\ Exception\\ DumpException ` exception.
180
174
@@ -185,7 +179,10 @@ If you only need to dump one array, you can use the
185
179
186
180
use Symfony\Component\Yaml\Yaml;
187
181
188
- $yaml = Yaml::dump($array, $inline);
182
+ $yaml = Yaml::dump($array);
183
+
184
+ Array Expansion and Inlining
185
+ ............................
189
186
190
187
The YAML format supports two kind of representation for arrays, the expanded
191
188
one, and the inline one. By default, the dumper uses the inline
@@ -201,7 +198,7 @@ representation to the inline one:
201
198
202
199
.. code-block :: php
203
200
204
- echo $dumper-> dump($array, 1);
201
+ echo Yaml:: dump($array, 1);
205
202
206
203
.. code-block :: yaml
207
204
@@ -210,7 +207,7 @@ representation to the inline one:
210
207
211
208
.. code-block :: php
212
209
213
- echo $dumper-> dump($array, 2);
210
+ echo Yaml:: dump($array, 2);
214
211
215
212
.. code-block :: yaml
216
213
@@ -219,6 +216,58 @@ representation to the inline one:
219
216
foo : bar
220
217
bar : baz
221
218
219
+ Indentation
220
+ ...........
221
+
222
+ By default the YAML component will use 4 spaces for indentation. This can be
223
+ changed using the third argument as follows::
224
+
225
+ // use 8 spaces for indentation
226
+ echo Yaml::dump($array, 2, 8);
227
+
228
+ .. code-block :: yaml
229
+
230
+ foo : bar
231
+ bar :
232
+ foo : bar
233
+ bar : baz
234
+
235
+ Invalid Types and Object Serialization
236
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
237
+
238
+ By default the YAML component will encode any "unsupported" type (i.e.
239
+ resources and objects) as ``null ``.
240
+
241
+ Instead of encoding as ``null `` you can choose to throw an exception if an invalid
242
+ type is encountered in either the dumper or parser as follows::
243
+
244
+ // throw an exception if a resource or object is encoutered
245
+ Yaml::dump($data, 2, 4, true);
246
+
247
+ // throw an exception if an encoded object is found in the YAML string
248
+ Yaml::parse($yaml, true);
249
+
250
+ However, you can activate object support using the next argument::
251
+
252
+ $object = new \stdClass();
253
+ $object->foo = 'bar';
254
+
255
+ $dumped = Yaml::dump($object, 2, 4, false, true);
256
+ // !!php/object:O:8:"stdClass":1:{s:5:"foo";s:7:"bar";}
257
+
258
+ $parsed = Yaml::parse($dumped, false, true);
259
+ var_dump(is_object($parsed)); // true
260
+ echo $parsed->foo; // bar
261
+
262
+ The YAML component uses PHP's ``serialize `` method to generate a string
263
+ representation of the object.
264
+
265
+ .. warning ::
266
+
267
+ Object seialization is specific to this implementation, other PHP YAML
268
+ parsers will likely not recognize the ``php/object `` tag and non-PHP
269
+ implementations certainly won't - use with discretion!
270
+
222
271
.. _YAML : http://yaml.org/
223
272
.. _Packagist : https://packagist.org/packages/symfony/yaml
224
273
.. _`YAML 1.2 version specification` : http://yaml.org/spec/1.2/spec.html
0 commit comments