From 51cfb477e961d7d0a62464fcdd132cdff7cc2a7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Tue, 7 Jun 2016 16:21:34 +0200 Subject: [PATCH 01/12] Add support for XmlReader objets --- .../VarDumper/Caster/XmlReaderCaster.php | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php diff --git a/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php b/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php new file mode 100644 index 0000000000000..1d700b6db8a41 --- /dev/null +++ b/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php @@ -0,0 +1,78 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\VarDumper\Caster; + +use Symfony\Component\VarDumper\Cloner\Stub; + +/** + * Casts XmlReader class to array representation. + * + * @author Baptiste Clavié + */ +class XmlReaderCaster +{ + private static $nodeTypes = array( + \XmlReader::NONE => 'NONE', + \XmlReader::ELEMENT => 'ELEMENT', + \XmlReader::ATTRIBUTE => 'ATTRIBUTE', + \XmlReader::TEXT => 'TEXT', + \XmlReader::CDATA => 'CDATA', + \XmlReader::ENTITY_REF => 'ENTITY_REF', + \XmlReader::ENTITY => 'ENTITY', + \XmlReader::PI => 'PI', + \XmlReader::COMMENT => 'COMMENT', + \XmlReader::DOC => 'DOC', + \XmlReader::DOC_TYPE => 'DOC_TYPE', + \XmlReader::DOC_FRAGMENT => 'DOC_FRAGMENT', + \XmlReader::NOTATION => 'NOTATION', + \XmlReader::WHITESPACE => 'WHITESPACE', + \XmlReader::SIGNIFICANT_WHITESPACE => 'SIGNIFICANT_WHITESPACE', + \XmlReader::END_ELEMENT => 'END_ELEMENT', + \XmlReader::END_ENTITY => 'END_ENTITY', + \XmlReader::XML_DECLARATION => 'XML_DECLARATION' + ); + + public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $isNested) + { + $nodeType = new ConstStub(self::$nodeTypes[$reader->nodeType], $reader->nodeType); + + $infos = array( + 'localName' => $reader->localName, + + 'depth' => $reader->depth, + + 'attributeCount' => $reader->attributeCount, + 'hasAttributes' => $reader->hasAttributes, + + 'hasValue' => $reader->hasValue, + 'isDefault' => $reader->isDefault, + 'isEmptyElement' => $reader->isEmptyElement, + 'nodeType' => $nodeType, + ); + + if ($reader->hasValue && (\XmlReader::TEXT === $reader->nodeType || \XmlReader::ATTRIBUTE === $reader->nodeType)) { + $infos['value'] = $reader->value; + + unset($infos['localName']); + $stub->cut += 1; + } + + if ($reader->hasAttributes) { + $infos[Caster::PREFIX_VIRTUAL . 'attributes'] = array(); + + while ($reader->moveToNextAttribute()) { + $infos[Caster::PREFIX_VIRTUAL . 'attributes'][$reader->name] = $reader->value; + } + } + + return $a + $infos; + } +} From 5d83a719985cdf02133976278661cb7a38000aee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Tue, 7 Jun 2016 16:25:51 +0200 Subject: [PATCH 02/12] Add XmlReaderCaster in the default casters --- src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php index 99ca07f264d76..ea1495519547c 100644 --- a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php +++ b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php @@ -67,6 +67,8 @@ abstract class AbstractCloner implements ClonerInterface 'DOMProcessingInstruction' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castProcessingInstruction', 'DOMXPath' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castXPath', + 'XmlReader' => 'Symfony\Component\VarDumper\Caster\XmlReaderCaster::castXmlReader', + 'ErrorException' => 'Symfony\Component\VarDumper\Caster\ExceptionCaster::castErrorException', 'Exception' => 'Symfony\Component\VarDumper\Caster\ExceptionCaster::castException', 'Error' => 'Symfony\Component\VarDumper\Caster\ExceptionCaster::castError', From 71558e5b5508b33a1aa08b15f150152eefd9e488 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Tue, 7 Jun 2016 17:00:48 +0200 Subject: [PATCH 03/12] Do not touch XmlReader state while reading attributes --- src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php b/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php index 1d700b6db8a41..88618f58c8cce 100644 --- a/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php @@ -66,10 +66,10 @@ public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $ } if ($reader->hasAttributes) { - $infos[Caster::PREFIX_VIRTUAL . 'attributes'] = array(); + $infos[Caster::PREFIX_VIRTUAL.'attributes'] = array(); - while ($reader->moveToNextAttribute()) { - $infos[Caster::PREFIX_VIRTUAL . 'attributes'][$reader->name] = $reader->value; + for ($i = 0; $i < $reader->attributeCount; ++$i) { + $infos[Caster::PREFIX_VIRTUAL.'attributes'][] = $reader->getAttributeNo($i); } } From 3250333d756c8293eb42b228fcebea0732978a56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Tue, 7 Jun 2016 17:29:25 +0200 Subject: [PATCH 04/12] Ignore (partially) some node types --- .../VarDumper/Caster/XmlReaderCaster.php | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php b/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php index 88618f58c8cce..9f53d141798ac 100644 --- a/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php @@ -37,7 +37,22 @@ class XmlReaderCaster \XmlReader::SIGNIFICANT_WHITESPACE => 'SIGNIFICANT_WHITESPACE', \XmlReader::END_ELEMENT => 'END_ELEMENT', \XmlReader::END_ENTITY => 'END_ENTITY', - \XmlReader::XML_DECLARATION => 'XML_DECLARATION' + \XmlReader::XML_DECLARATION => 'XML_DECLARATION', + ); + + private static $filteredTypes = array( + \XmlReader::ATTRIBUTE => true, + \XmlReader::ENTITY_REF => true, + \XmlReader::ENTITY => true, + \XmlReader::PI => true, + \XmlReader::DOC => true, + \XmlReader::DOC_TYPE => true, + \XmlReader::DOC_FRAGMENT => true, + \XmlReader::NOTATION => true, + \XmlReader::WHITESPACE => true, + \XmlReader::SIGNIFICANT_WHITESPACE => true, + \XmlReader::END_ELEMENT => true, + \XmlReader::END_ENTITY => true, ); public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $isNested) @@ -73,6 +88,21 @@ public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $ } } + if (isset(static::$filteredTypes[$reader->nodeType])) { + $cut = array( + 'nodeType' => $nodeType, + 'depth' => $reader->depth, + ); + + if ('#text' !== $reader->localName) { + $cut['localName'] = $reader->localName; + } + + $stub->cut += count($infos) - count($cut); + + return $cut; + } + return $a + $infos; } } From 513e73bc973633052ed0248986f31e4bf4a06aea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Thu, 16 Jun 2016 17:41:09 +0200 Subject: [PATCH 05/12] Add some more info on ATTRIBUTES elements --- .../Component/VarDumper/Caster/XmlReaderCaster.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php b/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php index 9f53d141798ac..d03f2dd857c64 100644 --- a/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php @@ -98,6 +98,14 @@ public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $ $cut['localName'] = $reader->localName; } + if (\XmlReader::ATTRIBUTE === $reader->nodeType) { + $cut['hasValue'] = $reader->hasValue; + + if ($reader->hasValue) { + $cut['value'] = $reader->value; + } + } + $stub->cut += count($infos) - count($cut); return $cut; From 5030ea5e457429ed6de56e1a04ab7e8758a99285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Thu, 16 Jun 2016 16:53:05 +0200 Subject: [PATCH 06/12] Add unit tests for XmlReader Caster --- .../Tests/Caster/XmlReaderCasterTest.php | 222 ++++++++++++++++++ .../VarDumper/Tests/Fixtures/xml_reader.xml | 7 + 2 files changed, 229 insertions(+) create mode 100644 src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php create mode 100644 src/Symfony/Component/VarDumper/Tests/Fixtures/xml_reader.xml diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php new file mode 100644 index 0000000000000..9a8b51c6a9456 --- /dev/null +++ b/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php @@ -0,0 +1,222 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\VarDumper\Tests\Caster; + +use Symfony\Component\VarDumper\Test\VarDumperTestTrait; + +/** + * @author Baptiste Clavié + */ +class XmlReaderCasterTest extends \PHPUnit_Framework_TestCase +{ + use VarDumperTestTrait; + + /** @var \XmlReader */ + private $reader; + + public function setUp() + { + $this->reader = new \XmlReader(); + $this->reader->open(__DIR__.'/../Fixtures/xml_reader.xml'); + } + + public function testSimpleElementNode() + { + $dump = <<<'DUMP' +XMLReader { + +localName: "foo" + +depth: 0 + +attributeCount: 0 + +hasAttributes: false + +hasValue: false + +isDefault: false + +isEmptyElement: false + +nodeType: ELEMENT +} +DUMP; + + while ($this->reader->read() && $this->reader->nodeType !== \XmlReader::ELEMENT); + + $this->assertDumpEquals($dump, $this->reader); + } + + public function testNestedElement() + { + $dump = <<<'DUMP' +XMLReader { + +localName: "bar" + +depth: 1 + +attributeCount: 0 + +hasAttributes: false + +hasValue: false + +isDefault: false + +isEmptyElement: false + +nodeType: ELEMENT +} + +DUMP; + + while ($this->reader->read() && $this->reader->localName !== 'bar'); + + $this->assertDumpEquals($dump, $this->reader); + } + + public function testEmptyElement() + { + $dump = <<<'DUMP' +XMLReader { + +localName: "bar" + +depth: 1 + +attributeCount: 0 + +hasAttributes: false + +hasValue: false + +isDefault: false + +isEmptyElement: true + +nodeType: ELEMENT +} + +DUMP; + + while ($this->reader->read() && $this->reader->localName !== 'bar'); + + $this->reader->next('bar'); + + $this->assertDumpEquals($dump, $this->reader); + } + + public function testElementWithAttributes() + { + $dump = <<<'DUMP' +XMLReader { + +localName: "bar" + +depth: 1 + +attributeCount: 2 + +hasAttributes: true + +hasValue: false + +isDefault: false + +isEmptyElement: false + +nodeType: ELEMENT + attributes: array:2 [ + 0 => "bar" + 1 => "fubar" + ] +} + +DUMP; + + while ($this->reader->read() && $this->reader->localName !== 'bar'); + + $this->reader->next('bar'); + $this->reader->next('bar'); + $this->reader->next('bar'); + + $this->assertDumpEquals($dump, $this->reader); + } + + public function testTextElement() + { + $dump = <<<'DUMP' +XMLReader { + +depth: 2 + +attributeCount: 0 + +hasAttributes: false + +hasValue: true + +isDefault: false + +isEmptyElement: false + +nodeType: TEXT + +value: "With text" + …1 +} + +DUMP; + + while ($this->reader->read() && $this->reader->nodeType !== \XmlReader::TEXT); + + $this->assertDumpEquals($dump, $this->reader); + } + + /** @dataProvider textFilteredProvider */ + public function testFilteredTextElement($nodeType, $nodeTypeName, $depth) + { + $dump = <<reader->read() && $this->reader->nodeType !== $nodeType); + + $this->assertDumpEquals($dump, $this->reader); + } + + public function textFilteredProvider() + { + return array( + 'Significant Whiltespace element' => array(\XmlReader::SIGNIFICANT_WHITESPACE, 'SIGNIFICANT_WHITESPACE', 1), + ); + } + + /** @dataProvider elementFilteredProvider */ + public function testFilteredElement($localName, $nodeType, $nodeTypeName, $expandType, $depth) + { + $dump = <<reader->read() && $this->reader->nodeType !== $nodeType); + + $this->assertDumpEquals($dump, $this->reader); + } + + public function elementFilteredProvider() + { + return array( + 'End Element' => array('bar', \XmlReader::END_ELEMENT, 'END_ELEMENT', 'DOMElement', 1), + ); + } + + public function testAttributeNode() + { + $dump = <<<'DUMP' +XMLReader { + +nodeType: ATTRIBUTE + +depth: 2 + +localName: "foo" + +hasValue: true + +value: "bar" + …4 +} + +DUMP; + + while ($this->reader->read() && !$this->reader->hasAttributes); + + $this->reader->moveToFirstAttribute(); + + $this->assertDumpEquals($dump, $this->reader); + } + + public function tearDown() + { + $this->reader->close(); + } +} + diff --git a/src/Symfony/Component/VarDumper/Tests/Fixtures/xml_reader.xml b/src/Symfony/Component/VarDumper/Tests/Fixtures/xml_reader.xml new file mode 100644 index 0000000000000..00ca046fca4b8 --- /dev/null +++ b/src/Symfony/Component/VarDumper/Tests/Fixtures/xml_reader.xml @@ -0,0 +1,7 @@ + + + + + With text + + From f4428101bdbd6e4a9887d0e6e3c56c586ca81592 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Thu, 16 Jun 2016 18:59:37 +0200 Subject: [PATCH 07/12] Add parser properties virtual property --- .../VarDumper/Caster/XmlReaderCaster.php | 9 ++++ .../Tests/Caster/XmlReaderCasterTest.php | 48 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php b/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php index d03f2dd857c64..3e73b43c36cf4 100644 --- a/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php @@ -71,6 +71,13 @@ public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $ 'isDefault' => $reader->isDefault, 'isEmptyElement' => $reader->isEmptyElement, 'nodeType' => $nodeType, + + Caster::PREFIX_VIRTUAL.'parserProperties' => array( + 'LOADDTD' => $reader->getParserProperty(\XmlReader::LOADDTD), + 'DEFAULTATTRS' => $reader->getParserProperty(\XmlReader::DEFAULTATTRS), + 'VALIDATE' => $reader->getParserProperty(\XmlReader::VALIDATE), + 'SUBST_ENTITIES' => $reader->getParserProperty(\XmlReader::SUBST_ENTITIES), + ), ); if ($reader->hasValue && (\XmlReader::TEXT === $reader->nodeType || \XmlReader::ATTRIBUTE === $reader->nodeType)) { @@ -92,6 +99,8 @@ public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $ $cut = array( 'nodeType' => $nodeType, 'depth' => $reader->depth, + + Caster::PREFIX_VIRTUAL.'parserProperties' => $infos[Caster::PREFIX_VIRTUAL.'parserProperties'], ); if ('#text' !== $reader->localName) { diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php index 9a8b51c6a9456..883afba923195 100644 --- a/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php +++ b/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php @@ -41,6 +41,12 @@ public function testSimpleElementNode() +isDefault: false +isEmptyElement: false +nodeType: ELEMENT + parserProperties: array:4 [ + "LOADDTD" => false + "DEFAULTATTRS" => false + "VALIDATE" => false + "SUBST_ENTITIES" => false + ] } DUMP; @@ -61,6 +67,12 @@ public function testNestedElement() +isDefault: false +isEmptyElement: false +nodeType: ELEMENT + parserProperties: array:4 [ + "LOADDTD" => false + "DEFAULTATTRS" => false + "VALIDATE" => false + "SUBST_ENTITIES" => false + ] } DUMP; @@ -82,6 +94,12 @@ public function testEmptyElement() +isDefault: false +isEmptyElement: true +nodeType: ELEMENT + parserProperties: array:4 [ + "LOADDTD" => false + "DEFAULTATTRS" => false + "VALIDATE" => false + "SUBST_ENTITIES" => false + ] } DUMP; @@ -105,6 +123,12 @@ public function testElementWithAttributes() +isDefault: false +isEmptyElement: false +nodeType: ELEMENT + parserProperties: array:4 [ + "LOADDTD" => false + "DEFAULTATTRS" => false + "VALIDATE" => false + "SUBST_ENTITIES" => false + ] attributes: array:2 [ 0 => "bar" 1 => "fubar" @@ -133,6 +157,12 @@ public function testTextElement() +isDefault: false +isEmptyElement: false +nodeType: TEXT + parserProperties: array:4 [ + "LOADDTD" => false + "DEFAULTATTRS" => false + "VALIDATE" => false + "SUBST_ENTITIES" => false + ] +value: "With text" …1 } @@ -151,6 +181,12 @@ public function testFilteredTextElement($nodeType, $nodeTypeName, $depth) XMLReader { +nodeType: $nodeTypeName +depth: $depth + parserProperties: array:4 [ + "LOADDTD" => false + "DEFAULTATTRS" => false + "VALIDATE" => false + "SUBST_ENTITIES" => false + ] …6 } @@ -175,6 +211,12 @@ public function testFilteredElement($localName, $nodeType, $nodeTypeName, $expan XMLReader { +nodeType: $nodeTypeName +depth: $depth + parserProperties: array:4 [ + "LOADDTD" => false + "DEFAULTATTRS" => false + "VALIDATE" => false + "SUBST_ENTITIES" => false + ] +localName: "$localName" …5 } @@ -199,6 +241,12 @@ public function testAttributeNode() XMLReader { +nodeType: ATTRIBUTE +depth: 2 + parserProperties: array:4 [ + "LOADDTD" => false + "DEFAULTATTRS" => false + "VALIDATE" => false + "SUBST_ENTITIES" => false + ] +localName: "foo" +hasValue: true +value: "bar" From ec1764bb6b260df35f1c78974663f6e414608664 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Thu, 16 Jun 2016 19:01:18 +0200 Subject: [PATCH 08/12] Add prefix and namespaceURI if declared --- .../VarDumper/Caster/XmlReaderCaster.php | 10 +++++++ .../Tests/Caster/XmlReaderCasterTest.php | 28 +++++++++++++++++++ .../VarDumper/Tests/Fixtures/xml_reader.xml | 3 ++ 3 files changed, 41 insertions(+) diff --git a/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php b/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php index 3e73b43c36cf4..2f32689aff9d7 100644 --- a/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php @@ -80,6 +80,11 @@ public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $ ), ); + if ('' !== $reader->prefix) { + $infos['prefix'] = $reader->prefix; + $infos['namespaceURI'] = $reader->namespaceURI; + } + if ($reader->hasValue && (\XmlReader::TEXT === $reader->nodeType || \XmlReader::ATTRIBUTE === $reader->nodeType)) { $infos['value'] = $reader->value; @@ -115,6 +120,11 @@ public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $ } } + if ('' !== $reader->prefix) { + $cut['prefix'] = $reader->prefix; + $cut['namespaceURI'] = $reader->namespaceURI; + } + $stub->cut += count($infos) - count($cut); return $cut; diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php index 883afba923195..c65645fa767ab 100644 --- a/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php +++ b/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php @@ -262,6 +262,34 @@ public function testAttributeNode() $this->assertDumpEquals($dump, $this->reader); } + public function testPrefixedElements() + { + $dump = <<<'DUMP' +XMLReader { + +localName: "baz" + +depth: 2 + +attributeCount: 0 + +hasAttributes: false + +hasValue: false + +isDefault: false + +isEmptyElement: false + +nodeType: ELEMENT + parserProperties: array:4 [ + "LOADDTD" => false + "DEFAULTATTRS" => false + "VALIDATE" => false + "SUBST_ENTITIES" => false + ] + +prefix: "baz" + +namespaceURI: "http://symfony.com" +} +DUMP; + + while ($this->reader->read() && $this->reader->localName !== 'baz'); + + $this->assertDumpEquals($dump, $this->reader); + } + public function tearDown() { $this->reader->close(); diff --git a/src/Symfony/Component/VarDumper/Tests/Fixtures/xml_reader.xml b/src/Symfony/Component/VarDumper/Tests/Fixtures/xml_reader.xml index 00ca046fca4b8..740c399fc4437 100644 --- a/src/Symfony/Component/VarDumper/Tests/Fixtures/xml_reader.xml +++ b/src/Symfony/Component/VarDumper/Tests/Fixtures/xml_reader.xml @@ -4,4 +4,7 @@ With text + + + From f9f12fee3f8739e4fbde3aa50405cb18482430f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Thu, 16 Jun 2016 21:27:19 +0200 Subject: [PATCH 09/12] Add special case for NONE node types --- .../VarDumper/Caster/XmlReaderCaster.php | 22 +++-- .../Tests/Caster/XmlReaderCasterTest.php | 95 +++++++++++-------- 2 files changed, 71 insertions(+), 46 deletions(-) diff --git a/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php b/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php index 2f32689aff9d7..9266d69fc7238 100644 --- a/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php @@ -60,6 +60,20 @@ public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $ $nodeType = new ConstStub(self::$nodeTypes[$reader->nodeType], $reader->nodeType); $infos = array( + 'nodeType' => $nodeType, + Caster::PREFIX_VIRTUAL.'parserProperties' => array( + 'LOADDTD' => $reader->getParserProperty(\XmlReader::LOADDTD), + 'DEFAULTATTRS' => $reader->getParserProperty(\XmlReader::DEFAULTATTRS), + 'VALIDATE' => $reader->getParserProperty(\XmlReader::VALIDATE), + 'SUBST_ENTITIES' => $reader->getParserProperty(\XmlReader::SUBST_ENTITIES), + ), + ); + + if (\XmlReader::NONE === $reader->nodeType) { + return $infos; + } + + $infos = $infos + array( 'localName' => $reader->localName, 'depth' => $reader->depth, @@ -70,14 +84,6 @@ public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $ 'hasValue' => $reader->hasValue, 'isDefault' => $reader->isDefault, 'isEmptyElement' => $reader->isEmptyElement, - 'nodeType' => $nodeType, - - Caster::PREFIX_VIRTUAL.'parserProperties' => array( - 'LOADDTD' => $reader->getParserProperty(\XmlReader::LOADDTD), - 'DEFAULTATTRS' => $reader->getParserProperty(\XmlReader::DEFAULTATTRS), - 'VALIDATE' => $reader->getParserProperty(\XmlReader::VALIDATE), - 'SUBST_ENTITIES' => $reader->getParserProperty(\XmlReader::SUBST_ENTITIES), - ), ); if ('' !== $reader->prefix) { diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php index c65645fa767ab..7455d922b99f8 100644 --- a/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php +++ b/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php @@ -33,13 +33,6 @@ public function testSimpleElementNode() { $dump = <<<'DUMP' XMLReader { - +localName: "foo" - +depth: 0 - +attributeCount: 0 - +hasAttributes: false - +hasValue: false - +isDefault: false - +isEmptyElement: false +nodeType: ELEMENT parserProperties: array:4 [ "LOADDTD" => false @@ -47,6 +40,13 @@ public function testSimpleElementNode() "VALIDATE" => false "SUBST_ENTITIES" => false ] + +localName: "foo" + +depth: 0 + +attributeCount: 0 + +hasAttributes: false + +hasValue: false + +isDefault: false + +isEmptyElement: false } DUMP; @@ -59,13 +59,6 @@ public function testNestedElement() { $dump = <<<'DUMP' XMLReader { - +localName: "bar" - +depth: 1 - +attributeCount: 0 - +hasAttributes: false - +hasValue: false - +isDefault: false - +isEmptyElement: false +nodeType: ELEMENT parserProperties: array:4 [ "LOADDTD" => false @@ -73,6 +66,13 @@ public function testNestedElement() "VALIDATE" => false "SUBST_ENTITIES" => false ] + +localName: "bar" + +depth: 1 + +attributeCount: 0 + +hasAttributes: false + +hasValue: false + +isDefault: false + +isEmptyElement: false } DUMP; @@ -86,13 +86,6 @@ public function testEmptyElement() { $dump = <<<'DUMP' XMLReader { - +localName: "bar" - +depth: 1 - +attributeCount: 0 - +hasAttributes: false - +hasValue: false - +isDefault: false - +isEmptyElement: true +nodeType: ELEMENT parserProperties: array:4 [ "LOADDTD" => false @@ -100,6 +93,13 @@ public function testEmptyElement() "VALIDATE" => false "SUBST_ENTITIES" => false ] + +localName: "bar" + +depth: 1 + +attributeCount: 0 + +hasAttributes: false + +hasValue: false + +isDefault: false + +isEmptyElement: true } DUMP; @@ -115,13 +115,6 @@ public function testElementWithAttributes() { $dump = <<<'DUMP' XMLReader { - +localName: "bar" - +depth: 1 - +attributeCount: 2 - +hasAttributes: true - +hasValue: false - +isDefault: false - +isEmptyElement: false +nodeType: ELEMENT parserProperties: array:4 [ "LOADDTD" => false @@ -129,6 +122,13 @@ public function testElementWithAttributes() "VALIDATE" => false "SUBST_ENTITIES" => false ] + +localName: "bar" + +depth: 1 + +attributeCount: 2 + +hasAttributes: true + +hasValue: false + +isDefault: false + +isEmptyElement: false attributes: array:2 [ 0 => "bar" 1 => "fubar" @@ -150,12 +150,6 @@ public function testTextElement() { $dump = <<<'DUMP' XMLReader { - +depth: 2 - +attributeCount: 0 - +hasAttributes: false - +hasValue: true - +isDefault: false - +isEmptyElement: false +nodeType: TEXT parserProperties: array:4 [ "LOADDTD" => false @@ -163,6 +157,12 @@ public function testTextElement() "VALIDATE" => false "SUBST_ENTITIES" => false ] + +depth: 2 + +attributeCount: 0 + +hasAttributes: false + +hasValue: true + +isDefault: false + +isEmptyElement: false +value: "With text" …1 } @@ -266,6 +266,13 @@ public function testPrefixedElements() { $dump = <<<'DUMP' XMLReader { + +nodeType: ELEMENT + parserProperties: array:4 [ + "LOADDTD" => false + "DEFAULTATTRS" => false + "VALIDATE" => false + "SUBST_ENTITIES" => false + ] +localName: "baz" +depth: 2 +attributeCount: 0 @@ -273,19 +280,31 @@ public function testPrefixedElements() +hasValue: false +isDefault: false +isEmptyElement: false - +nodeType: ELEMENT + +prefix: "baz" + +namespaceURI: "http://symfony.com" +} +DUMP; + + while ($this->reader->read() && $this->reader->localName !== 'baz'); + + $this->assertDumpEquals($dump, $this->reader); + } + + public function testNone() + { + $dump = <<<'DUMP' +XMLReader { + +nodeType: NONE parserProperties: array:4 [ "LOADDTD" => false "DEFAULTATTRS" => false "VALIDATE" => false "SUBST_ENTITIES" => false ] - +prefix: "baz" - +namespaceURI: "http://symfony.com" } DUMP; - while ($this->reader->read() && $this->reader->localName !== 'baz'); + while ($this->reader->read()); $this->assertDumpEquals($dump, $this->reader); } From 9d875169feac635e2bac0d526c2db66095e8ae2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Fri, 17 Jun 2016 11:46:46 +0200 Subject: [PATCH 10/12] Use an Enum Stub for parser properties --- .../VarDumper/Caster/XmlReaderCaster.php | 15 ++- .../Tests/Caster/XmlReaderCasterTest.php | 120 +++++++++--------- 2 files changed, 68 insertions(+), 67 deletions(-) diff --git a/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php b/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php index 9266d69fc7238..7341e9444b84c 100644 --- a/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php @@ -58,15 +58,16 @@ class XmlReaderCaster public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $isNested) { $nodeType = new ConstStub(self::$nodeTypes[$reader->nodeType], $reader->nodeType); + $parserProperties = new EnumStub(array( + 'LOADDTD' => $reader->getParserProperty(\XmlReader::LOADDTD), + 'DEFAULTATTRS' => $reader->getParserProperty(\XmlReader::DEFAULTATTRS), + 'VALIDATE' => $reader->getParserProperty(\XmlReader::VALIDATE), + 'SUBST_ENTITIES' => $reader->getParserProperty(\XmlReader::SUBST_ENTITIES), + )); $infos = array( 'nodeType' => $nodeType, - Caster::PREFIX_VIRTUAL.'parserProperties' => array( - 'LOADDTD' => $reader->getParserProperty(\XmlReader::LOADDTD), - 'DEFAULTATTRS' => $reader->getParserProperty(\XmlReader::DEFAULTATTRS), - 'VALIDATE' => $reader->getParserProperty(\XmlReader::VALIDATE), - 'SUBST_ENTITIES' => $reader->getParserProperty(\XmlReader::SUBST_ENTITIES), - ), + Caster::PREFIX_VIRTUAL.'parserProperties' => $parserProperties, ); if (\XmlReader::NONE === $reader->nodeType) { @@ -111,7 +112,7 @@ public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $ 'nodeType' => $nodeType, 'depth' => $reader->depth, - Caster::PREFIX_VIRTUAL.'parserProperties' => $infos[Caster::PREFIX_VIRTUAL.'parserProperties'], + Caster::PREFIX_VIRTUAL.'parserProperties' => $parserProperties, ); if ('#text' !== $reader->localName) { diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php index 7455d922b99f8..a727457cc3c22 100644 --- a/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php +++ b/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php @@ -34,12 +34,12 @@ public function testSimpleElementNode() $dump = <<<'DUMP' XMLReader { +nodeType: ELEMENT - parserProperties: array:4 [ - "LOADDTD" => false - "DEFAULTATTRS" => false - "VALIDATE" => false - "SUBST_ENTITIES" => false - ] + parserProperties: { + LOADDTD: false + DEFAULTATTRS: false + VALIDATE: false + SUBST_ENTITIES: false + } +localName: "foo" +depth: 0 +attributeCount: 0 @@ -60,12 +60,12 @@ public function testNestedElement() $dump = <<<'DUMP' XMLReader { +nodeType: ELEMENT - parserProperties: array:4 [ - "LOADDTD" => false - "DEFAULTATTRS" => false - "VALIDATE" => false - "SUBST_ENTITIES" => false - ] + parserProperties: { + LOADDTD: false + DEFAULTATTRS: false + VALIDATE: false + SUBST_ENTITIES: false + } +localName: "bar" +depth: 1 +attributeCount: 0 @@ -87,12 +87,12 @@ public function testEmptyElement() $dump = <<<'DUMP' XMLReader { +nodeType: ELEMENT - parserProperties: array:4 [ - "LOADDTD" => false - "DEFAULTATTRS" => false - "VALIDATE" => false - "SUBST_ENTITIES" => false - ] + parserProperties: { + LOADDTD: false + DEFAULTATTRS: false + VALIDATE: false + SUBST_ENTITIES: false + } +localName: "bar" +depth: 1 +attributeCount: 0 @@ -116,12 +116,12 @@ public function testElementWithAttributes() $dump = <<<'DUMP' XMLReader { +nodeType: ELEMENT - parserProperties: array:4 [ - "LOADDTD" => false - "DEFAULTATTRS" => false - "VALIDATE" => false - "SUBST_ENTITIES" => false - ] + parserProperties: { + LOADDTD: false + DEFAULTATTRS: false + VALIDATE: false + SUBST_ENTITIES: false + } +localName: "bar" +depth: 1 +attributeCount: 2 @@ -151,12 +151,12 @@ public function testTextElement() $dump = <<<'DUMP' XMLReader { +nodeType: TEXT - parserProperties: array:4 [ - "LOADDTD" => false - "DEFAULTATTRS" => false - "VALIDATE" => false - "SUBST_ENTITIES" => false - ] + parserProperties: { + LOADDTD: false + DEFAULTATTRS: false + VALIDATE: false + SUBST_ENTITIES: false + } +depth: 2 +attributeCount: 0 +hasAttributes: false @@ -181,12 +181,12 @@ public function testFilteredTextElement($nodeType, $nodeTypeName, $depth) XMLReader { +nodeType: $nodeTypeName +depth: $depth - parserProperties: array:4 [ - "LOADDTD" => false - "DEFAULTATTRS" => false - "VALIDATE" => false - "SUBST_ENTITIES" => false - ] + parserProperties: { + LOADDTD: false + DEFAULTATTRS: false + VALIDATE: false + SUBST_ENTITIES: false + } …6 } @@ -211,12 +211,12 @@ public function testFilteredElement($localName, $nodeType, $nodeTypeName, $expan XMLReader { +nodeType: $nodeTypeName +depth: $depth - parserProperties: array:4 [ - "LOADDTD" => false - "DEFAULTATTRS" => false - "VALIDATE" => false - "SUBST_ENTITIES" => false - ] + parserProperties: { + LOADDTD: false + DEFAULTATTRS: false + VALIDATE: false + SUBST_ENTITIES: false + } +localName: "$localName" …5 } @@ -241,12 +241,12 @@ public function testAttributeNode() XMLReader { +nodeType: ATTRIBUTE +depth: 2 - parserProperties: array:4 [ - "LOADDTD" => false - "DEFAULTATTRS" => false - "VALIDATE" => false - "SUBST_ENTITIES" => false - ] + parserProperties: { + LOADDTD: false + DEFAULTATTRS: false + VALIDATE: false + SUBST_ENTITIES: false + } +localName: "foo" +hasValue: true +value: "bar" @@ -267,12 +267,12 @@ public function testPrefixedElements() $dump = <<<'DUMP' XMLReader { +nodeType: ELEMENT - parserProperties: array:4 [ - "LOADDTD" => false - "DEFAULTATTRS" => false - "VALIDATE" => false - "SUBST_ENTITIES" => false - ] + parserProperties: { + LOADDTD: false + DEFAULTATTRS: false + VALIDATE: false + SUBST_ENTITIES: false + } +localName: "baz" +depth: 2 +attributeCount: 0 @@ -295,12 +295,12 @@ public function testNone() $dump = <<<'DUMP' XMLReader { +nodeType: NONE - parserProperties: array:4 [ - "LOADDTD" => false - "DEFAULTATTRS" => false - "VALIDATE" => false - "SUBST_ENTITIES" => false - ] + parserProperties: { + LOADDTD: false + DEFAULTATTRS: false + VALIDATE: false + SUBST_ENTITIES: false + } } DUMP; From 9049e45982088f1755682c5edb2f50acb81ce2a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Fri, 17 Jun 2016 13:52:42 +0200 Subject: [PATCH 11/12] Move the parserProperties at the end of list --- .../VarDumper/Caster/XmlReaderCaster.php | 19 ++-- .../Tests/Caster/XmlReaderCasterTest.php | 90 +++++++++---------- 2 files changed, 55 insertions(+), 54 deletions(-) diff --git a/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php b/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php index 7341e9444b84c..960acb25c38e2 100644 --- a/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php @@ -65,17 +65,16 @@ public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $ 'SUBST_ENTITIES' => $reader->getParserProperty(\XmlReader::SUBST_ENTITIES), )); - $infos = array( - 'nodeType' => $nodeType, - Caster::PREFIX_VIRTUAL.'parserProperties' => $parserProperties, - ); - if (\XmlReader::NONE === $reader->nodeType) { - return $infos; + return array( + 'nodeType' => $nodeType, + Caster::PREFIX_VIRTUAL.'parserProperties' => $parserProperties, + ); } - $infos = $infos + array( + $infos = array( 'localName' => $reader->localName, + 'nodeType' => $nodeType, 'depth' => $reader->depth, @@ -107,12 +106,12 @@ public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $ } } + $infos[Caster::PREFIX_VIRTUAL.'parserProperties'] = $parserProperties; + if (isset(static::$filteredTypes[$reader->nodeType])) { $cut = array( 'nodeType' => $nodeType, 'depth' => $reader->depth, - - Caster::PREFIX_VIRTUAL.'parserProperties' => $parserProperties, ); if ('#text' !== $reader->localName) { @@ -132,6 +131,8 @@ public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $ $cut['namespaceURI'] = $reader->namespaceURI; } + $cut[Caster::PREFIX_VIRTUAL.'parserProperties'] = $parserProperties; + $stub->cut += count($infos) - count($cut); return $cut; diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php index a727457cc3c22..34a04f18c0ef2 100644 --- a/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php +++ b/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php @@ -33,20 +33,20 @@ public function testSimpleElementNode() { $dump = <<<'DUMP' XMLReader { - +nodeType: ELEMENT - parserProperties: { - LOADDTD: false - DEFAULTATTRS: false - VALIDATE: false - SUBST_ENTITIES: false - } +localName: "foo" + +nodeType: ELEMENT +depth: 0 +attributeCount: 0 +hasAttributes: false +hasValue: false +isDefault: false +isEmptyElement: false + parserProperties: { + LOADDTD: false + DEFAULTATTRS: false + VALIDATE: false + SUBST_ENTITIES: false + } } DUMP; @@ -59,20 +59,20 @@ public function testNestedElement() { $dump = <<<'DUMP' XMLReader { - +nodeType: ELEMENT - parserProperties: { - LOADDTD: false - DEFAULTATTRS: false - VALIDATE: false - SUBST_ENTITIES: false - } +localName: "bar" + +nodeType: ELEMENT +depth: 1 +attributeCount: 0 +hasAttributes: false +hasValue: false +isDefault: false +isEmptyElement: false + parserProperties: { + LOADDTD: false + DEFAULTATTRS: false + VALIDATE: false + SUBST_ENTITIES: false + } } DUMP; @@ -86,20 +86,20 @@ public function testEmptyElement() { $dump = <<<'DUMP' XMLReader { - +nodeType: ELEMENT - parserProperties: { - LOADDTD: false - DEFAULTATTRS: false - VALIDATE: false - SUBST_ENTITIES: false - } +localName: "bar" + +nodeType: ELEMENT +depth: 1 +attributeCount: 0 +hasAttributes: false +hasValue: false +isDefault: false +isEmptyElement: true + parserProperties: { + LOADDTD: false + DEFAULTATTRS: false + VALIDATE: false + SUBST_ENTITIES: false + } } DUMP; @@ -115,14 +115,8 @@ public function testElementWithAttributes() { $dump = <<<'DUMP' XMLReader { - +nodeType: ELEMENT - parserProperties: { - LOADDTD: false - DEFAULTATTRS: false - VALIDATE: false - SUBST_ENTITIES: false - } +localName: "bar" + +nodeType: ELEMENT +depth: 1 +attributeCount: 2 +hasAttributes: true @@ -133,6 +127,12 @@ public function testElementWithAttributes() 0 => "bar" 1 => "fubar" ] + parserProperties: { + LOADDTD: false + DEFAULTATTRS: false + VALIDATE: false + SUBST_ENTITIES: false + } } DUMP; @@ -151,12 +151,6 @@ public function testTextElement() $dump = <<<'DUMP' XMLReader { +nodeType: TEXT - parserProperties: { - LOADDTD: false - DEFAULTATTRS: false - VALIDATE: false - SUBST_ENTITIES: false - } +depth: 2 +attributeCount: 0 +hasAttributes: false @@ -164,6 +158,12 @@ public function testTextElement() +isDefault: false +isEmptyElement: false +value: "With text" + parserProperties: { + LOADDTD: false + DEFAULTATTRS: false + VALIDATE: false + SUBST_ENTITIES: false + } …1 } @@ -211,13 +211,13 @@ public function testFilteredElement($localName, $nodeType, $nodeTypeName, $expan XMLReader { +nodeType: $nodeTypeName +depth: $depth + +localName: "$localName" parserProperties: { LOADDTD: false DEFAULTATTRS: false VALIDATE: false SUBST_ENTITIES: false } - +localName: "$localName" …5 } @@ -241,15 +241,15 @@ public function testAttributeNode() XMLReader { +nodeType: ATTRIBUTE +depth: 2 + +localName: "foo" + +hasValue: true + +value: "bar" parserProperties: { LOADDTD: false DEFAULTATTRS: false VALIDATE: false SUBST_ENTITIES: false } - +localName: "foo" - +hasValue: true - +value: "bar" …4 } @@ -266,14 +266,8 @@ public function testPrefixedElements() { $dump = <<<'DUMP' XMLReader { - +nodeType: ELEMENT - parserProperties: { - LOADDTD: false - DEFAULTATTRS: false - VALIDATE: false - SUBST_ENTITIES: false - } +localName: "baz" + +nodeType: ELEMENT +depth: 2 +attributeCount: 0 +hasAttributes: false @@ -282,6 +276,12 @@ public function testPrefixedElements() +isEmptyElement: false +prefix: "baz" +namespaceURI: "http://symfony.com" + parserProperties: { + LOADDTD: false + DEFAULTATTRS: false + VALIDATE: false + SUBST_ENTITIES: false + } } DUMP; From c5222fee33f0fd0b9ce0631d74ea9f13670d6cfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Fri, 17 Jun 2016 21:05:14 +0200 Subject: [PATCH 12/12] Refactor XmlReader caster Split main method into several sub methods when handling "special" cases such as ATTRIBUTE, NONE or filtered elements --- .../VarDumper/Caster/XmlReaderCaster.php | 81 +++++++++++-------- .../Tests/Caster/XmlReaderCasterTest.php | 16 ++-- 2 files changed, 55 insertions(+), 42 deletions(-) diff --git a/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php b/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php index 960acb25c38e2..117dcf5394d3d 100644 --- a/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php @@ -41,7 +41,6 @@ class XmlReaderCaster ); private static $filteredTypes = array( - \XmlReader::ATTRIBUTE => true, \XmlReader::ENTITY_REF => true, \XmlReader::ENTITY => true, \XmlReader::PI => true, @@ -66,21 +65,19 @@ public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $ )); if (\XmlReader::NONE === $reader->nodeType) { - return array( - 'nodeType' => $nodeType, - Caster::PREFIX_VIRTUAL.'parserProperties' => $parserProperties, - ); + return $a + self::castXmlNone($nodeType, $parserProperties); + } + + if (\XmlReader::ATTRIBUTE === $reader->nodeType) { + return $a + self::castAttribute($reader, $nodeType, $parserProperties); } $infos = array( 'localName' => $reader->localName, 'nodeType' => $nodeType, - 'depth' => $reader->depth, - 'attributeCount' => $reader->attributeCount, 'hasAttributes' => $reader->hasAttributes, - 'hasValue' => $reader->hasValue, 'isDefault' => $reader->isDefault, 'isEmptyElement' => $reader->isEmptyElement, @@ -91,11 +88,8 @@ public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $ $infos['namespaceURI'] = $reader->namespaceURI; } - if ($reader->hasValue && (\XmlReader::TEXT === $reader->nodeType || \XmlReader::ATTRIBUTE === $reader->nodeType)) { + if ($reader->hasValue && \XmlReader::TEXT === $reader->nodeType) { $infos['value'] = $reader->value; - - unset($infos['localName']); - $stub->cut += 1; } if ($reader->hasAttributes) { @@ -106,38 +100,57 @@ public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $ } } + if (isset(self::$filteredTypes[$reader->nodeType])) { + $infos = self::castFilteredElement($reader, $infos, $stub, $nodeType); + } + $infos[Caster::PREFIX_VIRTUAL.'parserProperties'] = $parserProperties; - if (isset(static::$filteredTypes[$reader->nodeType])) { - $cut = array( - 'nodeType' => $nodeType, - 'depth' => $reader->depth, - ); + return $a + $infos; + } - if ('#text' !== $reader->localName) { - $cut['localName'] = $reader->localName; - } + private static function castXmlNone(ConstStub $type, EnumStub $properties) + { + return array( + 'nodeType' => $type, + Caster::PREFIX_VIRTUAL.'parserProperties' => $properties, + ); + } - if (\XmlReader::ATTRIBUTE === $reader->nodeType) { - $cut['hasValue'] = $reader->hasValue; + private static function castFilteredElement(\XmlReader $reader, array $infos, Stub $stub, ConstStub $type) + { + $cut = array( + 'localName' => $reader->localName, + 'nodeType' => $type, + 'depth' => $reader->depth, + ); - if ($reader->hasValue) { - $cut['value'] = $reader->value; - } - } + $stub->cut += count($infos) - count($cut); - if ('' !== $reader->prefix) { - $cut['prefix'] = $reader->prefix; - $cut['namespaceURI'] = $reader->namespaceURI; - } + return $cut; + } - $cut[Caster::PREFIX_VIRTUAL.'parserProperties'] = $parserProperties; + private static function castAttribute(\XmlReader $reader, ConstStub $type, EnumStub $properties) + { + $infos = array( + 'localName' => $reader->localName, + 'nodeType' => $type, + 'depth' => $reader->depth, + 'isDefault' => $reader->isDefault, + 'hasValue' => $reader->hasValue, + ); - $stub->cut += count($infos) - count($cut); + if ($reader->hasValue) { + $infos['value'] = $reader->value; + } - return $cut; + if ('' !== $reader->prefix) { + $infos['prefix'] = $reader->prefix; + $infos['namespaceURI'] = $reader->namespaceURI; } - return $a + $infos; + $infos[Caster::PREFIX_VIRTUAL.'parserProperties'] = $properties; + + return $infos; } } diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php index 34a04f18c0ef2..597878436d85d 100644 --- a/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php +++ b/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php @@ -150,6 +150,7 @@ public function testTextElement() { $dump = <<<'DUMP' XMLReader { + +localName: "#text" +nodeType: TEXT +depth: 2 +attributeCount: 0 @@ -164,7 +165,6 @@ public function testTextElement() VALIDATE: false SUBST_ENTITIES: false } - …1 } DUMP; @@ -175,10 +175,11 @@ public function testTextElement() } /** @dataProvider textFilteredProvider */ - public function testFilteredTextElement($nodeType, $nodeTypeName, $depth) + public function testFilteredTextElement($nodeType, $nodeTypeName, $depth, $localName) { $dump = << array(\XmlReader::SIGNIFICANT_WHITESPACE, 'SIGNIFICANT_WHITESPACE', 1), + 'Significant Whiltespace element' => array(\XmlReader::SIGNIFICANT_WHITESPACE, 'SIGNIFICANT_WHITESPACE', 1, '#text'), ); } @@ -209,9 +210,9 @@ public function testFilteredElement($localName, $nodeType, $nodeTypeName, $expan { $dump = <<reader->close(); } } -