8000 [2.0][Component][Yaml] fix 4022 by gajdaw · Pull Request #4121 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[2.0][Component][Yaml] fix 4022 #4121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function parse($value)
// hash
} elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
// if next line is less indented or equal, then it means that the current value is null
if ($this->isNextLineIndented()) {
if ($this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) {
$data[$key] = null;
} else {
$c = $this->getRealCurrentLineNb() + 1;
Expand Down Expand Up @@ -275,7 +275,12 @@ private function getNextEmbedBlock($indentation = null)
if (null === $indentation) {
$newIndent = $this->getCurrentLineIndentation();

if (!$this->isCurrentLineEmpty() && 0 == $newIndent) {
$unindentedEmbedBlock = false;
if (preg_match('/^- .+/', $this->currentLine)) {
$unindentedEmbedBlock = true;
}

if (!$this->isCurrentLineEmpty() && 0 == $newIndent && !$unindentedEmbedBlock) {
throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
}
} else {
Expand Down Expand Up @@ -553,4 +558,37 @@ private function cleanup($value)

return $value;
}

/**
* Returns true if the next line starts unindented collection
*
* @return Boolean Returns true if the next line starts unindented collection, false otherwise
*/
private function isNextLineUnIndentedCollection()
{
$currentIndentation = $this->getCurrentLineIndentation();
$notEOF = $this->moveToNextLine();

while ($notEOF && $this->isCurrentLineEmpty()) {
$notEOF = $this->moveToNextLine();
}

if (false === $notEOF) {
return false;
}

$ret = false;
if (
$this->getCurrentLineIndentation() == $currentIndentation
&&
preg_match('/^- .*/', $this->currentLine)
) {
$ret = true;
}

$this->moveToPreviousLine();

return $ret;
}

}
17 changes: 17 additions & 0 deletions tests/Symfony/Tests/Component/Yaml/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,23 @@ public function testNonUtf8Exception()
}
}
}

public function testIssue4022()
{
$yaml = <<<EOF

collection:
- item1
- item2
- item3

EOF;

$array = array('collection' => array('item1', 'item2', 'item3'));

$this->assertEquals($array, $this->parser->parse($yaml));
}

}

class B
Expand Down
0