/** * Duplicate a page and its descendants. * * @param Page $page The page to be duplicated. * @param string|null $title Optional, the title of the copy, if null (default) the title of the copied page. * @param Page|null $parent Optional, the parent of the copy, if null (default) the parent of the copied page. * * @return Page The copy of the page. * * @throws InvalidArgumentException Raises if the page is recursively duplicated in itself. */ private function duplicateRecursively(Page $page, $title = null, Page $parent = null) { if (null !== $parent && true === $parent->isDescendantOf($page)) { throw new InvalidArgumentException('Cannot recursively duplicate a page in itself'); } // Storing current children before clonage $children = $this->getDescendants($page, 1); // Cloning the page $new_page = $this->copy($page, $title, $parent); $this->_em->flush($new_page); // Cloning children foreach (array_reverse($children) as $child) { if (!(Page::STATE_DELETED & $child->getState())) { $new_child = $this->duplicateRecursively($child, null, $new_page); $new_page->cloningData = array_merge_recursive($new_page->cloningData, $new_child->cloningData); } } return $new_page; }