public function copyPage($pg) { $key = $pg - 1; // Check if the page exists. if (!array_key_exists($key, $this->pages)) { throw new Exception('Error: That page does not exist.'); } $pi = $this->lastIndex($this->objects) + 1; $ci = $this->lastIndex($this->objects) + 2; $this->objects[$pi] = new Object\Page($this->objects[$this->pages[$key]]); $this->objects[$pi]->index = $pi; // Duplicate the page's content objects. $oldContent = $this->objects[$pi]->content; unset($this->objects[$pi]->content); foreach ($oldContent as $key => $value) { $this->objects[$ci] = new Object\Object((string) $this->objects[$value]); $this->objects[$ci]->index = $ci; $this->objects[$pi]->content[] = $ci; $ci += 1; } // Finalize related page variables and objects. $this->curPage = null === $this->curPage ? 0 : $this->lastIndex($this->pages) + 1; $this->pages[$this->curPage] = $pi; $this->objects[$this->parent]->count += 1; $this->objects[$this->parent]->kids[] = $pi; return $this; }