Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/main/lib/phonenumber/tools/ |
Upload File : |
<?php /** * Basic class for recursive xml parser */ namespace Bitrix\Main\PhoneNumber\Tools; abstract class XmlParser { /** @var XmlField[] */ protected $attributesMapping = array(); protected $properties = array(); public function __construct() { $this->attributesMapping = $this->getMap(); } /** * @param \XMLReader $xmlReader * @param string $path * @return array */ public function parseElement(\XMLReader $xmlReader, $path) { $this->init(); $this->walkDomTree($xmlReader, $path); return $this->properties; } protected function init() { $this->properties = array(); } /** * @param \XMLReader $xmlReader * @param string $parentPath XPath to the element. */ protected function walkDomTree(\XMLReader $xmlReader, $parentPath) { $path = $parentPath . $xmlReader->localName . '/'; $field = $this->getField($path); if(!is_null($field)) { if ($field->isMultiple()) { $this->properties[$field->getName()][] = $this->getElementValue($xmlReader, $parentPath, $field); } else { $this->properties[$field->getName()] = $this->getElementValue($xmlReader, $parentPath, $field); } // if element was parsed by subParser, XMLReader cursor will be moved to the end of the current element // and thus we should exit current level of recursion. if($xmlReader->nodeType == \XMLReader::END_ELEMENT) return; } if($xmlReader->nodeType == \XMLReader::ELEMENT && $xmlReader->hasAttributes) { $this->parseAttributes($xmlReader, $path); } // empty element does not have child elements if($xmlReader->isEmptyElement) return; // recursively reading child elements or leaving recursion on the end of the current element while($xmlReader->read()) { if($xmlReader->nodeType == \XMLReader::ELEMENT) $this->walkDomTree($xmlReader, $path); else if($xmlReader->nodeType == \XMLReader::END_ELEMENT) return; } } protected function parseAttributes(\XMLReader $xmlReader, $parentPath) { $xmlReader->moveToFirstAttribute(); do { $path = $parentPath . '@' . $xmlReader->localName; $field = $this->getField($path); if(!is_null($field)) { $this->properties[$field->getName()] = $field->decodeValue($xmlReader->value); } } while(($xmlReader->moveToNextAttribute())); $xmlReader->moveToElement(); } /** * @param $path * @return XmlField; */ protected function getField($path) { if(array_key_exists($path, $this->attributesMapping)) return $this->attributesMapping[$path]; else return null; } protected function getElementValue(\XMLReader $xmlReader, $path, XmlField $field) { $subParser = $field->getSubParser(); if(is_null($subParser)) return $field->decodeValue($xmlReader->readString()); else return $subParser->parseElement($xmlReader, $path); } /** * Function should return array * @return XmlField[] */ abstract public function getMap(); }