From 08bec021ad9ffa7009c865d6fe96ccdf50ea1f45 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 6 Feb 2018 13:37:54 +0100 Subject: [PATCH 1/3] Documented how to parse and dump custom YAML tags --- components/yaml.rst | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/components/yaml.rst b/components/yaml.rst index 408ff7a615c..d53cec70602 100644 --- a/components/yaml.rst +++ b/components/yaml.rst @@ -377,6 +377,31 @@ Binary data is automatically parsed if they include the ``!!binary`` YAML tag $parsed = Yaml::parse($dumped); $imageContents = $parsed['logo']; +Parsing and Dumping Custom Tags +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. versionadded:: 3.3 + Support for parsing and dumping custom tags was introduced in Symfony 3.3. + +In addition to the built-in support of tags like ``!php/const`` and +``!!binary``, you can define your own custom YAML tags and parse them with the +``PARSE_CUSTOM_TAGS`` flag:: + + $data = "!my_tag { foo: bar }"; + $parsed = Yaml::parse($data, Yaml::PARSE_CUSTOM_TAGS); + // $parsed = Symfony\Component\Yaml\Tag\TaggedValue('my_tag', array('foo' => 'bar')); + $tagName = $parsed->getTag(); // $tagName = 'my_tag' + $tagValue = $parsed->getValue(); // $tagValue = array('foo' => 'bar') + +If the contents to dump contain :class:`Symfony\\Component\\Yaml\\Tag\\TaggedValue` +objects, they are automatically transformed into YAML tags:: + + use Symfony\Component\Yaml\Tag\TaggedValue; + + $data = new TaggedValue('my_tag', array('foo' => 'bar')); + $dumped = Yaml::parse($data); + // $dumped = '!my_tag { foo: bar }' + Syntax Validation ~~~~~~~~~~~~~~~~~ From 2758a3a7227be96c67d916b92ded6c547fa855a6 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 6 Feb 2018 13:38:53 +0100 Subject: [PATCH 2/3] Align code --- components/yaml.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/yaml.rst b/components/yaml.rst index d53cec70602..b6ac90179ed 100644 --- a/components/yaml.rst +++ b/components/yaml.rst @@ -390,7 +390,7 @@ In addition to the built-in support of tags like ``!php/const`` and $data = "!my_tag { foo: bar }"; $parsed = Yaml::parse($data, Yaml::PARSE_CUSTOM_TAGS); // $parsed = Symfony\Component\Yaml\Tag\TaggedValue('my_tag', array('foo' => 'bar')); - $tagName = $parsed->getTag(); // $tagName = 'my_tag' + $tagName = $parsed->getTag(); // $tagName = 'my_tag' $tagValue = $parsed->getValue(); // $tagValue = array('foo' => 'bar') If the contents to dump contain :class:`Symfony\\Component\\Yaml\\Tag\\TaggedValue` From f6a06b3a6e04c2546f60faca7dbef63640b5cb83 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 7 Feb 2018 09:27:12 +0100 Subject: [PATCH 3/3] Fixed code --- components/yaml.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/yaml.rst b/components/yaml.rst index b6ac90179ed..1826abd0625 100644 --- a/components/yaml.rst +++ b/components/yaml.rst @@ -399,7 +399,7 @@ objects, they are automatically transformed into YAML tags:: use Symfony\Component\Yaml\Tag\TaggedValue; $data = new TaggedValue('my_tag', array('foo' => 'bar')); - $dumped = Yaml::parse($data); + $dumped = Yaml::dump($data); // $dumped = '!my_tag { foo: bar }' Syntax Validation