1
0
mirror of https://github.com/suk-ws/ph-Bookshelf.git synced 2025-03-14 23:37:29 +08:00
ph-Bookshelf/src/Element/BookCollection.php

74 lines
1.8 KiB
PHP
Raw Normal View History

<?php
require_once "./src/Element/Book.php";
class BookCollection {
const ROOT = "%root";
private string $name;
/** @var Book[]|BookCollection[] */
private array $array;
private ?BookCollection $parent;
private function __construct (string $name, array $a, ?BookCollection $parent) {
$this->name = $name;
$this->array = $a;
$this->parent = $parent;
}
/**
* @param DOMNode $root
* @param ?BookCollection $parent
* @param bool $isRoot
* @return BookCollection
* @throws Exception
*/
public static function parse (DOMNode $root, ?BookCollection $parent, bool $isRoot = false): BookCollection {
$name = BookCollection::ROOT;
if (!$isRoot) {
if ($root->hasAttributes()) {
$attrName = $root->attributes->getNamedItem("name");
if ($attrName == null) throw new Exception("BookCollection (not root) xml data missing attribute \"name\"");
else $name = $attrName->nodeValue;
} else throw new Exception("BookCollection (not root) xml data missing attributes");
}
$node = new BookCollection($name, array(), $parent);
for ($child = $root->firstChild; $child != null; $child = $child->nextSibling) {
switch ($child->nodeName) {
case "Book":
array_push($node->array, Book::parse($child, $node));
break;
case "Collection":
array_push($node->array, BookCollection::parse($child, $node));
break;
case "#text":
break;
default:
throw new Exception("Unsupported element type \"$child->nodeName\" in BookCollection named \"$name\"");
}
}
return $node;
}
public function getName (): string {
return $this->name;
}
/**
* @return Book[]|BookCollection[]
*/
public function getCollection (): array {
return $this->array;
}
/**
* @return BookCollection|null
*/
public function getParent (): BookCollection {
return $this->parent;
}
}