Sulu\Bundle\AdminBundle\Navigation\NavigationItem::merge PHP Method

merge() public method

Works only if there are no duplicate values on one level.
public merge ( NavigationItem $other = null ) : NavigationItem
$other NavigationItem 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;
    }

Usage Example

Example #1
0
 public function testMerge()
 {
     $merged = $this->item1->merge($this->item2);
     $this->assertEquals('Root', $merged->getName());
     $mergedChildren = $merged->getChildren();
     $this->assertEquals('Portals', $mergedChildren[0]->getName());
     $this->assertEquals('Settings', $mergedChildren[1]->getName());
     $this->assertEquals('Globals', $mergedChildren[2]->getName());
 }