Sulu\Component\Webspace\Webspace::toArray PHP Method

toArray() public method

public toArray ( $depth = null )
    public function toArray($depth = null)
    {
        $res = [];
        $res['key'] = $this->getKey();
        $res['name'] = $this->getName();
        $res['localizations'] = [];
        $res['templates'] = $this->getTemplates();
        $res['defaultTemplates'] = $this->getDefaultTemplates();
        $res['resourceLocator']['strategy'] = $this->getResourceLocatorStrategy();
        foreach ($this->getLocalizations() as $localization) {
            $res['localizations'][] = $localization->toArray();
        }
        $thisSecurity = $this->getSecurity();
        if ($thisSecurity != null) {
            $res['security']['system'] = $thisSecurity->getSystem();
        }
        $res['segments'] = [];
        $segments = $this->getSegments();
        if (!empty($segments)) {
            foreach ($segments as $segment) {
                $res['segments'][] = $segment->toArray();
            }
        }
        $res['theme'] = !$this->theme ? null : $this->theme;
        $res['portals'] = [];
        foreach ($this->getPortals() as $portal) {
            $res['portals'][] = $portal->toArray();
        }
        $res['navigation'] = [];
        $res['navigation']['contexts'] = [];
        if ($navigation = $this->getNavigation()) {
            foreach ($this->getNavigation()->getContexts() as $context) {
                $res['navigation']['contexts'][] = ['key' => $context->getKey(), 'metadata' => $context->getMetadata()];
            }
        }
        return $res;
    }

Usage Example

Example #1
0
 public function testToArray()
 {
     $expected = ['key' => 'foo', 'name' => 'portal_key', 'localizations' => [['fr']], 'security' => ['system' => 'sec_sys'], 'segments' => [['asd']], 'portals' => [['one']], 'theme' => ['dsa'], 'navigation' => ['contexts' => []]];
     $this->security->getSystem()->willReturn($expected['security']['system']);
     $this->localization->toArray()->willReturn($expected['localizations'][0]);
     $this->segment->toArray()->willReturn($expected['segments'][0]);
     $this->theme->toArray()->willReturn($expected['theme']);
     $this->portal->toArray()->willReturn($expected['portals'][0]);
     $this->webspace->setKey($expected['key']);
     $this->webspace->setName($expected['name']);
     $this->webspace->setLocalizations([$this->localization->reveal()]);
     $this->webspace->setSecurity($this->security->reveal());
     $this->webspace->setSegments([$this->segment->reveal()]);
     $this->webspace->setPortals([$this->portal->reveal()]);
     $this->webspace->setTheme($this->theme->reveal());
     $res = $this->webspace->toArray();
     $this->assertEquals($expected, $res);
 }