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

toArray() public method

Returns the content of the NavigationItem as array.
public toArray ( ) : array
return array
    public function toArray()
    {
        $array = ['title' => $this->getName(), 'icon' => $this->getIcon(), 'action' => $this->getAction(), 'event' => $this->getEvent(), 'eventArguments' => $this->getEventArguments(), 'hasSettings' => $this->getHasSettings(), 'disabled' => $this->getDisabled(), 'id' => $this->getId() != null ? $this->getId() : str_replace('.', '', uniqid('', true))];
        if ($this->getHeaderIcon() != null || $this->getHeaderTitle() != null) {
            $array['header'] = ['title' => $this->getHeaderTitle(), 'logo' => $this->getHeaderIcon()];
        }
        $children = $this->getChildren();
        usort($children, function (NavigationItem $a, NavigationItem $b) {
            $aPosition = $a->getPosition() ?: PHP_INT_MAX;
            $bPosition = $b->getPosition() ?: PHP_INT_MAX;
            return $aPosition - $bPosition;
        });
        foreach ($children as $key => $child) {
            /* @var NavigationItem $child */
            $array['items'][$key] = $child->toArray();
        }
        return $array;
    }

Usage Example

Ejemplo n.º 1
0
 public function testToArray()
 {
     $array = $this->item1->toArray();
     $this->assertEquals('Root', $array['title']);
     $this->assertEquals('action', $array['action']);
     $this->assertEquals('logo', $array['header']['logo']);
     $this->assertEquals('title', $array['header']['title']);
     $this->assertEquals('Portals', $array['items'][0]['title']);
     $this->assertEquals(null, $array['items'][0]['action']);
     $this->assertEquals('Settings', $array['items'][1]['title']);
     $this->assertEquals(null, $array['items'][1]['action']);
     $array = $this->item2->toArray();
     $this->assertNotContains('header', array_keys($array));
 }