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

  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.      *
  197.      * @Groups({"frontend", "Default"})
  198.      *
  199.      * @deprecated use getLocale instead
  200.      */
  201.     public function getLocalization($delimiter '_')
  202.     {
  203.         @\trigger_error(__METHOD__ '() is deprecated since version 1.2 and will be removed in 2.0. Use getLocale() instead.'\E_USER_DEPRECATED);
  204.         $localization $this->getLanguage();
  205.         if (null != $this->getCountry()) {
  206.             $localization .= $delimiter $this->getCountry();
  207.         }
  208.         return $localization;
  209.     }
  210.     /**
  211.      * Returns the localization code, which is a combination of the language and the country in a specific format.
  212.      *
  213.      * @param string $format requested localization format
  214.      *
  215.      * @return string
  216.      *
  217.      * @VirtualProperty
  218.      *
  219.      * @Groups({"frontend", "Default"})
  220.      */
  221.     public function getLocale($format self::UNDERSCORE)
  222.     {
  223.         $localization \strtolower($this->getLanguage());
  224.         if (null != $this->getCountry()) {
  225.             $country \strtolower($this->getCountry());
  226.             $delimiter '-';
  227.             switch ($format) {
  228.                 case self::UNDERSCORE:
  229.                     $delimiter '_';
  230.                     break;
  231.                 case self::ISO6391:
  232.                     $country \strtoupper($country);
  233.                     break;
  234.                 case self::LCID:
  235.                     $delimiter '_';
  236.                     $country \strtoupper($country);
  237.                     break;
  238.             }
  239.             $localization .= $delimiter $country;
  240.         }
  241.         return $localization;
  242.     }
  243.     /**
  244.      * Sets the parent of this localization.
  245.      */
  246.     public function setParent(self $parent)
  247.     {
  248.         $this->parent $parent;
  249.     }
  250.     /**
  251.      * Returns the parent of this localization.
  252.      *
  253.      * @return Localization
  254.      */
  255.     public function getParent()
  256.     {
  257.         return $this->parent;
  258.     }
  259.     /**
  260.      * Sets if this localization is the default one.
  261.      *
  262.      * @param bool $default
  263.      */
  264.     public function setDefault($default)
  265.     {
  266.         $this->default $default;
  267.     }
  268.     /**
  269.      * Sets if this localization is the x-default one.
  270.      *
  271.      * @param bool $xDefault
  272.      *
  273.      * @deprecated use setDefault to set the default Localization
  274.      */
  275.     public function setXDefault($xDefault)
  276.     {
  277.         @\trigger_error(\sprintf('The "%s" method is deprecated on "%s" use "setDefault" instead.'__METHOD____CLASS__), \E_USER_DEPRECATED);
  278.         $this->xDefault $xDefault;
  279.     }
  280.     /**
  281.      * Returns if this localization is the default one.
  282.      *
  283.      * @return bool True if this is the default localization, otherwise false
  284.      */
  285.     public function isDefault()
  286.     {
  287.         return $this->default;
  288.     }
  289.     /**
  290.      * Returns if this localization is the x-default one.
  291.      *
  292.      * @return bool True if this is the x-default localization, otherwise false
  293.      *
  294.      * @deprecated use getDefault to get the default Localization
  295.      */
  296.     public function isXDefault()
  297.     {
  298.         if (\func_num_args() < || \func_get_arg(0)) {
  299.             @\trigger_error(\sprintf('The "%s" method is deprecated on "%s" use "isDefault" instead.'__METHOD____CLASS__), \E_USER_DEPRECATED);
  300.         }
  301.         return $this->xDefault;
  302.     }
  303.     /**
  304.      * @param string $localization
  305.      *
  306.      * @return Localization|null
  307.      */
  308.     public function findLocalization($localization)
  309.     {
  310.         if ($this->getLocale() == $localization) {
  311.             return $this;
  312.         }
  313.         $children $this->getChildren();
  314.         if (!empty($children)) {
  315.             foreach ($children as $childLocalization) {
  316.                 $result $childLocalization->findLocalization($localization);
  317.                 if ($result) {
  318.                     return $result;
  319.                 }
  320.             }
  321.         }
  322.         return;
  323.     }
  324.     /**
  325.      * Returns a list of all localizations and sublocalizations.
  326.      *
  327.      * @return Localization[]
  328.      */
  329.     public function getAllLocalizations()
  330.     {
  331.         $localizations = [];
  332.         if (null !== $this->getChildren() && \count($this->getChildren()) > 0) {
  333.             foreach ($this->getChildren() as $child) {
  334.                 $localizations[] = $child;
  335.                 $localizations \array_merge($localizations$child->getAllLocalizations());
  336.             }
  337.         }
  338.         return $localizations;
  339.     }
  340.     /**
  341.      * @return string
  342.      */
  343.     public function __toString()
  344.     {
  345.         return $this->getLocale();
  346.     }
  347.     #[\ReturnTypeWillChange]
  348.     public function jsonSerialize()
  349.     {
  350.         return [
  351.             'localization' => $this->getLocale(),
  352.             'name' => $this->getLocale(),
  353.         ];
  354.     }
  355.     public function toArray($depth null)
  356.     {
  357.         $res = [];
  358.         $res['country'] = $this->getCountry();
  359.         $res['language'] = $this->getLanguage();
  360.         $res['localization'] = $this->getLocale();
  361.         $res['default'] = $this->isDefault();
  362.         $res['xDefault'] = $this->isXDefault(false);
  363.         $res['children'] = [];
  364.         $children $this->getChildren();
  365.         if ($children) {
  366.             foreach ($this->getChildren() as $childLocalization) {
  367.                 $res['children'][] = $childLocalization->toArray(null);
  368.             }
  369.         }
  370.         $res['shadow'] = $this->getShadow();
  371.         return $res;
  372.     }
  373. }