Mark V
1: <?php
2: namespace MOC\V\Component\Document\Vendor\UniversalXml;
3:
4: use MOC\V\Component\Document\Vendor\UniversalXml\Source\Node;
5: use MOC\V\Component\Document\Vendor\UniversalXml\Source\Parser;
6: use MOC\V\Component\Document\Vendor\UniversalXml\Source\Tokenizer;
7:
8: /**
9: * Class Api
10: *
11: * @package MOC\V\Component\Document\Vendor\UniversalXml
12: */
13: class Api
14: {
15:
16: /** @var string $XmlContent */
17: private $XmlContent = '';
18:
19: /**
20: * @param string $XmlContent
21: * @codeCoverageIgnore (Cache)
22: */
23: function __construct($XmlContent)
24: {
25:
26: $this->XmlContent = (string)$XmlContent;
27: }
28:
29: /**
30: * @return Node|null
31: */
32: public function parseContent()
33: {
34:
35: $Instance = new Parser(new Tokenizer($this->XmlContent));
36:
37: return $Instance->getResult();
38: }
39:
40: /**
41: * @param \SimpleXMLElement $Xml
42: *
43: * @codeCoverageIgnore
44: * @return Node
45: */
46: private function parseSimpleXml(\SimpleXMLElement $Xml)
47: {
48:
49: $Node = new Node();
50: $Node->setName($Xml->getName());
51: $Object = get_object_vars($Xml);
52:
53: if (isset( $Object['@attributes'] )) {
54: array_walk($Object['@attributes'], function ($Value, $Name, Node $Node) {
55:
56: $Node->setAttribute($Name, $Value);
57: }, $Node);
58: unset( $Object['@attributes'] );
59: }
60:
61: if (count($Object) > 0) {
62: $Object = new \ArrayIterator($Object);
63: foreach ($Object as $Children) {
64: if (is_object($Children)) {
65: $Node->addChild($this->parseSimpleXml($Children));
66: } else {
67: $Children = new \ArrayIterator($Children);
68: foreach ($Children as $Xml) {
69: $Node->addChild($this->parseSimpleXml($Xml));
70: }
71: }
72: }
73: } else {
74: $Node->setContent($Xml->__toString());
75: }
76:
77: return $Node;
78: }
79:
80: }
81: