vendor/sulu/sulu/src/Sulu/Component/Webspace/PortalInformation.php line 20

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\Util\ArrayableInterface;
  13. /**
  14.  * This class represents the information for a given URL.
  15.  */
  16. class PortalInformation implements ArrayableInterface
  17. {
  18.     /**
  19.      * The type of the match.
  20.      *
  21.      * @var int
  22.      */
  23.     private $type;
  24.     /**
  25.      * The webspace for this portal information.
  26.      *
  27.      * @var Webspace
  28.      */
  29.     private $webspace;
  30.     /**
  31.      * The portal for this portal information.
  32.      *
  33.      * @var Portal
  34.      */
  35.     private $portal;
  36.     /**
  37.      * The localization for this portal information.
  38.      *
  39.      * @var Localization
  40.      */
  41.     private $localization;
  42.     /**
  43.      * The segment for this portal information.
  44.      *
  45.      * @deprecated Is not set anymore since https://github.com/sulu/sulu/pull/5277
  46.      *
  47.      * @var Segment
  48.      */
  49.     private $segment;
  50.     /**
  51.      * The url for this portal information.
  52.      *
  53.      * @var string
  54.      */
  55.     private $url;
  56.     /**
  57.      * @var string The url to redirect to
  58.      */
  59.     private $redirect;
  60.     /**
  61.      * @var bool
  62.      */
  63.     private $main;
  64.     /**
  65.      * @var string
  66.      */
  67.     private $urlExpression;
  68.     /**
  69.      * @var int
  70.      */
  71.     private $priority;
  72.     public function __construct(
  73.         $type,
  74.         ?Webspace $webspace null,
  75.         ?Portal $portal null,
  76.         ?Localization $localization null,
  77.         $url null,
  78.         ?Segment $segment null,
  79.         $redirect null,
  80.         $main false,
  81.         $urlExpression null,
  82.         $priority 0
  83.     ) {
  84.         $this->setType($type);
  85.         $this->setWebspace($webspace);
  86.         $this->setPortal($portal);
  87.         $this->setLocalization($localization);
  88.         $this->setUrl($url);
  89.         $this->setRedirect($redirect);
  90.         $this->setMain($main);
  91.         $this->setUrlExpression($urlExpression);
  92.         $this->setPriority($priority);
  93.         if ($segment) {
  94.             $this->setSegment($segment);
  95.         }
  96.     }
  97.     /**
  98.      * Sets the localization for this PortalInformation.
  99.      *
  100.      * @param Localization $localization
  101.      */
  102.     public function setLocalization($localization)
  103.     {
  104.         $this->localization $localization;
  105.     }
  106.     /**
  107.      * Returns the localization for this PortalInformation.
  108.      *
  109.      * @return Localization
  110.      */
  111.     public function getLocalization()
  112.     {
  113.         return $this->localization;
  114.     }
  115.     /**
  116.      * Returns the localization for this PortalInformation.
  117.      *
  118.      * @return string
  119.      */
  120.     public function getLocale()
  121.     {
  122.         if (null === $this->localization) {
  123.             return;
  124.         }
  125.         return $this->localization->getLocale();
  126.     }
  127.     /**
  128.      * Sets the portal for this PortalInformation.
  129.      *
  130.      * @param \Sulu\Component\Webspace\Portal $portal
  131.      */
  132.     public function setPortal($portal)
  133.     {
  134.         $this->portal $portal;
  135.     }
  136.     /**
  137.      * Returns the portal for this PortalInformation.
  138.      *
  139.      * @return \Sulu\Component\Webspace\Portal
  140.      */
  141.     public function getPortal()
  142.     {
  143.         return $this->portal;
  144.     }
  145.     /**
  146.      * Returns key of portal.
  147.      */
  148.     public function getPortalKey()
  149.     {
  150.         if (null === $this->portal) {
  151.             return;
  152.         }
  153.         return $this->portal->getKey();
  154.     }
  155.     /**
  156.      * Sets the redirect for the PortalInformation.
  157.      *
  158.      * @param string $redirect
  159.      */
  160.     public function setRedirect($redirect)
  161.     {
  162.         $this->redirect $redirect;
  163.     }
  164.     /**
  165.      * Returns the redirect for the PortalInformation.
  166.      *
  167.      * @return string
  168.      */
  169.     public function getRedirect()
  170.     {
  171.         return $this->redirect;
  172.     }
  173.     /**
  174.      * Sets the segment for the PortalInformation.
  175.      *
  176.      * @deprecated Is not set anymore since https://github.com/sulu/sulu/pull/5277
  177.      *
  178.      * @param \Sulu\Component\Webspace\Segment $segment
  179.      */
  180.     public function setSegment($segment)
  181.     {
  182.         @trigger_deprecation(
  183.             'sulu/sulu',
  184.             '2.2',
  185.             'Segment on the PortalInformation will be removed and should not be used anymore.'
  186.         );
  187.         $this->segment $segment;
  188.     }
  189.     /**
  190.      * Returns the segment for the PortalInformation.
  191.      *
  192.      * @deprecated Is not set anymore since https://github.com/sulu/sulu/pull/5277
  193.      *
  194.      * @return \Sulu\Component\Webspace\Segment
  195.      */
  196.     public function getSegment()
  197.     {
  198.         @trigger_deprecation(
  199.             'sulu/sulu',
  200.             '2.2',
  201.             'Segment on the PortalInformation will be removed and should not be used anymore.'
  202.         );
  203.         return $this->segment;
  204.     }
  205.     /**
  206.      * @deprecated Is not set anymore since https://github.com/sulu/sulu/pull/5277
  207.      */
  208.     public function getSegmentKey()
  209.     {
  210.         @trigger_deprecation(
  211.             'sulu/sulu',
  212.             '2.2',
  213.             'Segment on the PortalInformation will be removed and should not be used anymore.'
  214.         );
  215.         return $this->segment $this->segment->getKey() : null;
  216.     }
  217.     /**
  218.      * Sets the match type of this PortalInformation.
  219.      *
  220.      * @param int $type
  221.      */
  222.     public function setType($type)
  223.     {
  224.         $this->type $type;
  225.     }
  226.     /**
  227.      * Returns the match type of this PortalInformation.
  228.      *
  229.      * @return int
  230.      */
  231.     public function getType()
  232.     {
  233.         return $this->type;
  234.     }
  235.     /**
  236.      * Sets the URL of this Portalinformation.
  237.      *
  238.      * @param string $url
  239.      */
  240.     public function setUrl($url)
  241.     {
  242.         $this->url $url;
  243.     }
  244.     /**
  245.      * Returns the URL of this Portalinformation.
  246.      *
  247.      * @return string
  248.      */
  249.     public function getUrl()
  250.     {
  251.         return $this->url;
  252.     }
  253.     /**
  254.      * Returns the host including the domain for the PortalInformation.
  255.      *
  256.      * @return string
  257.      */
  258.     public function getHost()
  259.     {
  260.         return \substr($this->url0$this->getHostLength());
  261.     }
  262.     /**
  263.      * Returns the prefix (the url without the host) for this PortalInformation.
  264.      *
  265.      * @return string
  266.      */
  267.     public function getPrefix()
  268.     {
  269.         $prefix = \substr($this->url$this->getHostLength() + 1);
  270.         return $prefix $prefix '/' null;
  271.     }
  272.     /**
  273.      * Sets the webspace for this PortalInformation.
  274.      *
  275.      * @param \Sulu\Component\Webspace\Webspace $webspace
  276.      */
  277.     public function setWebspace($webspace)
  278.     {
  279.         $this->webspace $webspace;
  280.     }
  281.     /**
  282.      * Returns the webspace for this PortalInformation.
  283.      *
  284.      * @return \Sulu\Component\Webspace\Webspace
  285.      */
  286.     public function getWebspace()
  287.     {
  288.         return $this->webspace;
  289.     }
  290.     /**
  291.      * Returns key of webspace.
  292.      */
  293.     public function getWebspaceKey()
  294.     {
  295.         if (null === $this->webspace) {
  296.             return;
  297.         }
  298.         return $this->webspace->getKey();
  299.     }
  300.     /**
  301.      * Returns true if url is main.
  302.      *
  303.      * @return bool
  304.      */
  305.     public function isMain()
  306.     {
  307.         return $this->main;
  308.     }
  309.     /**
  310.      * Sets true if url is main.
  311.      *
  312.      * @param bool $main
  313.      */
  314.     public function setMain($main)
  315.     {
  316.         $this->main $main;
  317.     }
  318.     /**
  319.      * Returns expression for url.
  320.      *
  321.      * @return string
  322.      */
  323.     public function getUrlExpression()
  324.     {
  325.         return $this->urlExpression;
  326.     }
  327.     /**
  328.      * Sets expression for url.
  329.      *
  330.      * @param string $urlExpression
  331.      */
  332.     public function setUrlExpression($urlExpression)
  333.     {
  334.         $this->urlExpression $urlExpression;
  335.     }
  336.     /**
  337.      * Calculate the length of the host part of the URL.
  338.      *
  339.      * @return int
  340.      */
  341.     private function getHostLength()
  342.     {
  343.         $hostLength = \strpos($this->url'/');
  344.         $hostLength = (false === $hostLength) ? \strlen($this->url) : $hostLength;
  345.         return $hostLength;
  346.     }
  347.     /**
  348.      * @return int
  349.      */
  350.     public function getPriority()
  351.     {
  352.         return $this->priority;
  353.     }
  354.     /**
  355.      * @param int $priority
  356.      */
  357.     public function setPriority($priority)
  358.     {
  359.         $this->priority $priority;
  360.     }
  361.     public function toArray($depth null)
  362.     {
  363.         $result = [];
  364.         $result['type'] = $this->getType();
  365.         $result['webspace'] = $this->getWebspace()->getKey();
  366.         $result['url'] = $this->getUrl();
  367.         $result['main'] = $this->isMain();
  368.         $result['priority'] = $this->getPriority();
  369.         $portal $this->getPortal();
  370.         if ($portal) {
  371.             $result['portal'] = $portal->getKey();
  372.         }
  373.         $localization $this->getLocalization();
  374.         if ($localization) {
  375.             $result['localization'] = $localization->toArray();
  376.         }
  377.         $result['redirect'] = $this->getRedirect();
  378.         $segment $this->getSegment();
  379.         if ($segment) {
  380.             $result['segment'] = $segment->getKey();
  381.         }
  382.         $urlExpression $this->getUrlExpression();
  383.         if ($urlExpression) {
  384.             $result['urlExpression'] = $urlExpression;
  385.         }
  386.         return $result;
  387.     }
  388. }