Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
xmlparser.php
1<?php
7
8
9abstract class XmlParser
10{
12 protected $attributesMapping = array();
13 protected $properties = array();
14
15 public function __construct()
16 {
17 $this->attributesMapping = $this->getMap();
18 }
19
25 public function parseElement(\XMLReader $xmlReader, $path)
26 {
27 $this->init();
28 $this->walkDomTree($xmlReader, $path);
29 return $this->properties;
30 }
31
32 protected function init()
33 {
34 $this->properties = array();
35 }
36
41 protected function walkDomTree(\XMLReader $xmlReader, $parentPath)
42 {
43 $path = $parentPath . $xmlReader->localName . '/';
44 $field = $this->getField($path);
45
46 if(!is_null($field))
47 {
48 if ($field->isMultiple())
49 {
50 $this->properties[$field->getName()][] = $this->getElementValue($xmlReader, $parentPath, $field);
51 }
52 else
53 {
54 $this->properties[$field->getName()] = $this->getElementValue($xmlReader, $parentPath, $field);
55 }
56
57 // if element was parsed by subParser, XMLReader cursor will be moved to the end of the current element
58 // and thus we should exit current level of recursion.
59 if($xmlReader->nodeType == \XMLReader::END_ELEMENT)
60 return;
61 }
62
63 if($xmlReader->nodeType == \XMLReader::ELEMENT && $xmlReader->hasAttributes)
64 {
65 $this->parseAttributes($xmlReader, $path);
66 }
67
68 // empty element does not have child elements
69 if($xmlReader->isEmptyElement)
70 return;
71
72 // recursively reading child elements or leaving recursion on the end of the current element
73 while($xmlReader->read())
74 {
75 if($xmlReader->nodeType == \XMLReader::ELEMENT)
76 $this->walkDomTree($xmlReader, $path);
77 else if($xmlReader->nodeType == \XMLReader::END_ELEMENT)
78 return;
79 }
80 }
81
82 protected function parseAttributes(\XMLReader $xmlReader, $parentPath)
83 {
84 $xmlReader->moveToFirstAttribute();
85
86 do
87 {
88 $path = $parentPath . '@' . $xmlReader->localName;
89 $field = $this->getField($path);
90
91 if(!is_null($field))
92 {
93 $this->properties[$field->getName()] = $field->decodeValue($xmlReader->value);
94 }
95 } while(($xmlReader->moveToNextAttribute()));
96
97 $xmlReader->moveToElement();
98 }
99
104 protected function getField($path)
105 {
106 if(array_key_exists($path, $this->attributesMapping))
107 return $this->attributesMapping[$path];
108 else
109 return null;
110 }
111
112 protected function getElementValue(\XMLReader $xmlReader, $path, XmlField $field)
113 {
114 $subParser = $field->getSubParser();
115 if(is_null($subParser))
116 return $field->decodeValue($xmlReader->readString());
117 else
118 return $subParser->parseElement($xmlReader, $path);
119 }
120
125 abstract public function getMap();
126
127}
parseElement(\XMLReader $xmlReader, $path)
Definition xmlparser.php:25
getElementValue(\XMLReader $xmlReader, $path, XmlField $field)
parseAttributes(\XMLReader $xmlReader, $parentPath)
Definition xmlparser.php:82
walkDomTree(\XMLReader $xmlReader, $parentPath)
Definition xmlparser.php:41