[Yaml] fixed parsing when a mapping is mixed within a sequence and vice-versa (closes #4634)
This commit is contained in:
parent
2e356c1ab9
commit
a1b73887f7
@ -60,6 +60,7 @@ class Parser
|
|||||||
}
|
}
|
||||||
|
|
||||||
$data = array();
|
$data = array();
|
||||||
|
$context = null;
|
||||||
while ($this->moveToNextLine()) {
|
while ($this->moveToNextLine()) {
|
||||||
if ($this->isCurrentLineEmpty()) {
|
if ($this->isCurrentLineEmpty()) {
|
||||||
continue;
|
continue;
|
||||||
@ -72,6 +73,11 @@ class Parser
|
|||||||
|
|
||||||
$isRef = $isInPlace = $isProcessed = false;
|
$isRef = $isInPlace = $isProcessed = false;
|
||||||
if (preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+?))?\s*$#u', $this->currentLine, $values)) {
|
if (preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+?))?\s*$#u', $this->currentLine, $values)) {
|
||||||
|
if ($context && 'mapping' == $context) {
|
||||||
|
throw new ParseException('You cannot define a sequence item when in a mapping');
|
||||||
|
}
|
||||||
|
$context = 'sequence';
|
||||||
|
|
||||||
if (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
|
if (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
|
||||||
$isRef = $matches['ref'];
|
$isRef = $matches['ref'];
|
||||||
$values['value'] = $matches['value'];
|
$values['value'] = $matches['value'];
|
||||||
@ -104,6 +110,11 @@ class Parser
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} elseif (preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->currentLine, $values)) {
|
} elseif (preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->currentLine, $values)) {
|
||||||
|
if ($context && 'sequence' == $context) {
|
||||||
|
throw new ParseException('You cannot define a mapping item when in a sequence');
|
||||||
|
}
|
||||||
|
$context = 'mapping';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$key = Inline::parseScalar($values['key']);
|
$key = Inline::parseScalar($values['key']);
|
||||||
} catch (ParseException $e) {
|
} catch (ParseException $e) {
|
||||||
|
@ -160,6 +160,31 @@ EOF;
|
|||||||
$this->parser->parse($yaml);
|
$this->parser->parse($yaml);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Symfony\Component\Yaml\Exception\ParseException
|
||||||
|
*/
|
||||||
|
public function testSequenceInAMapping()
|
||||||
|
{
|
||||||
|
Yaml::parse(<<<EOF
|
||||||
|
yaml:
|
||||||
|
hash: me
|
||||||
|
- array stuff
|
||||||
|
EOF
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Symfony\Component\Yaml\Exception\ParseException
|
||||||
|
*/
|
||||||
|
public function testMappingInASequence()
|
||||||
|
{
|
||||||
|
Yaml::parse(<<<EOF
|
||||||
|
yaml:
|
||||||
|
- array stuff
|
||||||
|
hash: me
|
||||||
|
EOF
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class B
|
class B
|
||||||
|
Reference in New Issue
Block a user