mirror of
https://github.com/Sarjuuk/aowow.git
synced 2025-11-29 15:58:16 +08:00
* 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
51 lines
1.2 KiB
PHP
51 lines
1.2 KiB
PHP
<?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";
|
|
}
|
|
}
|
|
|
|
?>
|