‪TYPO3CMS  ‪main
LocaleModifier.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
22 
50 {
52 
56  protected ‪$settings;
57 
61  protected ‪$localeMap;
62 
66  protected ‪$default;
67 
71  public function ‪__construct(array ‪$settings)
72  {
73  ‪$localeMap = ‪$settings['localeMap'] ?? null;
74  ‪$default = ‪$settings['default'] ?? null;
75 
76  if (!is_array(‪$localeMap)) {
77  throw new \InvalidArgumentException('localeMap must be array', 1537277153);
78  }
79  if (!is_string(‪$default ?? '')) {
80  throw new \InvalidArgumentException('default must be string', 1537277154);
81  }
82 
83  $this->settings = ‪$settings;
84  $this->localeMap = ‪$localeMap;
85  $this->default = ‪$default;
86  }
87 
91  public function ‪modify(): ?string
92  {
93  $locale = (string)$this->siteLanguage->getLocale();
94  foreach ($this->localeMap as $item) {
95  $pattern = '#^' . str_replace('_', '-', $item['locale']) . '#i';
96  if (preg_match($pattern, $locale)) {
97  return (string)$item['value'];
98  }
99  }
100  return ‪$this->default;
101  }
102 }
‪TYPO3\CMS\Core\Site\SiteLanguageAwareTrait
Definition: SiteLanguageAwareTrait.php:28
‪TYPO3\CMS\Core\Site\SiteLanguageAwareInterface
Definition: SiteLanguageAwareInterface.php:26
‪TYPO3\CMS\Core\Routing\Aspect\LocaleModifier\__construct
‪__construct(array $settings)
Definition: LocaleModifier.php:68
‪TYPO3\CMS\Core\Routing\Aspect\LocaleModifier\$default
‪string null $default
Definition: LocaleModifier.php:63
‪TYPO3\CMS\Core\Routing\Aspect
Definition: AspectFactory.php:18
‪TYPO3\CMS\Core\Routing\Aspect\LocaleModifier\$settings
‪array $settings
Definition: LocaleModifier.php:55
‪TYPO3\CMS\Core\Routing\Aspect\LocaleModifier\modify
‪modify()
Definition: LocaleModifier.php:88
‪TYPO3\CMS\Core\Routing\Aspect\ModifiableAspectInterface
Definition: ModifiableAspectInterface.php:25
‪TYPO3\CMS\Core\Routing\Aspect\LocaleModifier\$localeMap
‪array $localeMap
Definition: LocaleModifier.php:59
‪TYPO3\CMS\Core\Routing\Aspect\LocaleModifier
Definition: LocaleModifier.php:50