From 91df68215829c7366ae3be844197c68781c36657 Mon Sep 17 00:00:00 2001 From: John Kleijn Date: Sat, 6 Feb 2016 16:13:32 +0100 Subject: [PATCH 1/5] Fixed list handing when dealing with object maps --- src/Symfony/Component/Yaml/Parser.php | 8 +++++++- .../Component/Yaml/Tests/ParserTest.php | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index 9e4da1766431e..e932268d6dc43 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -302,7 +302,13 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport = } if ($objectForMap && !is_object($data)) { - $data = (object) $data; + if (is_array($data)) { + foreach (array_keys($data) as $index => $key) { + if ($index !== $key) { + return empty($data) ? null : (object) $data; + } + } + } } return empty($data) ? null : $data; diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index 579cf8ce7d37e..9e1980cc9f005 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -479,6 +479,24 @@ public function testObjectForMapIsAppliedAfterParsing() $this->assertEquals($expected, $this->parser->parse("foo: bar\nbaz: foobar", false, false, true)); } + public function testWillObjectForMapOptionWillIgnoreArrays() + { + $yaml = <<parser->parse($yaml, true, false, true); + $this->assertInternalType('object', $actual); + + $this->assertInternalType('array', $actual->array); + $this->assertInternalType('object', $actual->array[0]); + $this->assertInternalType('object', $actual->array[1]); + $this->assertSame('one', $actual->array[0]->key); + $this->assertSame('two', $actual->array[1]->key); + } + /** * @dataProvider invalidDumpedObjectProvider * @expectedException \Symfony\Component\Yaml\Exception\ParseException From 9ab907baab5c4253688161c0d7f398ec1960e1ab Mon Sep 17 00:00:00 2001 From: John Kleijn Date: Sat, 6 Feb 2016 16:19:40 +0100 Subject: [PATCH 2/5] Removed newlines to trigger PR header check --- src/Symfony/Component/Yaml/Tests/ParserTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index 9e1980cc9f005..72456f740ab81 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -486,7 +486,6 @@ public function testWillObjectForMapOptionWillIgnoreArrays() - key: one - key: two YAML; - $actual = $this->parser->parse($yaml, true, false, true); $this->assertInternalType('object', $actual); From a43a6363857ebc4d6073a3b43dbb6e794f9fcfb9 Mon Sep 17 00:00:00 2001 From: John Kleijn Date: Sat, 6 Feb 2016 16:43:29 +0100 Subject: [PATCH 3/5] Minor tweak --- src/Symfony/Component/Yaml/Parser.php | 10 ++++------ src/Symfony/Component/Yaml/Tests/ParserTest.php | 11 +++++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index e932268d6dc43..fc001c1e992c8 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -301,12 +301,10 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport = mb_internal_encoding($mbEncoding); } - if ($objectForMap && !is_object($data)) { - if (is_array($data)) { - foreach (array_keys($data) as $index => $key) { - if ($index !== $key) { - return empty($data) ? null : (object) $data; - } + if ($objectForMap && is_array($data)) { + foreach (array_keys($data) as $index => $key) { + if ($index !== $key) { + return (object) $data; } } } diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index 72456f740ab81..a80dab229784b 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -496,6 +496,17 @@ public function testWillObjectForMapOptionWillIgnoreArrays() $this->assertSame('two', $actual->array[1]->key); } + public function testWillObjectForMapOptionWillIgnoreEmptyArrays() + { + $yaml = <<parser->parse($yaml, true, false, true); + $this->assertInternalType('object', $actual); + + $this->assertInternalType('array', $actual->array); + } + /** * @dataProvider invalidDumpedObjectProvider * @expectedException \Symfony\Component\Yaml\Exception\ParseException From b4f889d4acfc2956e2a3e7560b595a64b18e904b Mon Sep 17 00:00:00 2001 From: John Kleijn Date: Sat, 6 Feb 2016 19:39:40 +0100 Subject: [PATCH 4/5] Refs #17709 --- src/Symfony/Component/Yaml/Parser.php | 6 +++++- src/Symfony/Component/Yaml/Tests/ParserTest.php | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index fc001c1e992c8..29573fd9069ca 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -304,7 +304,11 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport = if ($objectForMap && is_array($data)) { foreach (array_keys($data) as $index => $key) { if ($index !== $key) { - return (object) $data; + $object = new \stdClass; + foreach ($data as $key => $value) { + $object->$key = $value; + } + return $object; } } } diff --git a/src/Symfony/Component/Yaml/Tests/ParserTest.php b/src/Symfony/Component/Yaml/Tests/ParserTest.php index a80dab229784b..ff231a1c329bc 100644 --- a/src/Symfony/Component/Yaml/Tests/ParserTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParserTest.php @@ -507,6 +507,22 @@ public function testWillObjectForMapOptionWillIgnoreEmptyArrays() $this->assertInternalType('array', $actual->array); } + public function testCanParseNumericMap() + { + $yaml = <<parser->parse($yaml, true, false, true); + $this->assertInternalType('object', $actual); + $this->assertInternalType('object', $actual->map); + $this->assertTrue(property_exists($actual->map, '1')); + $this->assertTrue(property_exists($actual->map, '2')); + $this->assertSame('one', $actual->map->{'1'}); + $this->assertSame('two', $actual->map->{'2'}); + } + /** * @dataProvider invalidDumpedObjectProvider * @expectedException \Symfony\Component\Yaml\Exception\ParseException From 6ecf1de55b92dd972058bbef81427a37ce9c4670 Mon Sep 17 00:00:00 2001 From: John Kleijn Date: Sat, 6 Feb 2016 19:41:54 +0100 Subject: [PATCH 5/5] CS fix --- src/Symfony/Component/Yaml/Parser.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index 29573fd9069ca..176ee3f0bd058 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -304,10 +304,11 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport = if ($objectForMap && is_array($data)) { foreach (array_keys($data) as $index => $key) { if ($index !== $key) { - $object = new \stdClass; + $object = new \stdClass(); foreach ($data as $key => $value) { $object->$key = $value; } + return $object; } }