Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #19151 [VarDumper] Add support for XmlReader objects (Taluu)
This PR was merged into the 3.2-dev branch. Discussion ---------- [VarDumper] Add support for XmlReader objects | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #18989 | License | MIT | Doc PR | - Commits ------- 3779ee4 [VarDumper] Add support for XmlReader objects
- Loading branch information
Showing
6 changed files
with
342 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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é <clavie.b@gmail.com> | ||
*/ | ||
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 (Processing Instruction)', | ||
\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) | ||
{ | ||
$props = Caster::PREFIX_VIRTUAL.'parserProperties'; | ||
$info = array( | ||
'localName' => $reader->localName, | ||
'prefix' => $reader->prefix, | ||
'nodeType' => new ConstStub(self::$nodeTypes[$reader->nodeType], $reader->nodeType), | ||
'depth' => $reader->depth, | ||
'isDefault' => $reader->isDefault, | ||
'isEmptyElement' => \XmlReader::NONE === $reader->nodeType ? null : $reader->isEmptyElement, | ||
'xmlLang' => $reader->xmlLang, | ||
'attributeCount' => $reader->attributeCount, | ||
'value' => $reader->value, | ||
'namespaceURI' => $reader->namespaceURI, | ||
'baseURI' => $reader->baseURI, | ||
$props => array( | ||
'LOADDTD' => $reader->getParserProperty(\XmlReader::LOADDTD), | ||
'DEFAULTATTRS' => $reader->getParserProperty(\XmlReader::DEFAULTATTRS), | ||
'VALIDATE' => $reader->getParserProperty(\XmlReader::VALIDATE), | ||
'SUBST_ENTITIES' => $reader->getParserProperty(\XmlReader::SUBST_ENTITIES), | ||
), | ||
); | ||
|
||
if ($info[$props] = Caster::filter($info[$props], Caster::EXCLUDE_EMPTY, array(), $count)) { | ||
$info[$props] = new EnumStub($info[$props]); | ||
$info[$props]->cut = $count; | ||
} | ||
|
||
$info = Caster::filter($info, Caster::EXCLUDE_EMPTY, array(), $count); | ||
// +2 because hasValue and hasAttributes are always filtered | ||
$stub->cut += $count + 2; | ||
|
||
return $a + $info; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
247 changes: 247 additions & 0 deletions
247
src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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é <clavie.b@gmail.com> | ||
*/ | ||
class XmlReaderCasterTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
use VarDumperTestTrait; | ||
|
||
/** @var \XmlReader */ | ||
private $reader; | ||
|
||
protected function setUp() | ||
{ | ||
$this->reader = new \XmlReader(); | ||
$this->reader->open(__DIR__.'/../Fixtures/xml_reader.xml'); | ||
} | ||
|
||
protected function tearDown() | ||
{ | ||
$this->reader->close(); | ||
} | ||
|
||
public function testParserProperty() | ||
{ | ||
$this->reader->setParserProperty(\XMLReader::SUBST_ENTITIES, true); | ||
|
||
$expectedDump = <<<'EODUMP' | ||
XMLReader { | ||
+nodeType: NONE | ||
parserProperties: { | ||
SUBST_ENTITIES: true | ||
…3 | ||
} | ||
…12 | ||
} | ||
EODUMP; | ||
|
||
$this->assertDumpMatchesFormat($expectedDump, $this->reader); | ||
} | ||
|
||
/** | ||
* @dataProvider provideNodes | ||
*/ | ||
public function testNodes($seek, $expectedDump) | ||
{ | ||
while ($seek--) { | ||
$this->reader->read(); | ||
} | ||
$this->assertDumpMatchesFormat($expectedDump, $this->reader); | ||
} | ||
|
||
public function provideNodes() | ||
{ | ||
return array( | ||
array(0, <<<'EODUMP' | ||
XMLReader { | ||
+nodeType: NONE | ||
…13 | ||
} | ||
EODUMP | ||
), | ||
array(1, <<<'EODUMP' | ||
XMLReader { | ||
+localName: "foo" | ||
+nodeType: ELEMENT | ||
+baseURI: "%sxml_reader.xml" | ||
…11 | ||
} | ||
EODUMP | ||
), | ||
array(2, <<<'EODUMP' | ||
XMLReader { | ||
+localName: "#text" | ||
+nodeType: SIGNIFICANT_WHITESPACE | ||
+depth: 1 | ||
+value: """ | ||
\n | ||
""" | ||
+baseURI: "%sxml_reader.xml" | ||
…9 | ||
} | ||
EODUMP | ||
), | ||
array(3, <<<'EODUMP' | ||
XMLReader { | ||
+localName: "bar" | ||
+nodeType: ELEMENT | ||
+depth: 1 | ||
+baseURI: "%sxml_reader.xml" | ||
…10 | ||
} | ||
EODUMP | ||
), | ||
array(4, <<<'EODUMP' | ||
XMLReader { | ||
+localName: "bar" | ||
+nodeType: END_ELEMENT | ||
+depth: 1 | ||
+baseURI: "%sxml_reader.xml" | ||
…10 | ||
} | ||
EODUMP | ||
), | ||
array(6, <<<'EODUMP' | ||
XMLReader { | ||
+localName: "bar" | ||
+nodeType: ELEMENT | ||
+depth: 1 | ||
+isEmptyElement: true | ||
+baseURI: "%sxml_reader.xml" | ||
…9 | ||
} | ||
EODUMP | ||
), | ||
array(9, <<<'EODUMP' | ||
XMLReader { | ||
+localName: "#text" | ||
+nodeType: TEXT | ||
+depth: 2 | ||
+value: "With text" | ||
+baseURI: "%sxml_reader.xml" | ||
…9 | ||
} | ||
EODUMP | ||
), | ||
array(12, <<<'EODUMP' | ||
XMLReader { | ||
+localName: "bar" | ||
+nodeType: ELEMENT | ||
+depth: 1 | ||
+attributeCount: 2 | ||
+baseURI: "%sxml_reader.xml" | ||
…9 | ||
} | ||
EODUMP | ||
), | ||
array(13, <<<'EODUMP' | ||
XMLReader { | ||
+localName: "bar" | ||
+nodeType: END_ELEMENT | ||
+depth: 1 | ||
+baseURI: "%sxml_reader.xml" | ||
…10 | ||
} | ||
EODUMP | ||
), | ||
array(15, <<<'EODUMP' | ||
XMLReader { | ||
+localName: "bar" | ||
+nodeType: ELEMENT | ||
+depth: 1 | ||
+attributeCount: 1 | ||
+baseURI: "%sxml_reader.xml" | ||
…9 | ||
} | ||
EODUMP | ||
), | ||
array(16, <<<'EODUMP' | ||
XMLReader { | ||
+localName: "#text" | ||
+nodeType: SIGNIFICANT_WHITESPACE | ||
+depth: 2 | ||
+value: """ | ||
\n | ||
""" | ||
+baseURI: "%sxml_reader.xml" | ||
…9 | ||
} | ||
EODUMP | ||
), | ||
array(17, <<<'EODUMP' | ||
XMLReader { | ||
+localName: "baz" | ||
+prefix: "baz" | ||
+nodeType: ELEMENT | ||
+depth: 2 | ||
+namespaceURI: "http://symfony.com" | ||
+baseURI: "%sxml_reader.xml" | ||
…8 | ||
} | ||
EODUMP | ||
), | ||
array(18, <<<'EODUMP' | ||
XMLReader { | ||
+localName: "baz" | ||
+prefix: "baz" | ||
+nodeType: END_ELEMENT | ||
+depth: 2 | ||
+namespaceURI: "http://symfony.com" | ||
+baseURI: "%sxml_reader.xml" | ||
…8 | ||
} | ||
EODUMP | ||
), | ||
array(19, <<<'EODUMP' | ||
XMLReader { | ||
+localName: "#text" | ||
+nodeType: SIGNIFICANT_WHITESPACE | ||
+depth: 2 | ||
+value: """ | ||
\n | ||
""" | ||
+baseURI: "%sxml_reader.xml" | ||
…9 | ||
} | ||
EODUMP | ||
), | ||
array(21, <<<'EODUMP' | ||
XMLReader { | ||
+localName: "#text" | ||
+nodeType: SIGNIFICANT_WHITESPACE | ||
+depth: 1 | ||
+value: "\n" | ||
+baseURI: "%sxml_reader.xml" | ||
…9 | ||
} | ||
EODUMP | ||
), | ||
array(22, <<<'EODUMP' | ||
XMLReader { | ||
+localName: "foo" | ||
+nodeType: END_ELEMENT | ||
+baseURI: "%sxml_reader.xml" | ||
…11 | ||
} | ||
EODUMP | ||
), | ||
); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Symfony/Component/VarDumper/Tests/Fixtures/xml_reader.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<foo> | ||
<bar></bar> | ||
<bar /> | ||
<bar>With text</bar> | ||
<bar foo="bar" baz="fubar"></bar> | ||
<bar xmlns:baz="http://symfony.com"> | ||
<baz:baz></baz:baz> | ||
</bar> | ||
</foo> |
9af416d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍