blob: fa0b55dc7833e0416fd5fa6da700997284584270 [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 {
47 $props = Caster::PREFIX_VIRTUAL.'parserProperties';
48 $info = [
49 'localName' => $reader->localName,
50 'prefix' => $reader->prefix,
51 'nodeType' => new ConstStub(self::NODE_TYPES[$reader->nodeType], $reader->nodeType),
52 'depth' => $reader->depth,
53 'isDefault' => $reader->isDefault,
54 'isEmptyElement' => \XMLReader::NONE === $reader->nodeType ? null : $reader->isEmptyElement,
55 'xmlLang' => $reader->xmlLang,
56 'attributeCount' => $reader->attributeCount,
57 'value' => $reader->value,
58 'namespaceURI' => $reader->namespaceURI,
59 'baseURI' => $reader->baseURI ? new LinkStub($reader->baseURI) : $reader->baseURI,
60 $props => [
61 'LOADDTD' => $reader->getParserProperty(\XMLReader::LOADDTD),
62 'DEFAULTATTRS' => $reader->getParserProperty(\XMLReader::DEFAULTATTRS),
63 'VALIDATE' => $reader->getParserProperty(\XMLReader::VALIDATE),
64 'SUBST_ENTITIES' => $reader->getParserProperty(\XMLReader::SUBST_ENTITIES),
65 ],
66 ];
67
68 if ($info[$props] = Caster::filter($info[$props], Caster::EXCLUDE_EMPTY, [], $count)) {
69 $info[$props] = new EnumStub($info[$props]);
70 $info[$props]->cut = $count;
71 }
72
73 $info = Caster::filter($info, Caster::EXCLUDE_EMPTY, [], $count);
74 // +2 because hasValue and hasAttributes are always filtered
75 $stub->cut += $count + 2;
76
77 return $a + $info;
78 }
79}