mirror of
https://github.com/suk-ws/ph-Bookshelf.git
synced 2024-12-05 09:26:52 +08:00
构建了XML格式的书架和书籍元数据对象及其解析,添加git eol=LF 限制
- 总共添加的对象: - Bookshelf - LinkCollection - Link - BookCollection - Book - BookContented - Chapter - Page - Segment
This commit is contained in:
parent
658f76fdc0
commit
a401c4cc55
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
@ -0,0 +1 @@
|
||||
* text eol=lf
|
33
src/Element/Book.php
Normal file
33
src/Element/Book.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
class Book {
|
||||
|
||||
private string $id;
|
||||
private string $name;
|
||||
|
||||
public function __construct (string $id, string $name) {
|
||||
$this->id = $id;
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
public static function parse (DOMNode $xmlData): Book {
|
||||
$id = "";
|
||||
$name = "";
|
||||
if ($xmlData->hasAttributes()) {
|
||||
$id = $xmlData->attributes->getNamedItem("id")->nodeValue;
|
||||
$name = $xmlData->attributes->getNamedItem("name")->nodeValue;
|
||||
} else {
|
||||
echo "ERROR PARSE XML BOOK NO ATTRIBUTE\n";
|
||||
}
|
||||
return new Book($id, $name);
|
||||
}
|
||||
|
||||
public function getId (): string {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getName (): string {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
}
|
40
src/Element/BookCollection.php
Normal file
40
src/Element/BookCollection.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
require_once "./src/Element/Book.php";
|
||||
|
||||
class BookCollection {
|
||||
|
||||
/** @var Book[]|BookCollection[] */
|
||||
private array $array;
|
||||
|
||||
private function __construct (array $a) {
|
||||
$this->array = $a;
|
||||
}
|
||||
|
||||
public static function parse (DOMNode $root): BookCollection {
|
||||
$node = new BookCollection(array());
|
||||
for ($child = $root->firstChild; $child != null; $child = $child->nextSibling) {
|
||||
switch ($child->nodeName) {
|
||||
case "Book":
|
||||
array_push($node->array, Book::parse($child));
|
||||
break;
|
||||
case "Collection":
|
||||
array_push($node->array, BookCollection::parse($child));
|
||||
break;
|
||||
case "#text":
|
||||
break;
|
||||
default:
|
||||
echo "ERROR UNSUPPORTED NODE TYPE ON BOOK COLLECTION\n";
|
||||
}
|
||||
}
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Book[]|BookCollection[]
|
||||
*/
|
||||
public function getCollection (): array {
|
||||
return $this->array;
|
||||
}
|
||||
|
||||
}
|
39
src/Element/BookContent/BookContented.php
Normal file
39
src/Element/BookContent/BookContented.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
require_once "./src/Element/Book.php";
|
||||
require_once "./src/Element/BookContent/Chapter.php";
|
||||
|
||||
class BookContented extends Book {
|
||||
|
||||
private Chapter $childs;
|
||||
|
||||
public static function parse (DOMNode $xmlData): BookContented {
|
||||
$id = "";
|
||||
$name = "";
|
||||
if ($xmlData->hasAttributes()) {
|
||||
$id = $xmlData->attributes->getNamedItem("id")->nodeValue;
|
||||
$name = $xmlData->attributes->getNamedItem("name")->nodeValue;
|
||||
} else {
|
||||
echo "ERROR PARSE XML BOOK WITH CONTENT NO ATTRIBUTE\n";
|
||||
}
|
||||
$node = new BookContented($id, $name);
|
||||
if ($xmlData->hasChildNodes()) {
|
||||
$node->childs = Chapter::parse($xmlData);
|
||||
} else {
|
||||
echo "ERROR PARSE XML BOOK WITH CONTENT NO CHILD\n";
|
||||
}
|
||||
return $node;
|
||||
}
|
||||
|
||||
public static function parseString (string $xmlContent): BookContented {
|
||||
|
||||
$node = null;
|
||||
$dom = new DOMDocument();
|
||||
if ($dom->loadXML($xmlContent)) {
|
||||
$node = self::parse($dom->firstChild);
|
||||
} else echo "ERROR PARSE BOOK CONTENTED DOM FAILED\n";
|
||||
return $node;
|
||||
|
||||
}
|
||||
|
||||
}
|
36
src/Element/BookContent/Chapter.php
Normal file
36
src/Element/BookContent/Chapter.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
require_once "./src/Element/BookContent/Page.php";
|
||||
|
||||
class Chapter {
|
||||
|
||||
private string $name;
|
||||
|
||||
/** @var Chapter[]|Page[] */
|
||||
private array $childs;
|
||||
|
||||
private function __construct (string $name, array $array) {
|
||||
$this->name = $name;
|
||||
$this->childs = $array;
|
||||
}
|
||||
|
||||
public static function parse (DOMNode $xmlData): Chapter {
|
||||
$node = new Chapter($xmlData->attributes->getNamedItem("name")->nodeValue, array());
|
||||
for ($child = $xmlData->firstChild;$child != null ; $child = $child->nextSibling) {
|
||||
switch ($child->nodeName) {
|
||||
case "Page":
|
||||
array_push($node->childs, Page::parse($child));
|
||||
break;
|
||||
case "Chapter":
|
||||
array_push($node->childs, self::parse($child));
|
||||
break;
|
||||
case "#text":
|
||||
break;
|
||||
default:
|
||||
echo "ERROR UNSUPPORTED NODE TYPE ON BOOK CHAPTER\n";
|
||||
}
|
||||
}
|
||||
return $node;
|
||||
}
|
||||
|
||||
}
|
43
src/Element/BookContent/Page.php
Normal file
43
src/Element/BookContent/Page.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
require_once "./src/Element/BookContent/Segment.php";
|
||||
|
||||
class Page {
|
||||
|
||||
private string $id;
|
||||
private string $name;
|
||||
|
||||
/** @var Segment[] */
|
||||
private array $segues;
|
||||
|
||||
public function __construct (string $id, string $name, array $childs = array()) {
|
||||
$this->id = $id;
|
||||
$this->name = $name;
|
||||
$this->segues = $childs;
|
||||
}
|
||||
|
||||
public static function parse (DOMNode $xmlData): Page {
|
||||
$node = null;
|
||||
if (!$xmlData->hasAttributes()) {
|
||||
echo "ERROR PARSE XML PAGE NO ATTRIBUTE\n";
|
||||
} else {
|
||||
$node = new Page(
|
||||
$xmlData->attributes->getNamedItem("id")->nodeValue,
|
||||
$xmlData->attributes->getNamedItem("name")->nodeValue
|
||||
);
|
||||
}
|
||||
for ($child = $xmlData->firstChild;$child != null ; $child = $child->nextSibling) {
|
||||
switch ($child->nodeName) {
|
||||
case "Segment":
|
||||
array_push($node->segues, Segment::parse($child));
|
||||
break;
|
||||
case "#text":
|
||||
break;
|
||||
default:
|
||||
echo "ERROR UNSUPPORTED NODE TYPE ON PAGE CHAPTER\n";
|
||||
}
|
||||
}
|
||||
return $node;
|
||||
}
|
||||
|
||||
}
|
24
src/Element/BookContent/Segment.php
Normal file
24
src/Element/BookContent/Segment.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
class Segment {
|
||||
|
||||
private string $id;
|
||||
private string $name;
|
||||
|
||||
public function __construct ($id, $name) {
|
||||
$this->id = $id;
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
public static function parse (DOMNode $xmlData): Segment {
|
||||
$node = null;
|
||||
if ($xmlData->hasAttributes()) {
|
||||
$node = new Segment(
|
||||
$xmlData->attributes->getNamedItem("id")->nodeValue,
|
||||
$xmlData->attributes->getNamedItem("name")->nodeValue
|
||||
);
|
||||
}
|
||||
return $node;
|
||||
}
|
||||
|
||||
}
|
74
src/Element/Bookshelf.php
Normal file
74
src/Element/Bookshelf.php
Normal file
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
require_once "./src/Element/Book.php";
|
||||
require_once "./src/Element/BookCollection.php";
|
||||
require_once "./src/Element/Link.php";
|
||||
require_once "./src/Element/LinkCollection.php";
|
||||
require_once "./src/Element/BookContent/BookContented.php";
|
||||
|
||||
class Bookshelf {
|
||||
|
||||
private string $siteName;
|
||||
|
||||
private LinkCollection $links;
|
||||
private BookCollection $books;
|
||||
|
||||
private BookContented $rootBook;
|
||||
|
||||
public static function parseString (string $xmlData): Bookshelf {
|
||||
$return = new Bookshelf();
|
||||
$dom = new DOMDocument();
|
||||
if ($dom->loadXML($xmlData)) {
|
||||
$dom = $dom->firstChild;
|
||||
if ($dom->hasAttributes() && $dom->hasChildNodes()) {
|
||||
|
||||
// Bookshelf 属性
|
||||
$return->siteName = $dom->attributes->getNamedItem("siteName")->nodeValue;
|
||||
|
||||
// 对根节点的子节点进行遍历
|
||||
for ($rc = $dom->firstChild; $rc != null; $rc = $rc->nextSibling) {
|
||||
switch ($rc->nodeName) {
|
||||
case "links":
|
||||
$return->links = LinkCollection::parse($rc);
|
||||
break;
|
||||
case "books":
|
||||
$return->books = BookCollection::parse($rc);
|
||||
break;
|
||||
case "rootBook":
|
||||
$return->rootBook = BookContented::parse($rc);
|
||||
break;
|
||||
case "#text":
|
||||
break;
|
||||
default:
|
||||
echo "ERROR UNSUPPORTED TYPE ON BOOKSHELF\n";
|
||||
}
|
||||
}
|
||||
|
||||
} else echo "ERROR ROOT NO CONTENT\n";
|
||||
} else echo "ERROR PARSE BOOKSHELF DOM FAILED\n";
|
||||
return $return;
|
||||
}
|
||||
|
||||
public function getSiteName (): string {
|
||||
return $this->siteName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return LinkCollection
|
||||
*/
|
||||
public function getLinks (): LinkCollection {
|
||||
return $this->links;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BookCollection
|
||||
*/
|
||||
public function getBooks (): BookCollection {
|
||||
return $this->books;
|
||||
}
|
||||
|
||||
public function getRootBook (): BookContented {
|
||||
return $this->rootBook;
|
||||
}
|
||||
|
||||
}
|
34
src/Element/Link.php
Normal file
34
src/Element/Link.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
class Link {
|
||||
|
||||
private string $name;
|
||||
|
||||
private string $href;
|
||||
|
||||
public function __construct (string $name, string $href) {
|
||||
$this->name = $name;
|
||||
$this->href = $href;
|
||||
}
|
||||
|
||||
public static function parse (DOMNode $xmlData): Link {
|
||||
$name = "";
|
||||
$href = "";
|
||||
if ($xmlData->hasAttributes()) {
|
||||
$name = $xmlData->attributes->getNamedItem("name")->nodeValue;
|
||||
$href = $xmlData->attributes->getNamedItem("href")->nodeValue;
|
||||
} else {
|
||||
echo "ERROR PARSE XML LINK NO ATTRIBUTE\n";
|
||||
}
|
||||
return new Link($name, $href);
|
||||
}
|
||||
|
||||
public function getHref (): string {
|
||||
return $this->href;
|
||||
}
|
||||
|
||||
public function getName (): string {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
}
|
40
src/Element/LinkCollection.php
Normal file
40
src/Element/LinkCollection.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
require_once "./src/Element/Link.php";
|
||||
|
||||
class LinkCollection {
|
||||
|
||||
/** @var Link[]|LinkCollection[] */
|
||||
private array $array;
|
||||
|
||||
private function __construct (array $a) {
|
||||
$this->array = $a;
|
||||
}
|
||||
|
||||
public static function parse (DOMNode $root): LinkCollection {
|
||||
$node = new LinkCollection(array());
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user