Skip to content

Commit a1f8c3a

Browse files
committed
Merge branch '2.8' into 3.0
2 parents 33d6855 + c196b09 commit a1f8c3a

File tree

1 file changed

+58
-9
lines changed

1 file changed

+58
-9
lines changed

components/yaml/introduction.rst

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,6 @@ array to its YAML representation:
162162
163163
file_put_contents('/path/to/file.yml', $yaml);
164164
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-
171165
If an error occurs during the dump, the parser throws a
172166
:class:`Symfony\\Component\\Yaml\\Exception\\DumpException` exception.
173167

@@ -178,7 +172,10 @@ If you only need to dump one array, you can use the
178172
179173
use Symfony\Component\Yaml\Yaml;
180174
181-
$yaml = Yaml::dump($array, $inline);
175+
$yaml = Yaml::dump($array);
176+
177+
Array Expansion and Inlining
178+
............................
182179

183180
The YAML format supports two kind of representation for arrays, the expanded
184181
one, and the inline one. By default, the dumper uses the inline
@@ -194,7 +191,7 @@ representation to the inline one:
194191

195192
.. code-block:: php
196193
197-
echo $dumper->dump($array, 1);
194+
echo Yaml::dump($array, 1);
198195
199196
.. code-block:: yaml
200197
@@ -203,7 +200,7 @@ representation to the inline one:
203200
204201
.. code-block:: php
205202
206-
echo $dumper->dump($array, 2);
203+
echo Yaml::dump($array, 2);
207204
208205
.. code-block:: yaml
209206
@@ -212,6 +209,58 @@ representation to the inline one:
212209
foo: bar
213210
bar: baz
214211
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+
215264
.. _YAML: http://yaml.org/
216265
.. _Packagist: https://packagist.org/packages/symfony/yaml
217266
.. _`YAML 1.2 version specification`: http://yaml.org/spec/1.2/spec.html

0 commit comments

Comments
 (0)