Files
aowow/includes/components/frontend/book.class.php
Sarjuuk bffdb9672e 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
2025-07-27 16:42:12 +02:00

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";
}
}
?>