-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Dumper] Add support for XmlReader objets #18989
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 1 commit
51cfb47
5d83a71
71558e5
3250333
513e73b
5030ea5
f442810
ec1764b
f9f12fe
9d87516
9049e45
c5222fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?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', | ||
\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) | ||
{ | ||
$nodeType = new ConstStub(self::$nodeTypes[$reader->nodeType], $reader->nodeType); | ||
|
||
$infos = array( | ||
'localName' => $reader->localName, | ||
|
||
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. let's drop these blank lines |
||
'depth' => $reader->depth, | ||
|
||
'attributeCount' => $reader->attributeCount, | ||
'hasAttributes' => $reader->hasAttributes, | ||
|
||
'hasValue' => $reader->hasValue, | ||
'isDefault' => $reader->isDefault, | ||
'isEmptyElement' => $reader->isEmptyElement, | ||
'nodeType' => $nodeType, | ||
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. btw looking at the doc, there are more properties: http://php.net/manual/en/class.xmlreader.php 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. I added the prefix and the namespaceURI if the prefix is not empty (had to check And I also added the parser parameters, even though I am not really convinced. I also added it when the elements are ignored. |
||
); | ||
|
||
if ($reader->hasValue && (\XmlReader::TEXT === $reader->nodeType || \XmlReader::ATTRIBUTE === $reader->nodeType)) { | ||
$infos['value'] = $reader->value; | ||
|
||
unset($infos['localName']); | ||
$stub->cut += 1; | ||
} | ||
|
||
if ($reader->hasAttributes) { | ||
$infos[Caster::PREFIX_VIRTUAL . 'attributes'] = array(); | ||
|
||
while ($reader->moveToNextAttribute()) { | ||
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. this changes the state of the reader, which is a no-go. The casters are not allowed to produce side effects. 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. Yes, I had a different implementation before, but I can't have the attributes name (only indexes). If that is enough, then I guess I could revert back. But aren't the variables cloned or something like that when passed to the casters ? /cc @nicolas-grekas 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. This changes the state of the $reader object, is it possible to get the attributes doing any "move"? 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. Using something like this (Which is what I did before, but thought it was not really generous in information) : <?php
for ($i = 0; $c = $reader->attributeCount; $i < $c; ++$i) {
$infos[Caster::PREFIX_VIRTUAL . 'attributes'][] = $reader->getAttributeNo($i);
} But then we are losing the attribute name... |
||
$infos[Caster::PREFIX_VIRTUAL . 'attributes'][$reader->name] = $reader->value; | ||
} | ||
} | ||
|
||
return $a + $infos; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
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.
I would adopt a different code organization: let's start by building the full $infos array without any conditional statement, then filter based on the type. This should help reduce duplications, and ease counting
cut
value.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.
Can't do that, as NONE elements triggers fatal errors when trying to access the name or other properties. I thought of making some "special" cases, such as NONE and ATTRIBUTES, but then I could treat the filter afterwards and make a difference in the number of elements, as was done before.