2021-03-20 22:18:15 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once "./src/Element/Link.php";
|
|
|
|
|
|
|
|
class LinkCollection {
|
|
|
|
|
2021-03-23 00:35:14 +08:00
|
|
|
const ROOT = "%root";
|
2021-03-20 22:39:24 +08:00
|
|
|
|
|
|
|
private string $name;
|
|
|
|
|
2021-03-20 22:18:15 +08:00
|
|
|
/** @var Link[]|LinkCollection[] */
|
|
|
|
private array $array;
|
2021-04-22 22:47:43 +08:00
|
|
|
private ?LinkCollection $parent;
|
2021-03-20 22:18:15 +08:00
|
|
|
|
2021-04-26 00:10:50 +08:00
|
|
|
private function __construct (string $name, ?LinkCollection $parent) {
|
2021-03-20 22:39:24 +08:00
|
|
|
$this->name = $name;
|
2021-04-26 00:10:50 +08:00
|
|
|
$this->array = array();
|
2021-03-23 01:57:53 +08:00
|
|
|
$this->parent = $parent;
|
2021-03-20 22:18:15 +08:00
|
|
|
}
|
|
|
|
|
2021-03-20 23:52:33 +08:00
|
|
|
/**
|
|
|
|
* @param DOMNode $root
|
2021-04-22 22:47:43 +08:00
|
|
|
* @param ?LinkCollection $parent
|
2021-03-20 23:52:33 +08:00
|
|
|
* @param bool $isRoot
|
|
|
|
* @return LinkCollection
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2021-04-22 22:47:43 +08:00
|
|
|
public static function parse (DOMNode $root, ?LinkCollection $parent, bool $isRoot = false): LinkCollection {
|
2021-03-20 22:39:24 +08:00
|
|
|
$name = LinkCollection::ROOT;
|
2021-03-20 23:52:33 +08:00
|
|
|
if (!$isRoot) {
|
|
|
|
if ($root->hasAttributes()) {
|
|
|
|
$attrName = $root->attributes->getNamedItem("name");
|
|
|
|
if ($attrName == null) throw new Exception("LinkCollection (not root) xml data missing attribute \"name\"");
|
|
|
|
else $name = $attrName->nodeValue;
|
|
|
|
} else throw new Exception("LinkCollection (not root) xml data missing attributes");
|
|
|
|
}
|
2021-04-26 00:10:50 +08:00
|
|
|
$node = new LinkCollection($name, $parent);
|
2021-03-20 22:18:15 +08:00
|
|
|
for ($child = $root->firstChild; $child != null; $child = $child->nextSibling) {
|
|
|
|
switch ($child->nodeName) {
|
|
|
|
case "Link":
|
2021-03-23 01:57:53 +08:00
|
|
|
array_push($node->array, Link::parse($child, $node));
|
2021-03-20 22:18:15 +08:00
|
|
|
break;
|
|
|
|
case "Collection":
|
2021-03-23 01:57:53 +08:00
|
|
|
array_push($node->array, LinkCollection::parse($child, $node));
|
2021-03-20 22:18:15 +08:00
|
|
|
break;
|
|
|
|
case "#text":
|
|
|
|
break;
|
|
|
|
default:
|
2021-03-20 23:52:33 +08:00
|
|
|
throw new Exception("Unsupported element type \"$child->nodeName\" in LinkCollection named \"$name\"");
|
2021-03-20 22:18:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
2021-03-20 23:52:33 +08:00
|
|
|
public function getName (): string {
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
2021-03-20 22:18:15 +08:00
|
|
|
/**
|
|
|
|
* @return Link[]|LinkCollection[]
|
|
|
|
*/
|
|
|
|
public function getCollection (): array {
|
|
|
|
return $this->array;
|
|
|
|
}
|
|
|
|
|
2021-03-23 01:57:53 +08:00
|
|
|
/**
|
|
|
|
* @return LinkCollection|null
|
|
|
|
*/
|
|
|
|
public function getParent (): LinkCollection {
|
|
|
|
return $this->parent;
|
|
|
|
}
|
|
|
|
|
2021-04-26 00:10:50 +08:00
|
|
|
public function getHtml (): string {
|
|
|
|
$str = "";
|
2021-04-30 12:41:13 +08:00
|
|
|
if ($this->name != self::ROOT) $str .= "<li class='link-collection fold'><a class='link-collection chapter'>$this->name<i class='exc-trigger fa'></i></a><ul class='link-collection articles'>";
|
2021-04-26 00:10:50 +08:00
|
|
|
foreach ($this->array as $node) {
|
|
|
|
$str .= $node->getHtml();
|
|
|
|
}
|
|
|
|
if ($this->name != self::ROOT) $str .= "</ul></li>";
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
2021-03-20 22:18:15 +08:00
|
|
|
}
|