Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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

Closed
wants to merge 12 commits into from
Next Next commit
Add support for XmlReader objets
  • Loading branch information
Taluu committed Jun 21, 2016
commit 51cfb477e961d7d0a62464fcdd132cdff7cc2a7d
78 changes: 78 additions & 0 deletions 78 src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php
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)
Copy link
Member

@nicolas-grekas nicolas-grekas Jun 21, 2016

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.

Copy link
Contributor Author

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.

{
$nodeType = new ConstStub(self::$nodeTypes[$reader->nodeType], $reader->nodeType);

$infos = array(
'localName' => $reader->localName,

Copy link
Member

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

The 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
we should even dump results of getParserProperty IMHO

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 !== '', because even if it has a value (such as baz), empty($reader->prefix) returns true...)

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()) {
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The 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"?

Copy link
Contributor Author

@Taluu Taluu Jun 7, 2016

Choose a reason for hiding this comment

The 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;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.