8000 Merge branch '2.3' into 2.7 · symfony/symfony@eac5868 · GitHub
[go: up one dir, main page]

Skip to content

Commit eac5868

Browse files
committed
Merge branch '2.3' into 2.7
* 2.3: [Yaml] fix exception contexts People - person singularization [Yaml] properly handle unindented collections chomp newlines only at the end of YAML documents
2 parents f55680b + 53b7236 commit eac5868

File tree

4 files changed

+61
-13
lines changed

4 files changed

+61
-13
lines changed

src/Symfony/Component/PropertyAccess/StringUtil.php

Lines changed: 3 additions & 0 deletions
< 10000 table aria-label="Diff for: src/Symfony/Component/PropertyAccess/StringUtil.php" class="tab-size width-full DiffLines-module__tableLayoutFixed--ZmaVx" data-diff-anchor="diff-6c196420220a3a1d16e56cb2c657b56e8ff6f1770021ec87c619e4b29e9b8643" data-tab-size="8" data-paste-markdown-skip="true" role="grid" style="--line-number-cell-width:44px;--line-number-cell-width-unified:88px">Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ class StringUtil
124124

125125
// chateaux (chateau)
126126
array('xuae', 4, false, true, 'eau'),
127+
128+
// people (person)
129+
array('elpoep', 6, true, true, 'person'),
127130
);
128131

129132
/**

src/Symfony/Component/PropertyAccess/Tests/StringUtilTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ public function singularifyProvider()
107107
array('objectives', 'objective'),
108108
array('oxen', 'ox'),
109109
array('parties', 'party'),
110+
array('people', 'person'),
111+
array('persons', 'person'),
110112
array('phenomena', array('phenomenon', 'phenomenum')),
111113
array('photos', 'photo'),
112114
array('pianos', 'piano'),

src/Symfony/Component/Yaml/Parser.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Parser
2525
const FOLDED_SCALAR_PATTERN = self::BLOCK_SCALAR_HEADER_PATTERN;
2626

2727
private $offset = 0;
28+
private $totalNumberOfLines;
2829
private $lines = array();
2930
private $currentLineNb = -1;
3031
private $currentLine = '';
@@ -33,11 +34,13 @@ class Parser
3334
/**
3435
* Constructor.
3536
*
36-
* @param int $offset The offset of YAML document (used for line numbers in error messages)
37+
* @param int $offset The offset of YAML document (used for line numbers in error messages)
38+
* @param int|null $totalNumberOfLines The overall number of lines being parsed
3739
*/
38-
public function __construct($offset = 0)
40+
public function __construct($offset = 0, $totalNumberOfLines = null)
3941
{
4042
$this->offset = $offset;
43+
$this->totalNumberOfLines = $totalNumberOfLines;
4144
}
4245

4346
/**
@@ -62,6 +65,10 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
6265
$value = $this->cleanup($value);
6366
$this->lines = explode("\n", $value);
6467

68+
if (null === $this->totalNumberOfLines) {
69+
$this->totalNumberOfLines = count($this->lines);
70+
}
71+
6572
if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
6673
$mbEncoding = mb_internal_encoding();
6774
mb_internal_encoding('UTF-8');
@@ -83,7 +90,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
8390
$isRef = $mergeNode = false;
8491
if (preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+?))?\s*$#u', $this->currentLine, $values)) {
8592
if ($context && 'mapping' == $context) {
86-
throw new ParseException('You cannot define a sequence item when in a mapping');
93+
throw new ParseException('You cannot define a sequence item when in a mapping', $this->getRealCurrentLineNb() + 1, $this->currentLine);
8794
}
8895
$context = 'sequence';
8996

@@ -95,7 +102,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
95102
// array
96103
if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
97104
$c = $this->getRealCurrentLineNb() + 1;
98-
$parser = new self($c);
105+
$parser = new self($c, $this->totalNumberOfLines);
99106
$parser->refs = &$this->refs;
100107
$data[] = $parser->parse($this->getNextEmbedBlock(null, true), $exceptionOnInvalidType, $objectSupport, $objectForMap);
101108
} else {
@@ -104,7 +111,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
104111
) {
105112
// this is a compact notation element, add to next block and parse
106113
$c = $this->getRealCurrentLineNb();
107-
$parser = new self($c);
114+
$parser = new self($c, $this->totalNumberOfLines);
108115
$pa 10000 rser->refs = &$this->refs;
109116

110117
$block = $values['value'];
@@ -122,7 +129,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
122129
}
123130
} elseif (preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->currentLine, $values) && (false === strpos($values['key'], ' #') || in_array($values['key'][0], array('"', "'")))) {
124131
if ($context && 'sequence' == $context) {
125-
throw new ParseException('You cannot define a mapping item when in a sequence');
132+
throw new ParseException('You cannot define a mapping item when in a sequence', $this->currentLineNb + 1, $this->currentLine);
126133
}
127134
$context = 'mapping';
128135

@@ -169,7 +176,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
169176
$value = $this->getNextEmbedBlock();
170177
}
171178
$c = $this->getRealCurrentLineNb() + 1;
172-
$parser = new self($c);
179+
$parser = new self($c, $this->totalNumberOfLines);
173180
$parser->refs = &$this->refs;
174181
$parsed = $parser->parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap);
175182

@@ -220,7 +227,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
220227
}
221228
} else {
222229
$c = $this->getRealCurrentLineNb() + 1;
223-
$parser = new self($c);
230+
$parser = new self($c, $this->totalNumberOfLines);
224231
$parser->refs = &$this->refs;
225232
$value = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport, $objectForMap);
226233
// Spec: Keys MUST be unique; first one wins.
@@ -247,7 +254,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
247254
} else {
248255
// multiple documents are not supported
249256
if ('---' === $this->currentLine) {
250-
throw new ParseException('Multiple documents are not supported.');
257+
throw new ParseException('Multiple documents are not supported.', $this->currentLineNb + 1, $this->currentLine);
251258
}
252259

253260
// 1-liner optionally followed by newline(s)
@@ -483,7 +490,7 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $ob
483490
}
484491

485492
if (!array_key_exists($value, $this->refs)) {
486-
throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLine);
493+
throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLineNb + 1, $this->currentLine);
487494
}
488495

489496
return $this->refs[$value];
@@ -569,6 +576,8 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
569576
if ($notEOF) {
570577
$blockLines[] = '';
571578
$this->moveToPreviousLine();
579+
} elseif (!$notEOF && !$this->isCurrentLineLastLineInDocument()) {
580+
$blockLines[] = '';
572581
}
573582

574583
// folded style
@@ -675,6 +684,11 @@ private function isCurrentLineComment()
675684
return '' !== $ltrimmedLine && $ltrimmedLine[0] === '#';
676685
}
677686

687+
private function isCurrentLineLastLineInDocument()
688+
{
689+
return ($this->offset + $this->currentLineNb) >= ($this->totalNumberOfLines - 1);
690+
}
691+
678692
/**
679693
* Cleanups a YAML string to be parsed.
680694
*
@@ -752,7 +766,7 @@ private function isNextLineUnIndentedCollection()
752766
*/
753767
private function isStringUnIndentedCollectionItem()
754768
{
755-
return 0 === strpos($this->currentLine, '- ');
769+
return '-' === rtrim($this->currentLine) || 0 === strpos($this->currentLine, '- ');
756770
}
757771

758772
/**

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ public function testShortcutKeyUnindentedCollectionException()
547547

548548
/**
549549
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
550-
* @expectedExceptionMessage Multiple documents are not supported.
550+
* @expectedExceptionMessageRegExp /^Multiple documents are not supported.+/
551551
*/
552552
public function testMultipleDocumentsNotSupportedException()
553553
{
@@ -579,6 +579,34 @@ public function testSequenceInAMapping()
579579
);
580580
}
581581

582+
public function testSequenceInMappingStartedBySingleDashLine()
583+
{
584+
$yaml = <<<EOT
585+
a:
586+
-
587+
b:
588+
-
589+
bar: baz
590+
- foo
591+
d: e
592+
EOT;
593+
$expected = array(
594+
'a' => array(
595+
array(
596+
'b' => array(
597+
array(
598+
'bar' => 'baz',
599+
),
600+
),
601+
),
602+
'foo',
603+
),
604+
'd' => 'e',
605+
);
606+
607+
$this->assertSame($expected, $this->parser->parse($yaml));
608+
}
609+
582610
/**
583611
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
584612
*/
@@ -905,6 +933,7 @@ public function getCommentLikeStringInScalarBlockData()
905933
foo
906934
# bar
907935
baz
936+
908937
EOT
909938
,
910939
),
@@ -933,7 +962,7 @@ public function getCommentLikeStringInScalarBlockData()
933962
$expected = array(
934963
'foo' => array(
935964
'bar' => array(
936-
'scalar-block' => 'line1 line2>',
965+
'scalar-block' => "line1 line2>\n",
937966
),
938967
'baz' => array(
939968
'foobar' => null,

0 commit comments

Comments
 (0)
0