Skip to content

Commit 15885f2

Browse files
committed
Documented how to parse and dump custom YAML tags
1 parent ec0865f commit 15885f2

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

components/yaml.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,31 @@ syntax to parse them as proper PHP constants::
333333
$parameters = Yaml::parse($yaml, Yaml::PARSE_CONSTANT);
334334
// $parameters = array('foo' => 'PHP_INT_SIZE', 'bar' => 8);
335335

336+
Parsing and Dumping Custom Tags
337+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
338+
339+
.. versionadded:: 3.3
340+
Support for parsing and dumping custom tags was introduced in Symfony 3.3.
341+
342+
In addition to the built-in support of tags like ``!php/const`` and
343+
``!!binary``, you can define your own custom YAML tags and parse them with the
344+
``PARSE_CUSTOM_TAGS`` flag::
345+
346+
$data = "!my_tag { foo: bar }";
347+
$parsed = Yaml::parse($data, Yaml::PARSE_CUSTOM_TAGS);
348+
// $parsed = Symfony\Component\Yaml\Tag\TaggedValue('my_tag', array('foo' => 'bar'));
349+
$tagName = $parsed->getTag(); // $tagName = 'my_tag'
350+
$tagValue = $parsed->getValue(); // $tagValue = array('foo' => 'bar')
351+
352+
If the contents to dump contain :class:`Symfony\\Component\\Yaml\\Tag\\TaggedValue`
353+
objects, they are automatically transformed into YAML tags::
354+
355+
use Symfony\Component\Yaml\Tag\TaggedValue;
356+
357+
$data = new TaggedValue('my_tag', array('foo' => 'bar'));
358+
$dumped = Yaml::parse($data);
359+
// $dumped = '!my_tag { foo: bar }'
360+
336361
Syntax Validation
337362
~~~~~~~~~~~~~~~~~
338363

0 commit comments

Comments
 (0)