blob: 721513c5d2d7b104608a45047a6673067616f005 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?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
11namespace Symfony\Component\VarDumper\Caster;
12
13use 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 * @final
21 */
22class XmlReaderCaster
23{
24 private const NODE_TYPES = [
25 \XMLReader::NONE => 'NONE',
26 \XMLReader::ELEMENT => 'ELEMENT',
27 \XMLReader::ATTRIBUTE => 'ATTRIBUTE',
28 \XMLReader::TEXT => 'TEXT',
29 \XMLReader::CDATA => 'CDATA',
30 \XMLReader::ENTITY_REF => 'ENTITY_REF',
31 \XMLReader::ENTITY => 'ENTITY',
32 \XMLReader::PI => 'PI (Processing Instruction)',
33 \XMLReader::COMMENT => 'COMMENT',
34 \XMLReader::DOC => 'DOC',
35 \XMLReader::DOC_TYPE => 'DOC_TYPE',
36 \XMLReader::DOC_FRAGMENT => 'DOC_FRAGMENT',
37 \XMLReader::NOTATION => 'NOTATION',
38 \XMLReader::WHITESPACE => 'WHITESPACE',
39 \XMLReader::SIGNIFICANT_WHITESPACE => 'SIGNIFICANT_WHITESPACE',
40 \XMLReader::END_ELEMENT => 'END_ELEMENT',
41 \XMLReader::END_ENTITY => 'END_ENTITY',
42 \XMLReader::XML_DECLARATION => 'XML_DECLARATION',
43 ];
44
45 public static function castXmlReader(\XMLReader $reader, array $a, Stub $stub, bool $isNested)
46 {
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010047 try {
48 $properties = [
49 'LOADDTD' => @$reader->getParserProperty(\XMLReader::LOADDTD),
50 'DEFAULTATTRS' => @$reader->getParserProperty(\XMLReader::DEFAULTATTRS),
51 'VALIDATE' => @$reader->getParserProperty(\XMLReader::VALIDATE),
52 'SUBST_ENTITIES' => @$reader->getParserProperty(\XMLReader::SUBST_ENTITIES),
53 ];
54 } catch (\Error $e) {
55 $properties = [
56 'LOADDTD' => false,
57 'DEFAULTATTRS' => false,
58 'VALIDATE' => false,
59 'SUBST_ENTITIES' => false,
60 ];
61 }
62
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020063 $props = Caster::PREFIX_VIRTUAL.'parserProperties';
64 $info = [
65 'localName' => $reader->localName,
66 'prefix' => $reader->prefix,
67 'nodeType' => new ConstStub(self::NODE_TYPES[$reader->nodeType], $reader->nodeType),
68 'depth' => $reader->depth,
69 'isDefault' => $reader->isDefault,
70 'isEmptyElement' => \XMLReader::NONE === $reader->nodeType ? null : $reader->isEmptyElement,
71 'xmlLang' => $reader->xmlLang,
72 'attributeCount' => $reader->attributeCount,
73 'value' => $reader->value,
74 'namespaceURI' => $reader->namespaceURI,
75 'baseURI' => $reader->baseURI ? new LinkStub($reader->baseURI) : $reader->baseURI,
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010076 $props => $properties,
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020077 ];
78
79 if ($info[$props] = Caster::filter($info[$props], Caster::EXCLUDE_EMPTY, [], $count)) {
80 $info[$props] = new EnumStub($info[$props]);
81 $info[$props]->cut = $count;
82 }
83
84 $info = Caster::filter($info, Caster::EXCLUDE_EMPTY, [], $count);
85 // +2 because hasValue and hasAttributes are always filtered
86 $stub->cut += $count + 2;
87
88 return $a + $info;
89 }
90}