Skip to content

Commit 0baa75b

Browse files
committed
Merge branch '5.3' into 5.4
* 5.3: [Tests] Remove some unused fixtures 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 9e6346b + 226638a commit 0baa75b

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

280281
if (null !== $delimiters) {
@@ -288,6 +289,8 @@ public static function parseScalar(string $scalar, int $flags = 0, array $delimi
288289
}
289290
} else {
290291
// "normal" string
292+
$isQuoted = false;
293+
291294
if (!$delimiters) {
292295
$output = substr($scalar, $i);
293296
$i += \strlen($output);
@@ -310,7 +313,7 @@ public static function parseScalar(string $scalar, int $flags = 0, array $delimi
310313
}
311314

312315
if ($evaluate) {
313-
$output = self::evaluateScalar($output, $flags, $references);
316+
$output = self::evaluateScalar($output, $flags, $references, $isQuoted);
314317
}
315318
}
316319

@@ -322,7 +325,7 @@ public static function parseScalar(string $scalar, int $flags = 0, array $delimi
322325
*
323326
* @throws ParseException When malformed inline YAML string is parsed
324327
*/
325-
private static function parseQuotedScalar(string $scalar, int &$i): string
328+
private static function parseQuotedScalar(string $scalar, int &$i = 0): string
326329
{
327330
if (!Parser::preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
328331
throw new ParseException(sprintf('Malformed inline YAML string: "%s".', substr($scalar, $i)), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
@@ -375,8 +378,7 @@ private static function parseSequence(string $sequence, int $flags, int &$i = 0,
375378
$value = self::parseMapping($sequence, $flags, $i, $references);
376379
break;
377380
default:
378-
$isQuoted = \in_array($sequence[$i], ['"', "'"], true);
379-
$value = self::parseScalar($sequence, $flags, [',', ']'], $i, null === $tag, $references);
381+
$value = self::parseScalar($sequence, $flags, [',', ']'], $i, null === $tag, $references, $isQuoted);
380382

381383
// the value can be an array if a reference has been resolved to an array var
382384
if (\is_string($value) && !$isQuoted && false !== strpos($value, ': ')) {
@@ -523,8 +525,7 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a
523525
}
524526
break;
525527
default:
526-
$isValueQuoted = \in_array($mapping[$i], ['"', "'"]);
527-
$value = self::parseScalar($mapping, $flags, [',', '}', "\n"], $i, null === $tag, $references);
528+
$value = self::parseScalar($mapping, $flags, [',', '}', "\n"], $i, null === $tag, $references, $isValueQuoted);
528529
// Spec: Keys MUST be unique; first one wins.
529530
// Parser cannot abort this mapping earlier, since lines
530531
// are processed sequentially.
@@ -563,8 +564,9 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a
563564
*
564565
* @throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved
565566
*/
566-
private static function evaluateScalar(string $scalar, int $flags, array &$references = [])
567+
private static function evaluateScalar(string $scalar, int $flags, array &$references = [], bool &$isQuotedString = null)
567568
{
569+
$isQuotedString = false;
568570
$scalar = trim($scalar);
569571

570572
if (0 === strpos($scalar, '*')) {
@@ -600,7 +602,14 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
600602
case '!' === $scalar[0]:
601603
switch (true) {
602604
case 0 === strpos($scalar, '!!str '):
603-
return (string) substr($scalar, 6);
605+
$s = (string) substr($scalar, 6);
606+
607+
if (\in_array($s[0] ?? '', ['"', "'"], true)) {
608+
$isQuotedString = true;
609+
$s = self::parseQuotedScalar($s);
610+
}
611+
612+
return $s;
604613
case 0 === strpos($scalar, '! '):
605614
return substr($scalar, 2);
606615
case 0 === strpos($scalar, '!php/object'):

Tests/InlineTest.php

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

949+
public function testParseSingleQuotedTaggedString()
950+
{
951+
$this->assertSame('foo', Inline::parse("!!str 'foo'"));
952+
}
953+
954+
public function testParseDoubleQuotedTaggedString()
955+
{
956+
$this->assertSame('foo', Inline::parse('!!str "foo"'));
957+
}
958+
949959
public function testParseQuotedReferenceLikeStringsInMapping()
950960
{
951961
$yaml = <<<YAML
952-
{foo: '&foo', bar: "&bar"}
962+
{foo: '&foo', bar: "&bar", baz: !!str '&baz'}
953963
YAML;
954964

955-
$this->assertSame(['foo' => '&foo', 'bar' => '&bar'], Inline::parse($yaml));
965+
$this->assertSame(['foo' => '&foo', 'bar' => '&bar', 'baz' => '&baz'], Inline::parse($yaml));
956966
}
957967

958968
public function testParseQuotedReferenceLikeStringsInSequence()
959969
{
960970
$yaml = <<<YAML
961-
['&foo', "&bar" ]
971+
['&foo', "&bar", !!str '&baz']
962972
YAML;
963973

964-
$this->assertSame(['&foo', '&bar'], Inline::parse($yaml));
974+
$this->assertSame(['&foo', '&bar', '&baz'], Inline::parse($yaml));
965975
}
966976
}

0 commit comments

Comments
 (0)