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

Skip to content

Commit cc56340

Browse files
committed
[Yaml] parse inlined tags without values
1 parent 545df8f commit cc56340

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@ private static function parseSequence($sequence, $flags, &$i = 0, $references =
351351
continue;
352352
}
353353

354-
$tag = self::parseTag($sequence, $i, $flags);
354+
$isQuoted = false;
355+
$tag = self::parseTag($sequence, $i, $flags, ']');
355356
switch ($sequence[$i]) {
356357
case '[':
357358
// nested sequence
@@ -380,6 +381,10 @@ private static function parseSequence($sequence, $flags, &$i = 0, $references =
380381
}
381382

382383
if (null !== $tag && '' !== $tag) {
384+
if ('' === $value && !$isQuoted) {
385+
$value = null;
386+
}
387+
383388
$value = new TaggedValue($tag, $value);
384389
}
385390

@@ -456,7 +461,8 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar
456461
continue;
457462
}
458463

459-
$tag = self::parseTag($mapping, $i, $flags);
464+
$isValueQuoted = false;
465+
$tag = self::parseTag($mapping, $i, $flags, '}');
460466
switch ($mapping[$i]) {
461467
case '[':
462468
// nested sequence
@@ -479,6 +485,7 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar
479485
}
480486
break;
481487
default:
488+
$isValueQuoted = in_array($mapping[$i], array('"', "'"));
482489
$value = self::parseScalar($mapping, $flags, array(',', '}'), $i, null === $tag, $references);
483490
// Spec: Keys MUST be unique; first one wins.
484491
// Parser cannot abort this mapping earlier, since lines
@@ -490,6 +497,10 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar
490497
}
491498

492499
if (null !== $tag && '' !== $tag) {
500+
if ('' === $value && !$isValueQuoted) {
501+
$value = null;
502+
}
503+
493504
$output[$key] = new TaggedValue($tag, $value);
494505
} else {
495506
$output[$key] = $value;
@@ -635,16 +646,17 @@ private static function evaluateScalar($scalar, $flags, $references = array())
635646
* @param string $value
636647
* @param int &$i
637648
* @param int $flags
649+
* @param string $stopOn
638650
*
639651
* @return null|string
640652
*/
641-
private static function parseTag($value, &$i, $flags)
653+
private static function parseTag($value, &$i, $flags, $stopOn = '')
642654
{
643655
if ('!' !== $value[$i]) {
644656
return;
645657
}
646658

647-
$tagLength = strcspn($value, " \t\n", $i + 1);
659+
$tagLength = strcspn($value, " \t\n".$stopOn, $i + 1);
648660
$tag = substr($value, $i + 1, $tagLength);
649661

650662
$nextOffset = $i + $tagLength + 1;

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Yaml\Exception\ParseException;
1616
use Symfony\Component\Yaml\Inline;
17+
use Symfony\Component\Yaml\Tag\TaggedValue;
1718
use Symfony\Component\Yaml\Yaml;
1819

1920
class InlineTest extends TestCase
@@ -703,4 +704,40 @@ public function getNotPhpCompatibleMappingKeyData()
703704
'float' => array('{0.25: "foo"}', array('0.25' => 'foo')),
704705
);
705706
}
707+
708+
public function testTagWithoutValueInSequence()
709+
{
710+
$value = Inline::parse('[!foo]', Yaml::PARSE_CUSTOM_TAGS);
711+
712+
$this->assertInstanceOf(TaggedValue::class, $value[0]);
713+
$this->assertSame('foo', $value[0]->getTag());
714+
$this->assertNull($value[0]->getValue());
715+
}
716+
717+
public function testTagWithEmptyValueInSequence()
718+
{
719+
$value = Inline::parse('[!foo ""]', Yaml::PARSE_CUSTOM_TAGS);
720+
721+
$this->assertInstanceOf(TaggedValue::class, $value[0]);
722+
$this->assertSame('foo', $value[0]->getTag());
723+
$this->assertSame('', $value[0]->getValue());
724+
}
725+
726+
public function testTagWithoutValueInMapping()
727+
{
728+
$value = Inline::parse('{foo: !bar}', Yaml::PARSE_CUSTOM_TAGS);
729+
730+
$this->assertInstanceOf(TaggedValue::class, $value['foo']);
731+
$this->assertSame('bar', $value['foo']->getTag());
732+
$this->assertNull($value['foo']->getValue());
733+
}
734+
735+
public function testTagWithEmptyValueInMapping()
736+
{
737+
$value = Inline::parse('{foo: !bar ""}', Yaml::PARSE_CUSTOM_TAGS);
738+
739+
$this->assertInstanceOf(TaggedValue::class, $value['foo']);
740+
$this->assertSame('bar', $value['foo']->getTag());
741+
$this->assertSame('', $value['foo']->getValue());
742+
}
706743
}

0 commit comments

Comments
 (0)