Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aeb8050

Browse files
committed
[Yaml] Support tagged scalars
1 parent 8e0d41a commit aeb8050

File tree

5 files changed

+44
-16
lines changed

5 files changed

+44
-16
lines changed

src/Symfony/Component/Yaml/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ CHANGELOG
2121
the parser and dumper is no longer supported, pass bitmask flags instead
2222
* the constructor arguments of the `Parser` class have been removed
2323
* the `Inline` class is internal and no longer part of the BC promise
24+
* added support of tagged scalars.
25+
26+
```yml
27+
Yaml::parse('!foo bar', Yaml::PARSE_CUSTOM_TAGS);
28+
// returns TaggedValue('foo', 'bar');
29+
```
2430

2531
3.3.0
2632
-----

src/Symfony/Component/Yaml/Inline.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,6 @@ private static function evaluateScalar($scalar, $flags, $references = array())
551551
switch (true) {
552552
case 0 === strpos($scalar, '!str'):
553553
return (string) substr($scalar, 5);
554-
case 0 === strpos($scalar, '! '):
555-
return (int) self::parseScalar(substr($scalar, 2), $flags);
556554
case 0 === strpos($scalar, '!php/object:'):
557555
if (self::$objectSupport) {
558556
return unserialize(substr($scalar, 12));
@@ -581,7 +579,7 @@ private static function evaluateScalar($scalar, $flags, $references = array())
581579
case 0 === strpos($scalar, '!!binary '):
582580
return self::evaluateBinaryScalar(substr($scalar, 9));
583581
default:
584-
@trigger_error(sprintf('Using the unquoted scalar value "%s" is deprecated since version 3.3 and will be considered as a tagged value in 4.0. You must quote it.', $scalar), E_USER_DEPRECATED);
582+
throw new ParseException(sprintf('The string "%s" could not be parsed as it uses an unsupported built-in tag.', $scalar));
585583
}
586584

587585
// Optimize for returning strings.
@@ -649,20 +647,23 @@ private static function parseTag($value, &$i, $flags)
649647
$nextOffset = $i + $tagLength + 1;
650648
$nextOffset += strspn($value, ' ', $nextOffset);
651649

652-
// Is followed by a scalar
653-
if (!isset($value[$nextOffset]) || !in_array($value[$nextOffset], array('[', '{'), true)) {
654-
// Manage scalars in {@link self::evaluateScalar()}
650+
// Is followed by a scalar and is a built-in tag
651+
if ($tag && (!isset($value[$nextOffset]) || !in_array($value[$nextOffset], array('[', '{'), true)) && ('!' === $tag[0] || 'str' === $tag || 0 === strpos($tag, 'php/const:') || 0 === strpos($tag, 'php/object:'))) {
652+
// Manage in {@link self::evaluateScalar()}
655653
return;
656654
}
657655

656+
$i = $nextOffset;
657+
658658
// Built-in tags
659+
if ('' === $tag) {
660+
return;
661+
}
659662
if ($tag && '!' === $tag[0]) {
660663
throw new ParseException(sprintf('The built-in tag "!%s" is not implemented.', $tag));
661664
}
662665

663666
if (Yaml::PARSE_CUSTOM_TAGS & $flags) {
664-
$i = $nextOffset;
665-
666667
return $tag;
667668
}
668669

src/Symfony/Component/Yaml/Parser.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,12 +602,12 @@ private function parseValue($value, $flags, $context)
602602

603603
$data = $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
604604

605-
if ('' !== $matches['tag']) {
605+
if ('' !== $matches['tag'] && '!' !== $matches['tag']) {
606606
if ('!!binary' === $matches['tag']) {
607607
return Inline::evaluateBinaryScalar($data);
608-
} elseif ('!' !== $matches['tag']) {
609-
@trigger_error(sprintf('Using the custom tag "%s" for the value "%s" is deprecated since version 3.3. It will be replaced by an instance of %s in 4.0.', $matches['tag'], $data, TaggedValue::class), E_USER_DEPRECATED);
610608
}
609+
610+
return new TaggedValue(substr($matches['tag'], 1), $data);
611611
}
612612

613613
return $data;

src/Symfony/Component/Yaml/Tests/Fixtures/YtsSpecificationExamples.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,10 +910,10 @@ documents: 2
910910
test: Explicit typing
911911
yaml: |
912912
integer: 12
913-
also int: ! "12"
913+
but string: ! "12"
914914
string: !str 12
915915
php: |
916-
array( 'integer' => 12, 'also int' => 12, 'string' => '12' )
916+
array( 'integer' => 12, 'but string' => '12', 'string' => '12' )
917917
---
918918
test: Private types
919919
todo: true

src/Symfony/Component/Yaml/Tests/ParserTest.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,18 @@ public function testCustomTagSupport($expected, $yaml)
15421542
public function taggedValuesProvider()
15431543
{
15441544
return array(
1545+
'scalars' => array(
1546+
array(
1547+
'foo' => new TaggedValue('inline', 'bar'),
1548+
'quz' => new TaggedValue('long', 'this is a long text'),
1549+
),
1550+
<<<YAML
1551+
foo: !inline bar
1552+
quz: !long >
1553+
this is a long
1554+
text
1555+
YAML
1556+
),
15451557
'sequences' => array(
15461558
array(new TaggedValue('foo', array('yaml')), new TaggedValue('quz', array('bar'))),
15471559
<<<YAML
@@ -1579,12 +1591,21 @@ public function testCustomTagsDisabled()
15791591
}
15801592

15811593
/**
1582-
* @group legacy
1583-
* @expectedDeprecation Using the unquoted scalar value "!iterator foo" is deprecated since version 3.3 and will be considered as a tagged value in 4.0. You must quote it.
1594+
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
1595+
* @expectedExceptionMessage Tags support is not enabled. Enable the `Yaml::PARSE_CUSTOM_TAGS` flag to use "!iterator" at line 1 (near "!iterator foo").
15841596
*/
15851597
public function testUnsupportedTagWithScalar()
15861598
{
1587-
$this->assertEquals('!iterator foo', $this->parser->parse('!iterator foo'));
1599+
$this->parser->parse('!iterator foo');
1600+
}
1601+
1602+
/**
1603+
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
1604+
* @expectedExceptionMessage The string "!!iterator foo" could not be parsed as it uses an unsupported built-in tag at line 1 (near "!!iterator foo").
1605+
*/
1606+
public function testUnsupportedBuiltInTagWithScalar()
1607+
{
1608+
$this->parser->parse('!!iterator foo');
15881609
}
15891610

15901611
/**

0 commit comments

Comments
 (0)