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