vendor/sulu/sulu/src/Sulu/Component/Localization/Localization.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\Localization;
  11. use JMS\Serializer\Annotation\Groups;
  12. use JMS\Serializer\Annotation\VirtualProperty;
  13. use Sulu\Component\Util\ArrayableInterface;
  14. /**
  15.  * Represents a localization of a webspace definition.
  16.  */
  17. class Localization implements \JsonSerializableArrayableInterface
  18. {
  19.     public const UNDERSCORE 'de_at';
  20.     public const DASH 'de-at';
  21.     public const ISO6391 'de-AT';
  22.     public const LCID 'de_AT';
  23.     /**
  24.      * Create an instance of localization for given locale.
  25.      *
  26.      * @param string $locale
  27.      * @param string $format
  28.      *
  29.      * @return Localization
  30.      */
  31.     public static function createFromString($locale$format self::UNDERSCORE)
  32.     {
  33.         $delimiter '-';
  34.         if (\in_array($format, [self::UNDERSCOREself::LCID])) {
  35.             $delimiter '_';
  36.         }
  37.         $parts = \explode($delimiter$locale);
  38.         $localization = new self();
  39.         $localization->setLanguage(\strtolower($parts[0]));
  40.         if (\count($parts) > 1) {
  41.             $localization->setCountry(\strtolower($parts[1]));
  42.         }
  43.         return $localization;
  44.     }
  45.     /**
  46.      * The language of the localization.
  47.      *
  48.      * @var string
  49.      *
  50.      * @Groups({"frontend", "Default"})
  51.      */
  52.     private $language;
  53.     /**
  54.      * The country of the localization.
  55.      *
  56.      * @var string
  57.      *
  58.      * @Groups({"frontend", "Default"})
  59.      */
  60.     private $country;
  61.     /**
  62.      * Defines how the generation of shadow pages should be handled.
  63.      *
  64.      * @var string
  65.      *
  66.      * @Groups({"frontend", "Default"})
  67.      */
  68.     private $shadow;
  69.     /**
  70.      * The sub localizations of this one.
  71.      *
  72.      * @var Localization[]
  73.      *
  74.      * @Groups({"frontend", "Default"})
  75.      */
  76.     private $children;
  77.     /**
  78.      * The parent localization.
  79.      *
  80.      * @var Localization
  81.      *
  82.      * @Groups({"frontend", "Default"})
  83.      */
  84.     private $parent;
  85.     /**
  86.      * Defines whether this localization is the default one or not.
  87.      *
  88.      * @var bool
  89.      *
  90.      * @Groups({"frontend", "Default"})
  91.      */
  92.     private $default;
  93.     /**
  94.      * Defines whether this localization is the x-default one or not.
  95.      * This will be used to determine the default hreflang tag.
  96.      *
  97.      * @var bool
  98.      *
  99.      * @Groups({"frontend", "Default"})
  100.      *
  101.      * @deprecated use $default instead
  102.      */
  103.     private $xDefault;
  104.     public function __construct($language null$country null)
  105.     {
  106.         $this->language $language;
  107.         $this->country $country;
  108.     }
  109.     /**
  110.      * Sets the country of this localization.
  111.      *
  112.      * @param string $country
  113.      */
  114.     public function setCountry($country)
  115.     {
  116.         $this->country $country;
  117.     }
  118.     /**
  119.      * Returns the country of this localization.
  120.      *
  121.      * @return string
  122.      */
  123.     public function getCountry()
  124.     {
  125.         return $this->country;
  126.     }
  127.     /**
  128.      * Sets the language of this localization.
  129.      *
  130.      * @param string $language
  131.      */
  132.     public function setLanguage($language)
  133.     {
  134.         $this->language $language;
  135.     }
  136.     /**
  137.      * Returns the language of this localization.
  138.      *
  139.      * @return string
  140.      */
  141.     public function getLanguage()
  142.     {
  143.         return $this->language;
  144.     }
  145.     /**
  146.      * Sets how to handle shadow pages for this localization.
  147.      *
  148.      * @param string $shadow
  149.      */
  150.     public function setShadow($shadow)
  151.     {
  152.         $this->shadow $shadow;
  153.     }
  154.     /**
  155.      * Returns how to handle shadow pages for this localization.
  156.      *
  157.      * @return string
  158.      */
  159.     public function getShadow()
  160.     {
  161.         return $this->shadow;
  162.     }
  163.     /**
  164.      * Adds a new child localization.
  165.      */
  166.     public function addChild(self $child)
  167.     {
  168.         $this->children[] = $child;
  169.     }
  170.     /**
  171.      * Sets the children of the localization.
  172.      *
  173.      * @param Localization[] $children
  174.      */
  175.     public function setChildren($children)
  176.     {
  177.         $this->children $children;
  178.     }
  179.     /**
  180.      * Returns the children of the localization.
  181.      *
  182.      * @return Localization[]
  183.      */
  184.     public function getChildren()
  185.     {
  186.         return $this->children;
  187.     }
  188.     /**
  189.      * Returns the localization code, which is a combination of the language and the country.
  190.      *
  191.      * @param string $delimiter between language and country
  192.      *
  193.      * @return string
  194.      *
  195.      * @VirtualProperty
  196.      * @Groups({"frontend", "Default"})
  197.      *
  198.      * @deprecated use getLocale instead
  199.      */
  200.     public function getLocalization($delimiter '_')
  201.     {
  202.         @trigger_deprecation('sulu/sulu''1.2'__METHOD__ '() is deprecated and will be removed in 2.0. Use getLocale() instead.');
  203.         $localization $this->getLanguage();
  204.         if (null != $this->getCountry()) {
  205.             $localization .= $delimiter $this->getCountry();
  206.         }
  207.         return $localization;
  208.     }
  209.     /**
  210.      * Returns the localization code, which is a combination of the language and the country in a specific format.
  211.      *
  212.      * @param string $format requested localization format
  213.      *
  214.      * @return string
  215.      *
  216.      * @VirtualProperty
  217.      * @Groups({"frontend", "Default"})
  218.      */
  219.     public function getLocale($format self::UNDERSCORE)
  220.     {
  221.         $localization = \strtolower($this->getLanguage());
  222.         if (null != $this->getCountry()) {
  223.             $country = \strtolower($this->getCountry());
  224.             $delimiter '-';
  225.             switch ($format) {
  226.                 case self::UNDERSCORE:
  227.                     $delimiter '_';
  228.                     break;
  229.                 case self::ISO6391:
  230.                     $country = \strtoupper($country);
  231.                     break;
  232.                 case self::LCID:
  233.                     $delimiter '_';
  234.                     $country = \strtoupper($country);
  235.                     break;
  236.             }
  237.             $localization .= $delimiter $country;
  238.         }
  239.         return $localization;
  240.     }
  241.     /**
  242.      * Sets the parent of this localization.
  243.      */
  244.     public function setParent(self $parent)
  245.     {
  246.         $this->parent $parent;
  247.     }
  248.     /**
  249.      * Returns the parent of this localization.
  250.      *
  251.      * @return Localization
  252.      */
  253.     public function getParent()
  254.     {
  255.         return $this->parent;
  256.     }
  257.     /**
  258.      * Sets if this localization is the default one.
  259.      *
  260.      * @param bool $default
  261.      */
  262.     public function setDefault($default)
  263.     {
  264.         $this->default $default;
  265.     }
  266.     /**
  267.      * Sets if this localization is the x-default one.
  268.      *
  269.      * @param bool $xDefault
  270.      *
  271.      * @deprecated use setDefault to set the default Localization
  272.      */
  273.     public function setXDefault($xDefault)
  274.     {
  275.         @trigger_deprecation('sulu/sulu''2.3''The "%s" method is deprecated on "%s" use "setDefault" instead.'__METHOD____CLASS__);
  276.         $this->xDefault $xDefault;
  277.     }
  278.     /**
  279.      * Returns if this localization is the default one.
  280.      *
  281.      * @return bool True if this is the default localization, otherwise false
  282.      */
  283.     public function isDefault()
  284.     {
  285.         return $this->default;
  286.     }
  287.     /**
  288.      * Returns if this localization is the x-default one.
  289.      *
  290.      * @return bool True if this is the x-default localization, otherwise false
  291.      *
  292.      * @deprecated use getDefault to get the default Localization
  293.      */
  294.     public function isXDefault()
  295.     {
  296.         if (\func_num_args() < || \func_get_arg(0)) {
  297.             @trigger_deprecation('sulu/sulu''2.4''The "%s" method is deprecated on "%s" use "isDefault" instead.'__METHOD____CLASS__);
  298.         }
  299.         return $this->xDefault;
  300.     }
  301.     /**
  302.      * @param string $localization
  303.      *
  304.      * @return Localization|null
  305.      */
  306.     public function findLocalization($localization)
  307.     {
  308.         if ($this->getLocale() == $localization) {
  309.             return $this;
  310.         }
  311.         $children $this->getChildren();
  312.         if (!empty($children)) {
  313.             foreach ($children as $childLocalization) {
  314.                 $result $childLocalization->findLocalization($localization);
  315.                 if ($result) {
  316.                     return $result;
  317.                 }
  318.             }
  319.         }
  320.         return;
  321.     }
  322.     /**
  323.      * Returns a list of all localizations and sublocalizations.
  324.      *
  325.      * @return Localization[]
  326.      */
  327.     public function getAllLocalizations()
  328.     {
  329.         $localizations = [];
  330.         if (null !== $this->getChildren() && \count($this->getChildren()) > 0) {
  331.             foreach ($this->getChildren() as $child) {
  332.                 $localizations[] = $child;
  333.                 $localizations = \array_merge($localizations$child->getAllLocalizations());
  334.             }
  335.         }
  336.         return $localizations;
  337.     }
  338.     /**
  339.      * @return string
  340.      */
  341.     public function __toString()
  342.     {
  343.         return $this->getLocale();
  344.     }
  345.     #[\ReturnTypeWillChange]
  346.     public function jsonSerialize()
  347.     {
  348.         return [
  349.             'localization' => $this->getLocale(),
  350.             'name' => $this->getLocale(),
  351.         ];
  352.     }
  353.     public function toArray($depth null)
  354.     {
  355.         $res = [];
  356.         $res['country'] = $this->getCountry();
  357.         $res['language'] = $this->getLanguage();
  358.         $res['localization'] = $this->getLocale();
  359.         $res['default'] = $this->isDefault();
  360.         $res['xDefault'] = $this->isXDefault(false);
  361.         $res['children'] = [];
  362.         $children $this->getChildren();
  363.         if ($children) {
  364.             foreach ($this->getChildren() as $childLocalization) {
  365.                 $res['children'][] = $childLocalization->toArray(null);
  366.             }
  367.         }
  368.         $res['shadow'] = $this->getShadow();
  369.         return $res;
  370.     }
  371. }