-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[VarDumper] Add support for XmlReader objects #19151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. attributes are not dumped anymore (because we can't get their name, and also because arguments are not directly part of the state of the current iteration) |
||
'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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
$stub->cut += $count + 2; | ||
|
||
return $a + $info; | ||
} | ||
} |
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 | ||
), | ||
); | ||
} | ||
} |
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> |
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.
(Processing Instruction)
added for readability (the other items are readable enough butPI
is really too abstract IMHO)