10000 feature #19151 [VarDumper] Add support for XmlReader objects (Taluu) · symfony/symfony@9af416d · GitHub
[go: up one dir, main page]

Skip to content

Commit 9af416d

Browse files
committed
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
2 parents 43f9514 + 3779ee4 commit 9af416d

File tree

6 files changed

+342
-1
lines changed

6 files changed

+342
-1
lines changed

src/Symfony/Component/VarDumper/Caster/Caster.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,14 @@ public static function castObject($obj, \ReflectionClass $reflector)
7373
* @param array $a The array containing the properties to filter.
7474
* @param int $filter A bit field of Caster::EXCLUDE_* constants specifying which properties to filter out.
7575
* @param string[] $listedProperties List of properties to exclude when Caster::EXCLUDE_VERBOSE is set, and to preserve when Caster::EXCLUDE_NOT_IMPORTANT is set.
76+
* @param int &$count Set to the number of removed properties.
7677
*
7778
* @return array The filtered array
7879
*/
79-
public static function filter(array $a, $filter, array $listedProperties = array())
80+
public static function filter(array $a, $filter, array $listedProperties = array(), &$count = 0)
8081
{
82+
$count = 0;
83+
8184
foreach ($a as $k => $v) {
8285
$type = self::EXCLUDE_STRICT & $filter;
8386

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

109112
if ((self::EXCLUDE_STRICT & $filter) ? $type === $filter : $type) {
110113
unset($a[$k]);
114+
++$count;
111115
}
112116
}
113117

src/Symfony/Component/VarDumper/Caster/StubCaster.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public static function castEnum(EnumStub $c, array $a, Stub $stub, $isNested)
5555
$stub->class = '';
5656
$stub->handle = 0;
5757
$stub->value = null;
58+
$stub->cut = $c->cut;
5859

5960
$a = array();
6061

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/*
3+
* This file is part of the Symfony package.
4+
*
5+
* (c) Fabien Potencier <fabien@symfony.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Symfony\Component\VarDumper\Caster;
12+
13+
use Symfony\Component\VarDumper\Cloner\Stub;
14+
15+
/**
16+
* Casts XmlReader class to array representation.
17+
*
18+
* @author Baptiste Clavié <clavie.b@gmail.com>
19+
*/
20+
class XmlReaderCaster
21+
{
22+
private static $nodeTypes = array(
23+
\XmlReader::NONE => 'NONE',
24+
\XmlReader::ELEMENT => 'ELEMENT',
25+
\XmlReader::ATTRIBUTE => 'ATTRIBUTE',
26+
\XmlReader::TEXT => 'TEXT',
27+
\XmlReader::CDATA => 'CDATA',
28+
\XmlReader::ENTITY_REF => 'ENTITY_REF',
29+
\XmlReader::ENTITY => 'ENTITY',
30+
\XmlReader::PI => 'PI (Processing Instruction)',
31+
\XmlReader::COMMENT => 'COMMENT',
32+
\XmlReader::DOC => 'DOC',
33+
\XmlReader::DOC_TYPE => 'DOC_TYPE',
34+
\XmlReader::DOC_FRAGMENT => 'DOC_FRAGMENT',
35+
\XmlReader::NOTATION => 'NOTATION',
36+
\XmlReader::WHITESPACE => 'WHITESPACE',
37+
\XmlReader::SIGNIFICANT_WHITESPACE => 'SIGNIFICANT_WHITESPACE',
38+
\XmlReader::END_ELEMENT => 'END_ELEMENT',
39+
\XmlReader::END_ENTITY => 'END_ENTITY',
40+
\XmlReader::XML_DECLARATION => 'XML_DECLARATION',
41+
);
42+
43+
public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $isNested)
44+
{
45+
$props = Caster::PREFIX_VIRTUAL.'parserProperties';
46+
$info = array(
47+
'localName' => $reader->localName,
48+
'prefix' => $reader->prefix,
49+
'nodeType' => new ConstStub(self::$nodeTypes[$reader->nodeType], $reader->nodeType),
50+
'depth' => $reader->depth,
51+
'isDefault' => $reader->isDefault,
52+
'isEmptyElement' => \XmlReader::NONE === $reader->nodeType ? null : $reader->isEmptyElement,
53+
'xmlLang' => $reader->xmlLang,
54+
'attributeCount' => $reader->attributeCount,
55+
'value' => $reader->value,
56+
'namespaceURI' => $reader->namespaceURI,
57+
'baseURI' => $reader->baseURI,
58+
$props => array(
59+
'LOADDTD' => $reader->getParserProperty(\XmlReader::LOADDTD),
60+
'DEFAULTATTRS' => $reader->getParserProperty(\XmlReader::DEFAULTATTRS),
61+
'VALIDATE' => $reader->getParserProperty(\XmlReader::VALIDATE),
62+
'SUBST_ENTITIES' => $reader->getParserProperty(\XmlReader::SUBST_ENTITIES),
63+
),
64+
);
65+
66+
if ($info[$props] = Caster::filter($info[$props], Caster::EXCLUDE_EMPTY, array(), $count)) {
67+
$info[$props] = new EnumStub($info[$props]);
68+
$info[$props]->cut = $count;
69+
}
70+
71+
$info = Caster::filter($info, Caster::EXCLUDE_EMPTY, array(), $count);
72+
// +2 because hasValue and hasAttributes are always filtered
73+
$stub->cut += $count + 2;
74+
75+
return $a + $info;
76+
}
77+
}

src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ abstract class AbstractCloner implements ClonerInterface
6767
'DOMProcessingInstruction' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castProcessingInstruction',
6868
'DOMXPath' => 'Symfony\Component\VarDumper\Caster\DOMCaster::castXPath',
6969

70+
'XmlReader' => 'Symfony\Component\VarDumper\Caster\XmlReaderCaster::castXmlReader',
71+
7072
'ErrorException' => 'Symfony\Component\VarDumper\Caster\ExceptionCaster::castErrorException',
7173
'Exception' => 'Symfony\Component\VarDumper\Caster\ExceptionCaster::castException',
7274
'Error' => 'Symfony\Component\VarDumper\Caster\ExceptionCaster::castError',
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Tests\Caster;
13+
14+
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
15+
16+
/**
17+
* @author Baptiste Clavié <clavie.b@gmail.com>
18+
*/
19+
class XmlReaderCasterTest extends \PHPUnit_Framework_TestCase
20+
{
21+
use VarDumperTestTrait;
22+
23+
/** @var \XmlReader */
24+
private $reader;
25+
26+
protected function setUp()
27+
{
28+
$this->reader = new \XmlReader();
29+
$this->reader->open(__DIR__.'/../Fixtures/xml_reader.xml');
30+
}
31+
32+
protected function tearDown()
33+
{
34+
$this->reader->close();
35+
}
36+
37+
public function testParserProperty()
38+
{
39+
$this->reader->setParserProperty(\XMLReader::SUBST_ENTITIES, true);
40+
41+
$expectedDump = <<<'EODUMP'
42+
XMLReader {
43+
+nodeType: NONE
44+
parserProperties: {
45+
SUBST_ENTITIES: true
46+
…3
47+
}
48+
…12
49+
}
50+
EODUMP;
51+
52+
$this->assertDumpMatchesFormat($expectedDump, $this->reader);
53+
}
54+
55+
/**
56+
* @dataProvider provideNodes
57+
*/
58+
public function testNodes($seek, $expectedDump)
59+
{
60+
while ($seek--) {
61+
$this->reader->read();
62+
}
63+
$this->assertDumpMatchesFormat($expectedDump, $this->reader);
64+
}
65+
66+
public function provideNodes()
67+
{
68+
return array(
69+
array(0, <<<'EODUMP'
70+
XMLReader {
71+
+nodeType: NONE
72+
…13
73+
}
74+
EODUMP
75+
),
76+
array(1, <<<'EODUMP'
77+
XMLReader {
78+
+localName: "foo"
79+
+nodeType: ELEMENT
80+
+baseURI: "%sxml_reader.xml"
81+
…11
82+
}
83+
EODUMP
84+
),
85+
array(2, <<<'EODUMP'
86+
XMLReader {
87+
+localName: "#text"
88+
+nodeType: SIGNIFICANT_WHITESPACE
89+
+depth: 1
90+
+value: """
91+
\n
92+
93+
"""
94+
+baseURI: "%sxml_reader.xml"
95+
…9
96+
}
97+
EODUMP
98+
),
99+
array(3, <<<'EODUMP'
100+
XMLReader {
101+
+localName: "bar"
102+
+nodeType: ELEMENT
103+
+depth: 1
104+
+baseURI: "%sxml_reader.xml"
105+
…10
106+
}
107+
EODUMP
108+
),
109+
array(4, <<<'EODUMP'
110+
XMLReader {
111+
+localName: "bar"
112+
+nodeType: END_ELEMENT
113+
+depth: 1
114+
+baseURI: "%sxml_reader.xml"
115+
…10
116+
}
117+
EODUMP
118+
),
119+
array(6, <<<'EODUMP'
120+
XMLReader {
121+
+localName: "bar"
122+
+nodeType: ELEMENT
123+
+depth: 1
124+
+isEmptyElement: true
125+
+baseURI: "%sxml_reader.xml"
126+
…9
127+
}
128+
EODUMP
129+
),
130+
array(9, <<<'EODUMP'
131+
XMLReader {
132+
+localName: "#text"
133+
+nodeType: TEXT
134+
+depth: 2
135+
+value: "With text"
136+
+baseURI: "%sxml_reader.xml"
137+
…9
138+
}
139+
EODUMP
140+
),
141+
array(12, <<<'EODUMP'
142+
XMLReader {
143+
+localName: "bar"
144+
+nodeType: ELEMENT
145+
+depth: 1
146+
+attributeCount: 2
147+
+baseURI: "%sxml_reader.xml"
148+
…9
149+
}
150+
EODUMP
151+
),
152+
array(13, <<<'EODUMP'
153+
XMLReader {
154+
+localName: "bar"
155+
+nodeType: END_ELEMENT
156+
+depth: 1
157+
+baseURI: "%sxml_reader.xml"
158+
…10
159+
}
160+
EODUMP
161+
),
162+
array(15, <<<'EODUMP'
163+
XMLReader {
164+
+localName: "bar"
165+
+nodeType: ELEMENT
166+
+depth: 1
167+
+attributeCount: 1
168+
+baseURI: "%sxml_reader.xml"
169+
…9
170+
}
171+
EODUMP
172+
),
173+
array(16, <<<'EODUMP'
174+
XMLReader {
175+
+localName: "#text"
176+
+nodeType: SIGNIFICANT_WHITESPACE
177+
+depth: 2
178+
+value: """
179+
\n
180+
181+
"""
182+
+baseURI: "%sxml_reader.xml"
183+
…9
184+
}
185+
EODUMP
186+
),
187+
array(17, <<<'EODUMP'
188+
XMLReader {
189+
+localName: "baz"
190+
+prefix: "baz"
191+
+nodeType: ELEMENT
192+
+depth: 2
193+
+namespaceURI: "http://symfony.com"
194+
+baseURI: "%sxml_reader.xml"
195+
…8
196+
}
197+
EODUMP
198+
),
199+
array(18, <<<'EODUMP'
200+
XMLReader {
201+
+localName: "baz"
202+
+prefix: "baz"
203+
+nodeType: END_ELEMENT
204+
+depth: 2
205+
+namespaceURI: "http://symfony.com"
206+
+baseURI: "%sxml_reader.xml"
207+
…8
208+
}
209+
EODUMP
210+
),
211+
array(19, <<<'EODUMP'
212+
XMLReader {
213+
+localName: "#text"
214+
+nodeType: SIGNIFICANT_WHITESPACE
215+
+depth: 2
216+
+value: """
217+
\n
218+
219+
"""
220+
+baseURI: "%sxml_reader.xml"
221+
…9
222+
}
223+
EODUMP
224+
),
225+
array(21, <<<'EODUMP'
226+
XMLReader {
227+
+localName: "#text"
228+
+nodeType: SIGNIFICANT_WHITESPACE
229+
+depth: 1
230+
+value: "\n"
231+
+baseURI: "%sxml_reader.xml"
232+
…9
233+
}
234+
EODUMP
235+
),
236+
array(22, <<<'EODUMP'
237+
XMLReader {
238+
+localName: "foo"
239+
+nodeType: END_ELEMENT
240+
+baseURI: "%sxml_reader.xml"
241+
…11
242+
}
243+
EODUMP
244+
),
245+
);
246+
}
247+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<foo>
3+
<bar></bar>
4+
<bar />
5+
<bar>With text</bar>
6+
<bar foo="bar" baz="fubar"></bar>
7+
<bar xmlns:baz="http://symfony.com">
8+
<baz:baz></baz:baz>
9+
</bar>
10+
</foo>

0 commit comments

Comments
 (0)
0