diff --git a/src/Symfony/Component/VarDumper/Caster/Caster.php b/src/Symfony/Component/VarDumper/Caster/Caster.php index 23e72e87701e..28cdef5d4587 100644 --- a/src/Symfony/Component/VarDumper/Caster/Caster.php +++ b/src/Symfony/Component/VarDumper/Caster/Caster.php @@ -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; @@ -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; } } diff --git a/src/Symfony/Component/VarDumper/Caster/StubCaster.php b/src/Symfony/Component/VarDumper/Caster/StubCaster.php index ebad5ba9844e..3d8016eb985a 100644 --- a/src/Symfony/Component/VarDumper/Caster/StubCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/StubCaster.php @@ -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(); diff --git a/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php b/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php new file mode 100644 index 000000000000..df23cf2432b4 --- /dev/null +++ b/src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php @@ -0,0 +1,77 @@ + + * + * 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 (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; + } +} diff --git a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php index 99ca07f264d7..ea1495519547 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', 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 000000000000..a92ebe29aba2 --- /dev/null +++ b/src/Symfony/Component/VarDumper/Tests/Caster/XmlReaderCasterTest.php @@ -0,0 +1,247 @@ + + * + * 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; + + 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 + ), + ); + } +} 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 000000000000..740c399fc443 --- /dev/null +++ b/src/Symfony/Component/VarDumper/Tests/Fixtures/xml_reader.xml @@ -0,0 +1,10 @@ + + + + + With text + + + + +