Skip to content

Commit 226638a

Browse files
committed
Merge branch '4.4' into 5.3
* 4.4: added missing translations for portuguese [#43726] [HttpClient][Mime] Add correct IDN flags for IDNA2008 compliance properly parse quoted strings tagged with !!str do not merge label classes into expanded choice labels [Serializer] PropertyNormalizer - return unique (i.e. filter duplicate ) attributes in extractAttributes function
2 parents cc94e33 + 2c309e2 commit 226638a

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
@@ -269,10 +269,11 @@ private static function dumpNull(int $flags): string
269269
*
270270
* @throws ParseException When malformed inline YAML string is parsed
271271
*/
272-
public static function parseScalar(string $scalar, int $flags = 0, array $delimiters = null, int &$i = 0, bool $evaluate = true, array &$references = [])
272+
public static function parseScalar(string $scalar, int $flags = 0, array $delimiters = null, int &$i = 0, bool $evaluate = true, array &$references = [], bool &$isQuoted = null)
273273
{
274274
if (\in_array($scalar[$i], ['"', "'"], true)) {
275275
// quoted scalar
276+
$isQuoted = true;
276277
$output = self::parseQuotedScalar($scalar, $i);
277278

278279
if (null !== $delimiters) {
@@ -286,6 +287,8 @@ public static function parseScalar(string $scalar, int $flags = 0, array $delimi
286287
}
287288
} else {
288289
// "normal" string
290+
$isQuoted = false;
291+
289292
if (!$delimiters) {
290293
$output = substr($scalar, $i);
291294
$i += \strlen($output);
@@ -308,7 +311,7 @@ public static function parseScalar(string $scalar, int $flags = 0, array $delimi
308311
}
309312

310313
if ($evaluate) {
311-
$output = self::evaluateScalar($output, $flags, $references);
314+
$output = self::evaluateScalar($output, $flags, $references, $isQuoted);
312315
}
313316
}
314317

@@ -320,7 +323,7 @@ public static function parseScalar(string $scalar, int $flags = 0, array $delimi
320323
*
321324
* @throws ParseException When malformed inline YAML string is parsed
322325
*/
323-
private static function parseQuotedScalar(string $scalar, int &$i): string
326+
private static function parseQuotedScalar(string $scalar, int &$i = 0): string
324327
{
325328
if (!Parser::preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
326329
throw new ParseException(sprintf('Malformed inline YAML string: "%s".', substr($scalar, $i)), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
@@ -373,8 +376,7 @@ private static function parseSequence(string $sequence, int $flags, int &$i = 0,
373376
$value = self::parseMapping($sequence, $flags, $i, $references);
374377
break;
375378
default:
376-
$isQuoted = \in_array($sequence[$i], ['"', "'"], true);
377-
$value = self::parseScalar($sequence, $flags, [',', ']'], $i, null === $tag, $references);
379+
$value = self::parseScalar($sequence, $flags, [',', ']'], $i, null === $tag, $references, $isQuoted);
378380

379381
// the value can be an array if a reference has been resolved to an array var
380382
if (\is_string($value) && !$isQuoted && false !== strpos($value, ': ')) {
@@ -521,8 +523,7 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a
521523
}
522524
break;
523525
default:
524-
$isValueQuoted = \in_array($mapping[$i], ['"', "'"]);
525-
$value = self::parseScalar($mapping, $flags, [',', '}', "\n"], $i, null === $tag, $references);
526+
$value = self::parseScalar($mapping, $flags, [',', '}', "\n"], $i, null === $tag, $references, $isValueQuoted);
526527
// Spec: Keys MUST be unique; first one wins.
527528
// Parser cannot abort this mapping earlier, since lines
528529
// are processed sequentially.
@@ -561,8 +562,9 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a
561562
*
562563
* @throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved
563564
*/
564-
private static function evaluateScalar(string $scalar, int $flags, array &$references = [])
565+
private static function evaluateScalar(string $scalar, int $flags, array &$references = [], bool &$isQuotedString = null)
565566
{
567+
$isQuotedString = false;
566568
$scalar = trim($scalar);
567569

568570
if (0 === strpos($scalar, '*')) {
@@ -598,7 +600,14 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
598600
case '!' === $scalar[0]:
599601
switch (true) {
600602
case 0 === strpos($scalar, '!!str '):
601-
return (string) substr($scalar, 6);
603+
$s = (string) substr($scalar, 6);
604+
605+
if (\in_array($s[0] ?? '', ['"', "'"], true)) {
606+
$isQuotedString = true;
607+
$s = self::parseQuotedScalar($s);
608+
}
609+
610+
return $s;
602611
case 0 === strpos($scalar, '! '):
603612
return substr($scalar, 2);
604613
case 0 === strpos($scalar, '!php/object'):

Tests/InlineTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -944,21 +944,31 @@ public function ideographicSpaceProvider(): array
944944
];
945945
}
946946

947+
public function testParseSingleQuotedTaggedString()
948+
{
949+
$this->assertSame('foo', Inline::parse("!!str 'foo'"));
950+
}
951+
952+
public function testParseDoubleQuotedTaggedString()
953+
{
954+
$this->assertSame('foo', Inline::parse('!!str "foo"'));
955+
}
956+
947957
public function testParseQuotedReferenceLikeStringsInMapping()
948958
{
949959
$yaml = <<<YAML
950-
{foo: '&foo', bar: "&bar"}
960+
{foo: '&foo', bar: "&bar", baz: !!str '&baz'}
951961
YAML;
952962

953-
$this->assertSame(['foo' => '&foo', 'bar' => '&bar'], Inline::parse($yaml));
963+
$this->assertSame(['foo' => '&foo', 'bar' => '&bar', 'baz' => '&baz'], Inline::parse($yaml));
954964
}
955965

956966
public function testParseQuotedReferenceLikeStringsInSequence()
957967
{
958968
$yaml = <<<YAML
959-
['&foo', "&bar" ]
969+
['&foo', "&bar", !!str '&baz']
960970
YAML;
961971

962-
$this->assertSame(['&foo', '&bar'], Inline::parse($yaml));
972+
$this->assertSame(['&foo', '&bar', '&baz'], Inline::parse($yaml));
963973
}
964974
}

0 commit comments

Comments
 (0)