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

Skip to content

Commit 9046796

Browse files
committed
[Yaml] Support tagged scalars
1 parent 15b7cdb commit 9046796

File tree

4 files changed

+42
-51
lines changed

4 files changed

+42
-51
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,6 @@ private static function evaluateScalar($scalar, $flags, $references = array())
609609
switch (true) {
610610
case 0 === strpos($scalar, '!str'):
611611
return (string) substr($scalar, 5);
612-
case 0 === strpos($scalar, '! '):
613-
return (int) self::parseScalar(substr($scalar, 2), $flags);
614612
case 0 === strpos($scalar, '!php/object:'):
615613
if (self::$objectSupport) {
616614
return unserialize(substr($scalar, 12));
@@ -620,18 +618,6 @@ private static function evaluateScalar($scalar, $flags, $references = array())
620618
throw new ParseException('Object support when parsing a YAML file has been disabled.');
621619
}
622620

623-
return;
624-
case 0 === strpos($scalar, '!!php/object:'):
625-
if (self::$objectSupport) {
626-
@trigger_error('The !!php/object tag to indicate dumped PHP objects is deprecated since version 3.1 and will be removed in 4.0. Use the !php/object tag instead.', E_USER_DEPRECATED);
627-
628-
return unserialize(substr($scalar, 13));
629-
}
630-
631-
if (self::$exceptionOnInvalidType) {
632-
throw new ParseException('Object support when parsing a YAML file has been disabled.');
633-
}
634-
635621
return;
636622
case 0 === strpos($scalar, '!php/const:'):
637623
if (self::$constantSupport) {
@@ -651,7 +637,7 @@ private static function evaluateScalar($scalar, $flags, $references = array())
651637
case 0 === strpos($scalar, '!!binary '):
652638
return self::evaluateBinaryScalar(substr($scalar, 9));
653639
default:
654-
@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);
640+
throw new ParseException(sprintf('The string "%s" could not be parsed as it uses an unsupported built-in tag.', $scalar));
655641
}
656642

657643
// Optimize for returning strings.
@@ -724,20 +710,23 @@ private static function parseTag($value, &$i, $flags)
724710
$nextOffset = $i + $tagLength + 1;
725711
$nextOffset += strspn($value, ' ', $nextOffset);
726712

727-
// Is followed by a scalar
728-
if (!isset($value[$nextOffset]) || !in_array($value[$nextOffset], array('[', '{'), true)) {
729-
// Manage scalars in {@link self::evaluateScalar()}
713+
// Is followed by a scalar and is a built-in tag
714+
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:'))) {
715+
// Manage in {@link self::evaluateScalar()}
730716
return;
731717
}
732718

719+
$i = $nextOffset;
720+
733721
// Built-in tags
722+
if ('' === $tag) {
723+
return;
724+
}
734725
if ($tag && '!' === $tag[0]) {
735726
throw new ParseException(sprintf('The built-in tag "!%s" is not implemented.', $tag));
736727
}
737728

738729
if (Yaml::PARSE_CUSTOM_TAGS & $flags) {
739-
$i = $nextOffset;
740-
741730
return $tag;
742731
}
743732

src/Symfony/Component/Yaml/Parser.php

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

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

650-
if ('' !== $matches['tag']) {
650+
if ('' !== $matches['tag'] && '!' !== $matches['tag']) {
651651
if ('!!binary' === $matches['tag']) {
652652
return Inline::evaluateBinaryScalar($data);
653-
} elseif ('!' !== $matches['tag']) {
654-
@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);
655653
}
654+
655+
return new TaggedValue(substr($matches['tag'], 1), $data);
656656
}
657657

658658
return $data;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -930,10 +930,10 @@ documents: 2
930930
test: Explicit typing
931931
yaml: |
932932
integer: 12
933-
also int: ! "12"
933+
but string: ! "12"
934934
string: !str 12
935935
php: |
936-
array( 'integer' => 12, 'also int' => 12, 'string' => '12' )
936+
array( 'integer' => 12, 'but string' => '12', 'string' => '12' )
937937
---
938938
test: Private types
939939
todo: true

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

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -487,18 +487,6 @@ public function testObjectSupportEnabledPassingTrue()
487487
$this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, false, true), '->parse() is able to parse objects');
488488
}
489489

490-
/**
491-
* @group legacy
492-
*/
493-
public function testObjectSupportEnabledWithDeprecatedTag()
494-
{
495-
$input = <<<'EOF'
496-
foo: !!php/object:O:30:"Symfony\Component\Yaml\Tests\B":1:{s:1:"b";s:3:"foo";}
497-
bar: 1
498-
EOF;
499-
$this->assertEquals(array('foo' => new B(), 'bar' => 1), $this->parser->parse($input, Yaml::PARSE_OBJECT), '->parse() is able to parse objects');
500-
}
501-
502490
/**
503491
* @dataProvider invalidDumpedObjectProvider
504492
*/
@@ -625,19 +613,12 @@ public function testObjectsSupportDisabledWithExceptionsUsingBooleanToggles($yam
625613

626614
public function invalidDumpedObjectProvider()
627615
{
628-
$yamlTag = <<<'EOF'
629-
foo: !!php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
630-
bar: 1
631-
EOF;
632-
$localTag = <<<'EOF'
616+
return array(array(
617+
<<<'EOF'
633618
foo: !php/object:O:30:"Symfony\Tests\Component\Yaml\B":1:{s:1:"b";s:3:"foo";}
634619
bar: 1
635-
EOF;
636-
637-
return array(
638-
'yaml-tag' => array($yamlTag),
639-
'local-tag' => array($localTag),
640-
);
620+
EOF
621+
));
641622
}
642623

643624
/**
@@ -1598,6 +1579,18 @@ public function testCustomTagSupport($expected, $yaml)
15981579
public function taggedValuesProvider()
15991580
{
16001581
return array(
1582+
'scalars' => array(
1583+
array(
1584+
'foo' => new TaggedValue('inline', 'bar'),
1585+
'quz' => new TaggedValue('long', 'this is a long text'),
1586+
),
1587+
<<<YAML
1588+
foo: !inline bar
1589+
quz: !long >
1590+
this is a long
1591+
text
1592+
YAML
1593+
),
16011594
'sequences' => array(
16021595
array(new TaggedValue('foo', array('yaml')), new TaggedValue('quz', array('bar'))),
16031596
<<<YAML
@@ -1635,12 +1628,21 @@ public function testCustomTagsDisabled()
16351628
}
16361629

16371630
/**
1638-
* @group legacy
1639-
* @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.
1631+
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
1632+
* @expectedExceptionMessage Tags support is not enabled. Enable the `Yaml::PARSE_CUSTOM_TAGS` flag to use "!iterator" at line 1 (near "!iterator foo").
16401633
*/
16411634
public function testUnsupportedTagWithScalar()
16421635
{
1643-
$this->assertEquals('!iterator foo', $this->parser->parse('!iterator foo'));
1636+
$this->parser->parse('!iterator foo');
1637+
}
1638+
1639+
/**
1640+
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
1641+
* @expectedExceptionMessage The string "!!iterator foo" could not be parsed as it uses an unsupported built-in tag at line 1 (near "!!iterator foo").
1642+
*/
1643+
public function testUnsupportedBuiltInTagWithScalar()
1644+
{
1645+
$this->parser->parse('!!iterator foo');
16441646
}
16451647

16461648
/**

0 commit comments

Comments
 (0)