/** * Merges this navigation item with the other parameter and returns a new NavigationItem. * Works only if there are no duplicate values on one level. * @param NavigationItem $other The navigation item this one should be merged with * @return NavigationItem */ public function merge(NavigationItem $other = null) { // Create new item $new = $this->copyChildless(); // Add all children from this item foreach ($this->getChildren() as $child) { /** @var NavigationItem $child */ $new->addChild($child->merge($other != null ? $other->findChildren($child) : null)); } // Add all children from the other item if ($other != null) { foreach ($other->getChildren() as $child) { /** @var NavigationItem $child */ if (!$new->find($child)) { $new->addChild($child->merge($this->copyChildless())); } } } return $new; }