Skip to content

Commit

Permalink
[2.1][Component][Yaml] fix 4022
Browse files Browse the repository at this point in the history
  • Loading branch information
gajdaw committed May 8, 2012
1 parent 76ef8da commit 80a2a92
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 2 deletions.
57 changes: 55 additions & 2 deletions src/Symfony/Component/Yaml/Parser.php
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,9 @@ private function getNextEmbedBlock($indentation = null)
if (null === $indentation) {
$newIndent = $this->getCurrentLineIndentation();

if (!$this->isCurrentLineEmpty() && 0 == $newIndent) {
$unindentedEmbedBlock = $this->isStringUnIndentedCollectionItem($this->currentLine);

if (!$this->isCurrentLineEmpty() && 0 === $newIndent && !$unindentedEmbedBlock) {
throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
}
} else {
Expand All @@ -284,7 +286,15 @@ private function getNextEmbedBlock($indentation = null)

$data = array(substr($this->currentLine, $newIndent));

$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem($this->currentLine);

while ($this->moveToNextLine()) {

if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem($this->currentLine)) {
$this->moveToPreviousLine();
break;
}

if ($this->isCurrentLineEmpty()) {
if ($this->isCurrentLineBlank()) {
$data[] = substr($this->currentLine, $newIndent);
Expand Down Expand Up @@ -553,4 +563,47 @@ 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
&&
$this->isStringUnIndentedCollectionItem($this->currentLine)
) {
$ret = true;
}

$this->moveToPreviousLine();

return $ret;
}

/**
* Returns true if the string is unindented collection item
*
* @return Boolean Returns true if the string is unindented collection item, false otherwise
*/
private function isStringUnIndentedCollectionItem($string)
{
return (0 === strpos($this->currentLine, '- '));
}

}
1 change: 1 addition & 0 deletions src/Symfony/Component/Yaml/Tests/Fixtures/index.yml
Expand Up @@ -15,3 +15,4 @@
- YtsNullsAndEmpties
- YtsSpecificationExamples
- YtsTypeTransfers
- unindentedCollections
@@ -0,0 +1,62 @@
--- %YAML:1.0
test: Unindented collection
brief: >
Unindented collection
yaml: |
collection:
- item1
- item2
- item3
php: |
array('collection' => array('item1', 'item2', 'item3'))
---
test: Nested unindented collection (two levels)
brief: >
Nested unindented collection
yaml: |
collection:
key:
- a
- b
- c
php: |
array('collection' => array('key' => array('a', 'b', 'c')))
---
test: Nested unindented collection (three levels)
brief: >
Nested unindented collection
yaml: |
collection:
key:
subkey:
- one
- two
- three
php: |
array('collection' => array('key' => array('subkey' => array('one', 'two', 'three'))))
---
test: Key/value after unindented collection (1)
brief: >
Key/value after unindented collection (1)
yaml: |
collection:
key:
- a
- b
- c
foo: bar
php: |
array('collection' => array('key' => array('a', 'b', 'c')), 'foo' => 'bar')
---
test: Key/value after unindented collection (at the same level)
brief: >
Key/value after unindented collection
yaml: |
collection:
key:
- a
- b
- c
foo: bar
php: |
array('collection' => array('key' => array('a', 'b', 'c'), 'foo' => 'bar'))
20 changes: 20 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Expand Up @@ -140,6 +140,26 @@ public function testNonUtf8Exception()
}
}
}

/**
*
* @expectedException Symfony\Component\Yaml\Exception\ParseException
*
*/
public function testUnindentedCollectionException()
{
$yaml = <<<EOF
collection:
-item1
-item2
-item3
EOF;

$this->parser->parse($yaml);
}

}

class B
Expand Down

0 comments on commit 80a2a92

Please sign in to comment.