| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\Package\CalDav\ICal; |
| 4 |
|
| 5 |
use DOMDocument; |
| 6 |
use DOMXPath; |
| 7 |
|
| 8 |
class XmlParser |
| 9 |
{ |
| 10 |
public static function parse($xml) |
| 11 |
{ |
| 12 |
$instance = new static; |
| 13 |
|
| 14 |
$doc = new DOMDocument(); |
| 15 |
|
| 16 |
$doc->loadXML($xml); |
| 17 |
|
| 18 |
$xpath = new DOMXPath($doc); |
| 19 |
|
| 20 |
$xpath->registerNamespace('d', 'DAV:'); |
| 21 |
|
| 22 |
$list = []; |
| 23 |
|
| 24 |
foreach ($xpath->query('//d:response') as $res) { |
| 25 |
$list[] = $instance->parseNodes($res); |
| 26 |
} |
| 27 |
|
| 28 |
return $list; |
| 29 |
} |
| 30 |
|
| 31 |
protected function parseNodes($x) |
| 32 |
{ |
| 33 |
$nodes = []; |
| 34 |
|
| 35 |
foreach ($x->childNodes as $n) { |
| 36 |
if ($this->hasChild($n)) { |
| 37 |
$nodes[$n->localName][] = $this->parseNodes($n); |
| 38 |
} else if ($n->nodeType == XML_ELEMENT_NODE) { |
| 39 |
$nodes[$n->localName][] = $n->nodeValue; |
| 40 |
|
| 41 |
if ($n->hasAttributes()) { |
| 42 |
foreach ($n->attributes as $attr) { |
| 43 |
$nodes[$n->localName][]['attributers'][$attr->nodeName] = $attr->nodeValue; |
| 44 |
} |
| 45 |
|
| 46 |
$nodes[$n->localName] = array_values(array_filter($nodes[$n->localName])); |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
return $nodes; |
| 52 |
} |
| 53 |
|
| 54 |
protected function hasChild($n) |
| 55 |
{ |
| 56 |
if ($n->hasChildNodes()) { |
| 57 |
foreach ($n->childNodes as $c) { |
| 58 |
if ($c->nodeType == XML_ELEMENT_NODE) { |
| 59 |
return true; |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return false; |
| 65 |
} |
| 66 |
} |
| 67 |
|