From 3249cbbfe59ecff1f36e974ff34652e200419b64 Mon Sep 17 00:00:00 2001 From: dantleech Date: Sat, 21 May 2016 11:27:56 +0100 Subject: [PATCH 1/3] Advanced YAML component usage --- components/yaml/introduction.rst | 67 +++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 9 deletions(-) diff --git a/components/yaml/introduction.rst b/components/yaml/introduction.rst index adb0d0956e6..f2002ccc57a 100644 --- a/components/yaml/introduction.rst +++ b/components/yaml/introduction.rst @@ -169,12 +169,6 @@ array to its YAML representation: file_put_contents('/path/to/file.yml', $yaml); -.. note:: - - Of course, the Symfony Yaml dumper is not able to dump resources. Also, - even if the dumper is able to dump PHP objects, it is considered to be a - not supported feature. - If an error occurs during the dump, the parser throws a :class:`Symfony\\Component\\Yaml\\Exception\\DumpException` exception. @@ -185,7 +179,10 @@ If you only need to dump one array, you can use the use Symfony\Component\Yaml\Yaml; - $yaml = Yaml::dump($array, $inline); + $yaml = Yaml::dump($array); + +Array Expansion and Inlining +............................ The YAML format supports two kind of representation for arrays, the expanded one, and the inline one. By default, the dumper uses the inline @@ -201,7 +198,7 @@ representation to the inline one: .. code-block:: php - echo $dumper->dump($array, 1); + echo Yaml::dump($array, 1); .. code-block:: yaml @@ -210,7 +207,7 @@ representation to the inline one: .. code-block:: php - echo $dumper->dump($array, 2); + echo Yaml::dump($array, 2); .. code-block:: yaml @@ -219,6 +216,58 @@ representation to the inline one: foo: bar bar: baz +Indentation +........... + +By default the YAML component will use 4 spaces for indentation. This can be +changed using the third argument as follows:: + + // use 8 spaces for indentation + echo Yaml::dump($array, 2, 8); + +.. code-block:: yaml + + foo: bar + bar: + foo: bar + bar: baz + +Invalid Types and Object Serialization +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +By default the YAML component will encode any "unsupported" type (i.e. +resources and objects) as ``null``. + +Instead of encoding as ``null`` you can choose to throw an exception if an invalid +type is encountered in either the dumper or parser as follows:: + + // throw an exception if a resource or object is encoutered + Yaml::dump($data, 2, 4, true); + + // throw an exception if an encoded object is found in the YAML string + Yaml::parse($yaml, true); + +However, you can activate object support using the next argument:: + + $object = new \stdClass(); + $object->foo = 'bar'; + + $dumped = Yaml::dump($object, 2, 4, false, true); + // !!php/object:O:8:"stdClass":1:{s:5:"foo";s:7:"bar";} + + $parsed = Yaml::parse($dumped, false, true); + var_dump(is_object($parsed)); // true + echo $parsed->foo; // bar + +The YAML component uses PHP's ``serialize`` method to generate a string +representation of the object. + +.. warning:: + + Object seialization is specific to this implementation, other PHP YAML + parsers will likely not recognize the ``php/object`` tag and non-PHP + implementations certainly won't - use with discretion! + .. _YAML: http://yaml.org/ .. _Packagist: https://packagist.org/packages/symfony/yaml .. _`YAML 1.2 version specification`: http://yaml.org/spec/1.2/spec.html From 33d6855d0871366f43f81be01d891b0c0d048627 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Sat, 21 May 2016 17:09:15 +0200 Subject: [PATCH 2/3] Remove 2.x versionadded directives --- components/phpunit_bridge.rst | 3 --- reference/forms/types/entity.rst | 3 --- reference/twig_reference.rst | 6 ------ 3 files changed, 12 deletions(-) diff --git a/components/phpunit_bridge.rst b/components/phpunit_bridge.rst index a3f8e2a0905..db288f52bd4 100644 --- a/components/phpunit_bridge.rst +++ b/components/phpunit_bridge.rst @@ -121,9 +121,6 @@ compatibility reasons. Time-sensitive Tests --------------------- -.. versionadded:: 2.8 - Support for clock mocking was introduced in Symfony 2.8. - Use Case ~~~~~~~~ diff --git a/reference/forms/types/entity.rst b/reference/forms/types/entity.rst index 65f5a548bb2..10d4986937a 100644 --- a/reference/forms/types/entity.rst +++ b/reference/forms/types/entity.rst @@ -205,9 +205,6 @@ return a ``QueryBuilder``. If you'd like to display an empty list of entries, you can return ``null`` in the Closure. -.. versionadded:: 2.8 - Returning ``null`` in the Closure was introduced in Symfony 2.8. - Overridden Options ------------------ diff --git a/reference/twig_reference.rst b/reference/twig_reference.rst index 52187049f1f..33c6f532d73 100644 --- a/reference/twig_reference.rst +++ b/reference/twig_reference.rst @@ -345,9 +345,6 @@ information in :ref:`book-templating-pages`. absolute_url ~~~~~~~~~~~~ -.. versionadded:: 2.7 - The ``absolute_url()`` function was introduced in Symfony 2.7. - .. code-block:: jinja {{ absolute_url(path) }} @@ -370,9 +367,6 @@ you're on the following page in your app: relative_path ~~~~~~~~~~~~~ -.. versionadded:: 2.7 - The ``relative_path()`` function was introduced in Symfony 2.7. - .. code-block:: jinja {{ relative_path(path) }} From 5bdb6d339581824e1d591dd4b816eb66e33ec63c Mon Sep 17 00:00:00 2001 From: WouterJ Date: Sat, 21 May 2016 17:25:19 +0200 Subject: [PATCH 3/3] [#6582] Some syntax improvements --- components/yaml/introduction.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/components/yaml/introduction.rst b/components/yaml/introduction.rst index f2002ccc57a..c61b40fa0e4 100644 --- a/components/yaml/introduction.rst +++ b/components/yaml/introduction.rst @@ -241,7 +241,7 @@ resources and objects) as ``null``. Instead of encoding as ``null`` you can choose to throw an exception if an invalid type is encountered in either the dumper or parser as follows:: - // throw an exception if a resource or object is encoutered + // throw an exception if a resource or object is encountered Yaml::dump($data, 2, 4, true); // throw an exception if an encoded object is found in the YAML string @@ -259,12 +259,12 @@ However, you can activate object support using the next argument:: var_dump(is_object($parsed)); // true echo $parsed->foo; // bar -The YAML component uses PHP's ``serialize`` method to generate a string +The YAML component uses PHP's ``serialize()`` method to generate a string representation of the object. -.. warning:: +.. caution:: - Object seialization is specific to this implementation, other PHP YAML + Object serialization is specific to this implementation, other PHP YAML parsers will likely not recognize the ``php/object`` tag and non-PHP implementations certainly won't - use with discretion!