Skip to content

[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

Merged
merged 1 commit into from
Jun 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Symfony/Component/VarDumper/Caster/Caster.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,14 @@ public static function castObject($obj, \ReflectionClass $reflector)
* @param array $a The array containing the properties to filter.
* @param int $filter A bit field of Caster::EXCLUDE_* constants specifying which properties to filter out.
* @param string[] $listedProperties List of properties to exclude when Caster::EXCLUDE_VERBOSE is set, and to preserve when Caster::EXCLUDE_NOT_IMPORTANT is set.
* @param int &$count Set to the number of removed properties.
*
* @return array The filtered array
*/
public static function filter(array $a, $filter, array $listedProperties = array())
public static function filter(array $a, $filter, array $listedProperties = array(), &$count = 0)
{
$count = 0;

foreach ($a as $k => $v) {
$type = self::EXCLUDE_STRICT & $filter;

Expand Down Expand Up @@ -108,6 +111,7 @@ public static function filter(array $a, $filter, array $listedProperties = array

if ((self::EXCLUDE_STRICT & $filter) ? $type === $filter : $type) {
unset($a[$k]);
++$count;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/VarDumper/Caster/StubCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public static function castEnum(EnumStub $c, array $a, Stub $stub, $isNested)
$stub->class = '';
$stub->handle = 0;
$stub->value = null;
$stub->cut = $c->cut;

$a = array();

Expand Down
77 changes: 77 additions & 0 deletions src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php
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)',
Copy link
Member Author

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 but PI is really too abstract IMHO)

\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,
Copy link
Member Author

Choose a reason for hiding this comment

The 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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasValue and hasAttributes are always filtered because value and attributeCount already convey the required info

$stub->cut += $count + 2;

return $a + $info;
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
247 changes: 247 additions & 0 deletions src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php
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 src/Symfony/Component/VarDumper/Tests/Fixtures/xml_reader.xml
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>