Skip to content

Commit 9c2a751

Browse files
committed
feature #6582 Advanced YAML component usage (dantleech)
This PR was merged into the 2.3 branch. Discussion ---------- Advanced YAML component usage | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes | Applies to | 2.3 > 3.0 (i think) | Fixed tickets | #6524 Prerequisite to #6226 This PR documents the (now deprecated) features for indentation, invalid types and object serialization which have been present since sf 2.3 Commits ------- 3249cbb Advanced YAML component usage
2 parents 31de26f + 3249cbb commit 9c2a751

File tree

1 file changed

+58
-9
lines changed

1 file changed

+58
-9
lines changed

Diff for: components/yaml/introduction.rst

+58-9
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,6 @@ array to its YAML representation:
169169
170170
file_put_contents('/path/to/file.yml', $yaml);
171171
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-
178172
If an error occurs during the dump, the parser throws a
179173
:class:`Symfony\\Component\\Yaml\\Exception\\DumpException` exception.
180174

@@ -185,7 +179,10 @@ If you only need to dump one array, you can use the
185179
186180
use Symfony\Component\Yaml\Yaml;
187181
188-
$yaml = Yaml::dump($array, $inline);
182+
$yaml = Yaml::dump($array);
183+
184+
Array Expansion and Inlining
185+
............................
189186

190187
The YAML format supports two kind of representation for arrays, the expanded
191188
one, and the inline one. By default, the dumper uses the inline
@@ -201,7 +198,7 @@ representation to the inline one:
201198

202199
.. code-block:: php
203200
204-
echo $dumper->dump($array, 1);
201+
echo Yaml::dump($array, 1);
205202
206203
.. code-block:: yaml
207204
@@ -210,7 +207,7 @@ representation to the inline one:
210207
211208
.. code-block:: php
212209
213-
echo $dumper->dump($array, 2);
210+
echo Yaml::dump($array, 2);
214211
215212
.. code-block:: yaml
216213
@@ -219,6 +216,58 @@ representation to the inline one:
219216
foo: bar
220217
bar: baz
221218
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+
222271
.. _YAML: http://yaml.org/
223272
.. _Packagist: https://packagist.org/packages/symfony/yaml
224273
.. _`YAML 1.2 version specification`: http://yaml.org/spec/1.2/spec.html

0 commit comments

Comments
 (0)