-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
51cfb47
Add support for XmlReader objets
Taluu 5d83a71
Add XmlReaderCaster in the default casters
Taluu 71558e5
Do not touch XmlReader state while reading attributes
Taluu 3250333
Ignore (partially) some node types
Taluu 513e73b
Add some more info on ATTRIBUTES elements
Taluu 5030ea5
Add unit tests for XmlReader Caster
Taluu f442810
Add parser properties virtual property
Taluu ec1764b
Add prefix and namespaceURI if declared
Taluu f9f12fe
Add special case for NONE node types
Taluu 9d87516
Use an Enum Stub for parser properties
Taluu 9049e45
Move the parserProperties at the end of list
Taluu c5222fe
Refactor XmlReader caster
Taluu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
156 changes: 156 additions & 0 deletions
156
src/Symfony/Component/VarDumper/Caster/XmlReaderCaster.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
<?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', | ||
); | ||
|
||
private static $filteredTypes = array( | ||
\XmlReader::ENTITY_REF => true, | ||
\XmlReader::ENTITY => true, | ||
\XmlReader::PI => true, | ||
\XmlReader::DOC => true, | ||
\XmlReader::DOC_TYPE => true, | ||
\XmlReader::DOC_FRAGMENT => true, | ||
\XmlReader::NOTATION => true, | ||
\XmlReader::WHITESPACE => true, | ||
\XmlReader::SIGNIFICANT_WHITESPACE => true, | ||
\XmlReader::END_ELEMENT => true, | ||
\XmlReader::END_ENTITY => true, | ||
); | ||
|
||
public static function castXmlReader(\XmlReader $reader, array $a, Stub $stub, $isNested) | ||
{ | ||
$nodeType = new ConstStub(self::$nodeTypes[$reader->nodeType], $reader->nodeType); | ||
$parserProperties = new EnumStub(array( | ||
'LOADDTD' => $reader->getParserProperty(\XmlReader::LOADDTD), | ||
'DEFAULTATTRS' => $reader->getParserProperty(\XmlReader::DEFAULTATTRS), | ||
'VALIDATE' => $reader->getParserProperty(\XmlReader::VALIDATE), | ||
'SUBST_ENTITIES' => $reader->getParserProperty(\XmlReader::SUBST_ENTITIES), | ||
)); | ||
|
||
if (\XmlReader::NONE === $reader->nodeType) { | ||
return $a + self::castXmlNone($nodeType, $parserProperties); | ||
} | ||
|
||
if (\XmlReader::ATTRIBUTE === $reader->nodeType) { | ||
return $a + self::castAttribute($reader, $nodeType, $parserProperties); | ||
} | ||
|
||
$infos = array( | ||
'localName' => $reader->localName, | ||
'nodeType' => $nodeType, | ||
'depth' => $reader->depth, | ||
'attributeCount' => $reader->attributeCount, | ||
'hasAttributes' => $reader->hasAttributes, | ||
'hasValue' => $reader->hasValue, | ||
'isDefault' => $reader->isDefault, | ||
'isEmptyElement' => $reader->isEmptyElement, | ||
); | ||
|
||
if ('' !== $reader->prefix) { | ||
$infos['prefix'] = $reader->prefix; | ||
$infos['namespaceURI'] = $reader->namespaceURI; | ||
} | ||
|
||
if ($reader->hasValue && \XmlReader::TEXT === $reader->nodeType) { | ||
$infos['value'] = $reader->value; | ||
} | ||
|
||
if ($reader->hasAttributes) { | ||
$infos[Caster::PREFIX_VIRTUAL.'attributes'] = array(); | ||
|
||
for ($i = 0; $i < $reader->attributeCount; ++$i) { | ||
$infos[Caster::PREFIX_VIRTUAL.'attributes'][] = $reader->getAttributeNo($i); | ||
} | ||
} | ||
|
||
if (isset(self::$filteredTypes[$reader->nodeType])) { | ||
$infos = self::castFilteredElement($reader, $infos, $stub, $nodeType); | ||
} | ||
|
||
$infos[Caster::PREFIX_VIRTUAL.'parserProperties'] = $parserProperties; | ||
|
||
return $a + $infos; | ||
} | ||
|
||
private static function castXmlNone(ConstStub $type, EnumStub $properties) | ||
{ | ||
return array( | ||
'nodeType' => $type, | ||
Caster::PREFIX_VIRTUAL.'parserProperties' => $properties, | ||
); | ||
} | ||
|
||
private static function castFilteredElement(\XmlReader $reader, array $infos, Stub $stub, ConstStub $type) | ||
{ | ||
$cut = array( | ||
'localName' => $reader->localName, | ||
'nodeType' => $type, | ||
'depth' => $reader->depth, | ||
); | ||
|
||
$stub->cut += count($infos) - count($cut); | ||
|
||
return $cut; | ||
} | ||
|
||
private static function castAttribute(\XmlReader $reader, ConstStub $type, EnumStub $properties) | ||
{ | ||
$infos = array( | ||
'localName' => $reader->localName, | ||
'nodeType' => $type, | ||
'depth' => $reader->depth, | ||
'isDefault' => $reader->isDefault, | ||
'hasValue' => $reader->hasValue, | ||
); | ||
|
||
if ($reader->hasValue) { | ||
$infos['value'] = $reader->value; | ||
} | ||
|
||
if ('' !== $reader->prefix) { | ||
$infos['prefix'] = $reader->prefix; | ||
$infos['namespaceURI'] = $reader->namespaceURI; | ||
} | ||
|
||
$infos[Caster::PREFIX_VIRTUAL.'parserProperties'] = $properties; | ||
|
||
return $infos; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.