36 public const TAG_PAGES =
"pages";
37 public const TAG_PAGE_TEXT =
"text";
38 public const TAG_PAGE_PHOTONAME =
"photoname";
44 private array $pages = [];
46 public function __construct(
ItemIdentifier $identifier,
string $name){
47 parent::__construct($identifier, $name);
54 return isset($this->pages[$pageId]);
63 return $this->pages[$pageId]->getText();
71 public function setPageText(
int $pageId,
string $pageText) : self{
72 if(!$this->pageExists($pageId)){
73 $this->addPage($pageId);
86 public function addPage(
int $pageId) : self{
88 throw new \InvalidArgumentException(
"Page number \"$pageId\" is out of range");
91 for($current = count($this->pages); $current <= $pageId; $current++){
103 $newPages = $this->pages;
104 unset($newPages[$pageId]);
105 $this->pages = array_values($newPages);
114 public function insertPage(
int $pageId,
string $pageText =
"") : self{
115 if($pageId < 0 || $pageId > count($this->pages)){
116 throw new \InvalidArgumentException(
"Page ID must not be negative");
118 $newPages = array_slice($this->pages, 0, $pageId);
120 array_push($newPages, ...array_slice($this->pages, $pageId));
121 $this->pages = $newPages;
131 public function swapPages(
int $pageId1,
int $pageId2) : bool{
132 $pageContents1 = $this->getPageText($pageId1);
133 $pageContents2 = $this->getPageText($pageId2);
134 $this->setPageText($pageId1, $pageContents2);
135 $this->setPageText($pageId2, $pageContents1);
159 $this->pages = array_values($pages);
164 parent::deserializeCompoundTag($tag);
169 if(($compoundPages = $pages->cast(CompoundTag::class)) !==
null){
170 foreach($compoundPages as $page){
171 $this->pages[] =
new WritableBookPage(mb_scrub($page->getString(self::TAG_PAGE_TEXT),
'UTF-8'), $page->getString(self::TAG_PAGE_PHOTONAME,
""));
173 }elseif(($stringPages = $pages->cast(StringTag::class)) !==
null){
174 foreach($stringPages as $page){
175 $this->pages[] =
new WritableBookPage(mb_scrub($page->getValue(),
'UTF-8'));
181 protected function serializeCompoundTag(
CompoundTag $tag) : void{
182 parent::serializeCompoundTag($tag);
183 if(count($this->pages) > 0){
184 $pages =
new ListTag();
185 foreach($this->pages as $page){
186 $pages->push(CompoundTag::create()
187 ->setString(self::TAG_PAGE_TEXT, $page->getText())
188 ->setString(self::TAG_PAGE_PHOTONAME, $page->getPhotoName())
191 $tag->
setTag(self::TAG_PAGES, $pages);