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