vendor/sulu/sulu/src/Sulu/Component/Webspace/Portal.php line 21

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Component\Webspace;
  11. use Sulu\Component\Localization\Localization;
  12. use Sulu\Component\Webspace\Exception\EnvironmentNotFoundException;
  13. use Sulu\Component\Webspace\Exception\PortalLocalizationNotFoundException;
  14. /**
  15.  * Container for a portal configuration.
  16.  */
  17. class Portal
  18. {
  19.     /**
  20.      * The name of the portal.
  21.      *
  22.      * @var string
  23.      */
  24.     private $name;
  25.     /**
  26.      * The key of the portal.
  27.      *
  28.      * @var string
  29.      */
  30.     private $key;
  31.     /**
  32.      * An array of localizations.
  33.      *
  34.      * @var Localization[]
  35.      */
  36.     private $localizations;
  37.     /**
  38.      * The default localization for this portal.
  39.      *
  40.      * @var Localization
  41.      */
  42.     private $defaultLocalization;
  43.     /**
  44.      * The x-default localization for this portal.
  45.      *
  46.      * @deprecated use $defaultLocalization instead
  47.      *
  48.      * @var Localization
  49.      */
  50.     private $xDefaultLocalization;
  51.     /**
  52.      * @var Environment[]
  53.      */
  54.     private $environments;
  55.     /**
  56.      * @var Webspace
  57.      */
  58.     private $webspace;
  59.     /**
  60.      * Sets the name of the portal.
  61.      *
  62.      * @param string $name The name of the portal
  63.      */
  64.     public function setName($name)
  65.     {
  66.         $this->name $name;
  67.     }
  68.     /**
  69.      * Returns the name of the portal.
  70.      *
  71.      * @return string The name of the portal
  72.      */
  73.     public function getName()
  74.     {
  75.         return $this->name;
  76.     }
  77.     /**
  78.      * @param string $key
  79.      */
  80.     public function setKey($key)
  81.     {
  82.         $this->key $key;
  83.     }
  84.     /**
  85.      * @return string
  86.      */
  87.     public function getKey()
  88.     {
  89.         return $this->key;
  90.     }
  91.     /**
  92.      * Adds the given language to the portal.
  93.      */
  94.     public function addLocalization(Localization $localization)
  95.     {
  96.         $this->localizations[] = $localization;
  97.         if ($localization->isDefault()) {
  98.             $this->setDefaultLocalization($localization);
  99.         }
  100.         if ($localization->isXDefault(false)) {
  101.             $this->setXDefaultLocalization($localizationfalse);
  102.         }
  103.     }
  104.     /**
  105.      * Sets the localizations to this portal.
  106.      *
  107.      * @param Localization[] $localizations
  108.      */
  109.     public function setLocalizations($localizations)
  110.     {
  111.         $this->localizations $localizations;
  112.     }
  113.     /**
  114.      * Returns the languages of this portal.
  115.      *
  116.      * @return Localization[] The languages of this portal
  117.      */
  118.     public function getLocalizations()
  119.     {
  120.         return $this->localizations;
  121.     }
  122.     public function getLocalization($locale)
  123.     {
  124.         foreach ($this->getLocalizations() as $localization) {
  125.             if ($locale === $localization->getLocale()) {
  126.                 return $localization;
  127.             }
  128.         }
  129.         throw new PortalLocalizationNotFoundException($this$locale);
  130.     }
  131.     /**
  132.      * @param Localization $defaultLocalization
  133.      */
  134.     public function setDefaultLocalization($defaultLocalization)
  135.     {
  136.         $this->defaultLocalization $defaultLocalization;
  137.         if (!$this->getXDefaultLocalization()) {
  138.             $this->setXDefaultLocalization($defaultLocalization);
  139.         }
  140.     }
  141.     /**
  142.      * @return Localization
  143.      */
  144.     public function getDefaultLocalization()
  145.     {
  146.         return $this->defaultLocalization;
  147.     }
  148.     /**
  149.      * @param Localization $xDefaultLocalization
  150.      *
  151.      * @deprecated use setDefaultLocalization instead
  152.      */
  153.     public function setXDefaultLocalization($xDefaultLocalization)
  154.     {
  155.         if (\func_num_args() < || \func_get_arg(1)) {
  156.             @\trigger_error(\sprintf('The "%s" method is deprecated on "%s" use "setDefaultLocalization" instead.'__METHOD____CLASS__), \E_USER_DEPRECATED);
  157.         }
  158.         $this->xDefaultLocalization $xDefaultLocalization;
  159.     }
  160.     /**
  161.      * @return Localization
  162.      *
  163.      * @deprecated use getDefaultLocalization instead
  164.      */
  165.     public function getXDefaultLocalization()
  166.     {
  167.         @\trigger_error(\sprintf('The "%s" method is deprecated on "%s" use "getDefaultLocalization" instead.'__METHOD____CLASS__), \E_USER_DEPRECATED);
  168.         return $this->xDefaultLocalization;
  169.     }
  170.     /**
  171.      * Adds an environment to this portal.
  172.      *
  173.      * @param Environment $environment Environment The environment to add
  174.      */
  175.     public function addEnvironment($environment)
  176.     {
  177.         $this->environments[$environment->getType()] = $environment;
  178.     }
  179.     /**
  180.      * Sets the environments for this portal.
  181.      *
  182.      * @param Environment[] $environments
  183.      */
  184.     public function setEnvironments(array $environments)
  185.     {
  186.         $this->environments = [];
  187.         foreach ($environments as $environment) {
  188.             $this->addEnvironment($environment);
  189.         }
  190.     }
  191.     /**
  192.      * Returns the environment for this portal.
  193.      *
  194.      * @return Environment[]
  195.      */
  196.     public function getEnvironments()
  197.     {
  198.         return $this->environments;
  199.     }
  200.     /**
  201.      * Returns the environment with the given type, and throws an exception if the environment does not exist.
  202.      *
  203.      * @param string $type
  204.      *
  205.      * @throws Exception\EnvironmentNotFoundException
  206.      *
  207.      * @return Environment
  208.      */
  209.     public function getEnvironment($type)
  210.     {
  211.         if (!isset($this->environments[$type])) {
  212.             throw new EnvironmentNotFoundException($this$type);
  213.         }
  214.         return $this->environments[$type];
  215.     }
  216.     public function setWebspace(Webspace $webspace)
  217.     {
  218.         $this->webspace $webspace;
  219.     }
  220.     /**
  221.      * @return Webspace
  222.      */
  223.     public function getWebspace()
  224.     {
  225.         return $this->webspace;
  226.     }
  227.     public function toArray($depth null)
  228.     {
  229.         $res = [];
  230.         $res['name'] = $this->getName();
  231.         $res['key'] = $this->getKey();
  232.         $res['localizations'] = [];
  233.         foreach ($this->getLocalizations() as $localization) {
  234.             $res['localizations'][] = $localization->toArray();
  235.         }
  236.         foreach ($this->getEnvironments() as $environment) {
  237.             $res['environments'][] = $environment->toArray();
  238.         }
  239.         return $res;
  240.     }
  241. }