Skip to content

Commit f192d5b

Browse files
committed
Advanced YAML component usage
1 parent bbec4cc commit f192d5b

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

components/yaml/introduction.rst

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,10 @@ If you only need to dump one array, you can use the
185185
186186
use Symfony\Component\Yaml\Yaml;
187187
188-
$yaml = Yaml::dump($array, $inline);
188+
$yaml = Yaml::dump($array);
189+
190+
Array Expansion and Inlining
191+
............................
189192

190193
The YAML format supports two kind of representation for arrays, the expanded
191194
one, and the inline one. By default, the dumper uses the inline
@@ -201,7 +204,7 @@ representation to the inline one:
201204

202205
.. code-block:: php
203206
204-
echo $dumper->dump($array, 1);
207+
echo Yaml::dump($array, 1);
205208
206209
.. code-block:: yaml
207210
@@ -210,7 +213,7 @@ representation to the inline one:
210213
211214
.. code-block:: php
212215
213-
echo $dumper->dump($array, 2);
216+
echo Yaml::dump($array, 2);
214217
215218
.. code-block:: yaml
216219
@@ -219,6 +222,49 @@ representation to the inline one:
219222
foo: bar
220223
bar: baz
221224
225+
Indentation
226+
...........
227+
228+
By default the YAML component will use 4 spaces for indentation. This can be
229+
changed using the second argument as follows::
230+
231+
// use 8 spaces for indentation
232+
echo Yaml::dump($array, 2, 8);
233+
234+
.. code-block:: yaml
235+
236+
foo: bar
237+
bar:
238+
foo: bar
239+
bar: baz
240+
241+
Invalid Types and Object Serialization
242+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
243+
244+
By default the YAML component will encode any "unsupported" type (i.e.
245+
resources and objects) as ``null``.
246+
247+
Instead of encoding as ``null`` you can choose to throw an exception if an invalid
248+
type is encountered in either the dumper or parser as follows::
249+
250+
// throw an exception if a resource or object is encoutered
251+
Yaml::dump($data, 2, 4, true);
252+
253+
// throw an exception if an encoded object is found in the YAML string
254+
Yaml::parse($yaml, true);
255+
256+
However, you can activate object support using the next argument::
257+
258+
$object = new \stdClass();
259+
$object->hello = 'goodbye';
260+
261+
$dumped = Yaml::dump($object, 2, 4, false, true);
262+
// !!php/object:O:8:"stdClass":1:{s:5:"hello";s:7:"goodbye";}
263+
264+
$parsed = Yaml::parse($dumped, false, true);
265+
var_dump(is_object($parsed)); // true
266+
echo $parsed->hello; // goodbye
267+
222268
.. _YAML: http://yaml.org/
223269
.. _Packagist: https://packagist.org/packages/symfony/yaml
224270
.. _`YAML 1.2 version specification`: http://yaml.org/spec/1.2/spec.html

0 commit comments

Comments
 (0)