mirror of
https://github.com/Sarjuuk/aowow.git
synced 2025-11-29 15:58:16 +08:00
Future/Frontend
* create php classes, each mirroring a js object * each frontend class implements __toString and json_serialize and as such can be directly used by the template * also allows for sane object creation before js screams in agony * usage TBD
This commit is contained in:
50
includes/components/frontend/book.class.php
Normal file
50
includes/components/frontend/book.class.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Aowow;
|
||||
|
||||
if (!defined('AOWOW_REVISION'))
|
||||
die('illegal access');
|
||||
|
||||
|
||||
class Book implements \JsonSerializable
|
||||
{
|
||||
public function __construct(
|
||||
private array $pages, // js:array of html
|
||||
private string $parent = 'book-generic', // HTMLNode.id
|
||||
private ?int $page = null) // start page; defaults to 1
|
||||
{
|
||||
if (!$this->parent)
|
||||
trigger_error(self::class.'::__construct - initialized without parent element', E_USER_WARNING);
|
||||
|
||||
if (!$this->pages)
|
||||
trigger_error(self::class.'::__construct - initialized without content', E_USER_WARNING);
|
||||
else
|
||||
$this->pages = Util::parseHtmlText($this->pages);
|
||||
}
|
||||
|
||||
public function &iterate() : \Generator
|
||||
{
|
||||
reset($this->pages);
|
||||
|
||||
foreach ($this->pages as $idx => &$page)
|
||||
yield $idx => $page;
|
||||
}
|
||||
|
||||
public function jsonSerialize() : array
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ($this as $prop => $val)
|
||||
if ($val !== null && $prop[0] != '_')
|
||||
$result[$prop] = $val;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function __toString() : string
|
||||
{
|
||||
return "new Book(".Util::toJSON($this).");\n";
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user