Skip to content

Commit e40ca91

Browse files
committed
Merge branch '5.4' into 6.0
* 5.4: [Tests] Remove some unused fixtures added missing translations for portuguese [#43726] [HttpClient][Mime] Add correct IDN flags for IDNA2008 compliance Prevent installation of incompatible mailer component versions properly parse quoted strings tagged with !!str do not merge label classes into expanded choice labels Bump Symfony version to 5.4.0 Update VERSION for 5.4.0-BETA3 Update CHANGELOG for 5.4.0-BETA3 [Serializer] PropertyNormalizer - return unique (i.e. filter duplicate ) attributes in extractAttributes function Allow autodetecting mapping type for any object
2 parents 640e162 + 0baa75b commit e40ca91

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

Inline.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,11 @@ private static function dumpNull(int $flags): string
247247
*
248248
* @throws ParseException When malformed inline YAML string is parsed
249249
*/
250-
public static function parseScalar(string $scalar, int $flags = 0, array $delimiters = null, int &$i = 0, bool $evaluate = true, array &$references = []): mixed
250+
public static function parseScalar(string $scalar, int $flags = 0, array $delimiters = null, int &$i = 0, bool $evaluate = true, array &$references = [], bool &$isQuoted = null): mixed
251251
{
252252
if (\in_array($scalar[$i], ['"', "'"], true)) {
253253
// quoted scalar
254+
$isQuoted = true;
254255
$output = self::parseQuotedScalar($scalar, $i);
255256

256257
if (null !== $delimiters) {
@@ -264,6 +265,8 @@ public static function parseScalar(string $scalar, int $flags = 0, array $delimi
264265
}
265266
} else {
266267
// "normal" string
268+
$isQuoted = false;
269+
267270
if (!$delimiters) {
268271
$output = substr($scalar, $i);
269272
$i += \strlen($output);
@@ -286,7 +289,7 @@ public static function parseScalar(string $scalar, int $flags = 0, array $delimi
286289
}
287290

288291
if ($evaluate) {
289-
$output = self::evaluateScalar($output, $flags, $references);
292+
$output = self::evaluateScalar($output, $flags, $references, $isQuoted);
290293
}
291294
}
292295

@@ -298,7 +301,7 @@ public static function parseScalar(string $scalar, int $flags = 0, array $delimi
298301
*
299302
* @throws ParseException When malformed inline YAML string is parsed
300303
*/
301-
private static function parseQuotedScalar(string $scalar, int &$i): string
304+
private static function parseQuotedScalar(string $scalar, int &$i = 0): string
302305
{
303306
if (!Parser::preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
304307
throw new ParseException(sprintf('Malformed inline YAML string: "%s".', substr($scalar, $i)), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
@@ -351,8 +354,7 @@ private static function parseSequence(string $sequence, int $flags, int &$i = 0,
351354
$value = self::parseMapping($sequence, $flags, $i, $references);
352355
break;
353356
default:
354-
$isQuoted = \in_array($sequence[$i], ['"', "'"], true);
355-
$value = self::parseScalar($sequence, $flags, [',', ']'], $i, null === $tag, $references);
357+
$value = self::parseScalar($sequence, $flags, [',', ']'], $i, null === $tag, $references, $isQuoted);
356358

357359
// the value can be an array if a reference has been resolved to an array var
358360
if (\is_string($value) && !$isQuoted && false !== strpos($value, ': ')) {
@@ -497,8 +499,7 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a
497499
}
498500
break;
499501
default:
500-
$isValueQuoted = \in_array($mapping[$i], ['"', "'"]);
501-
$value = self::parseScalar($mapping, $flags, [',', '}', "\n"], $i, null === $tag, $references);
502+
$value = self::parseScalar($mapping, $flags, [',', '}', "\n"], $i, null === $tag, $references, $isValueQuoted);
502503
// Spec: Keys MUST be unique; first one wins.
503504
// Parser cannot abort this mapping earlier, since lines
504505
// are processed sequentially.
@@ -535,8 +536,9 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a
535536
*
536537
* @throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved
537538
*/
538-
private static function evaluateScalar(string $scalar, int $flags, array &$references = []): mixed
539+
private static function evaluateScalar(string $scalar, int $flags, array &$references = [], bool &$isQuotedString = null): mixed
539540
{
541+
$isQuotedString = false;
540542
$scalar = trim($scalar);
541543

542544
if (0 === strpos($scalar, '*')) {
@@ -572,7 +574,14 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
572574
case '!' === $scalar[0]:
573575
switch (true) {
574576
case 0 === strpos($scalar, '!!str '):
575-
return (string) substr($scalar, 6);
577+
$s = (string) substr($scalar, 6);
578+
579+
if (\in_array($s[0] ?? '', ['"', "'"], true)) {
580+
$isQuotedString = true;
581+
$s = self::parseQuotedScalar($s);
582+
}
583+
584+
return $s;
576585
case 0 === strpos($scalar, '! '):
577586
return substr($scalar, 2);
578587
case 0 === strpos($scalar, '!php/object'):

Tests/InlineTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -924,21 +924,31 @@ public function ideographicSpaceProvider(): array
924924
];
925925
}
926926

927+
public function testParseSingleQuotedTaggedString()
928+
{
929+
$this->assertSame('foo', Inline::parse("!!str 'foo'"));
930+
}
931+
932+
public function testParseDoubleQuotedTaggedString()
933+
{
934+
$this->assertSame('foo', Inline::parse('!!str "foo"'));
935+
}
936+
927937
public function testParseQuotedReferenceLikeStringsInMapping()
928938
{
929939
$yaml = <<<YAML
930-
{foo: '&foo', bar: "&bar"}
940+
{foo: '&foo', bar: "&bar", baz: !!str '&baz'}
931941
YAML;
932942

933-
$this->assertSame(['foo' => '&foo', 'bar' => '&bar'], Inline::parse($yaml));
943+
$this->assertSame(['foo' => '&foo', 'bar' => '&bar', 'baz' => '&baz'], Inline::parse($yaml));
934944
}
935945

936946
public function testParseQuotedReferenceLikeStringsInSequence()
937947
{
938948
$yaml = <<<YAML
939-
['&foo', "&bar" ]
949+
['&foo', "&bar", !!str '&baz']
940950
YAML;
941951

942-
$this->assertSame(['&foo', '&bar'], Inline::parse($yaml));
952+
$this->assertSame(['&foo', '&bar', '&baz'], Inline::parse($yaml));
943953
}
944954
}

0 commit comments

Comments
 (0)