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