vendor/sulu/sulu/src/Sulu/Component/Localization/Localization.php line 314

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