merged branch gajdaw/2_1_component_yaml_fix_4022 (PR #4126)

Commits
-------

80a2a92 [2.1][Component][Yaml] fix 4022

Discussion
----------

[2.1][Component][Yaml] fix 4022

Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: [![Build Status](https://secure.travis-ci.org/gajdaw/symfony.png?branch=2_1_component_yaml_fix_4022)](http://travis-ci.org/gajdaw/symfony)
Fixes the following tickets: #4121, #4022, #4135
Todo:

---------------------------------------------------------------------------

by stof at 2012-04-27T13:03:15Z

Why is it marked as ``[2.2]`` if it is a bugfix ?

@fabpot ping

---------------------------------------------------------------------------

by gajdaw at 2012-04-27T14:42:21Z

The title should be [2.1] - now it is correct.

I marked it 2.0 and PR was for 2.0 originally.

Fabien suggested that it should go to master branch: https://github.com/symfony/symfony/pull/4121#issuecomment-5362990

---------------------------------------------------------------------------

by fabpot at 2012-05-07T09:17:31Z

That does not work when you have something after the unindented collection:

    collection:
        key:
        - a
        - b
        - c
    foo: bar

---------------------------------------------------------------------------

by gajdaw at 2012-05-07T11:11:30Z

@fabpot Last commit contains test with your yaml:

    collection:
        key:
        - a
        - b
        - c
    foo: bar

Everything seems fine. Can you give me a hint: what do you mean, when you say "That does not work"?

---------------------------------------------------------------------------

by fabpot at 2012-05-07T12:36:19Z

Sorry, the failing test is the following:

    test: Key/value after unindented collection
    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'))

---------------------------------------------------------------------------

by gajdaw at 2012-05-07T15:48:26Z

@fabpot Last commit passed your test.

---------------------------------------------------------------------------

by fabpot at 2012-05-07T17:28:21Z

Can you squash your commits? Thanks.

---------------------------------------------------------------------------

by travisbot at 2012-05-08T05:32:58Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1273487) (merged 20891c58 into 919604ab).

---------------------------------------------------------------------------

by gajdaw at 2012-05-08T05:36:51Z

Done.

---------------------------------------------------------------------------

by travisbot at 2012-05-08T07:23:47Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1274162) (merged 80a2a92e into 898ff4e0).
This commit is contained in:
Fabien Potencier 2012-05-08 09:27:29 +02:00
commit e54f4e46c9
4 changed files with 138 additions and 2 deletions

View File

@ -159,7 +159,7 @@ class Parser
// 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;
@ -275,7 +275,9 @@ class Parser
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 {
@ -284,7 +286,15 @@ class Parser
$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);
@ -553,4 +563,47 @@ class Parser
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, '- '));
}
}

View File

@ -15,3 +15,4 @@
- YtsNullsAndEmpties
- YtsSpecificationExamples
- YtsTypeTransfers
- unindentedCollections

View File

@ -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'))

View File

@ -140,6 +140,26 @@ EOF
}
}
}
/**
*
* @expectedException Symfony\Component\Yaml\Exception\ParseException
*
*/
public function testUnindentedCollectionException()
{
$yaml = <<<EOF
collection:
-item1
-item2
-item3
EOF;
$this->parser->parse($yaml);
}
}
class B