2021-03-20 22:18:15 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once "./src/Element/Link.php";
|
|
|
|
|
|
|
|
class LinkCollection {
|
|
|
|
|
2021-03-20 22:39:24 +08:00
|
|
|
const ROOT = "::root";
|
|
|
|
|
|
|
|
private string $name;
|
|
|
|
|
2021-03-20 22:18:15 +08:00
|
|
|
/** @var Link[]|LinkCollection[] */
|
|
|
|
private array $array;
|
|
|
|
|
2021-03-20 22:39:24 +08:00
|
|
|
private function __construct (string $name, array $a) {
|
|
|
|
$this->name = $name;
|
2021-03-20 22:18:15 +08:00
|
|
|
$this->array = $a;
|
|
|
|
}
|
|
|
|
|
2021-03-20 22:39:24 +08:00
|
|
|
public static function parse (DOMNode $root, bool $isRoot = false): LinkCollection {
|
|
|
|
$name = LinkCollection::ROOT;
|
|
|
|
if (!$isRoot) $name = $root->attributes->getNamedItem("name")->nodeValue;
|
|
|
|
$node = new LinkCollection($name, array());
|
2021-03-20 22:18:15 +08:00
|
|
|
for ($child = $root->firstChild; $child != null; $child = $child->nextSibling) {
|
|
|
|
switch ($child->nodeName) {
|
|
|
|
case "Link":
|
|
|
|
array_push($node->array, Link::parse($child));
|
|
|
|
break;
|
|
|
|
case "Collection":
|
|
|
|
array_push($node->array, LinkCollection::parse($child));
|
|
|
|
break;
|
|
|
|
case "#text":
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
echo "ERROR UNSUPPORTED NODE TYPE ON LINK COLLECTION\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Link[]|LinkCollection[]
|
|
|
|
*/
|
|
|
|
public function getCollection (): array {
|
|
|
|
return $this->array;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|