‪TYPO3CMS  ‪main
StaticValueMapper.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 
55 {
57  use UnresolvedValueTrait;
58 
62  protected ‪$settings;
63 
67  protected ‪$map;
68 
72  protected ‪$localeMap;
73 
77  public function ‪__construct(array ‪$settings)
78  {
79  ‪$map = ‪$settings['map'] ?? null;
80  ‪$localeMap = ‪$settings['localeMap'] ?? [];
81 
82  if (!is_array(‪$map)) {
83  throw new \InvalidArgumentException('map must be array', 1537277143);
84  }
85  if (!is_array(‪$localeMap)) {
86  throw new \InvalidArgumentException('localeMap must be array', 1537277144);
87  }
88 
89  $this->settings = ‪$settings;
90  $this->map = array_map('strval', ‪$map);
91  $this->localeMap = ‪$localeMap;
92  }
93 
97  public function ‪count(): int
98  {
99  return ‪count($this->‪retrieveLocaleMap() ?? $this->map);
100  }
101 
105  public function ‪generate(string $value): ?string
106  {
108  $index = array_search($value, ‪$map, true);
109  return $index !== false ? (string)$index : null;
110  }
111 
115  public function ‪resolve(string $value): ?string
116  {
118  return isset(‪$map[$value]) ? (string)‪$map[$value] : null;
119  }
120 
124  protected function ‪retrieveLocaleMap(): ?array
125  {
126  $locale = (string)$this->siteLanguage->getLocale();
127  foreach ($this->localeMap as $item) {
128  $pattern = '#^' . str_replace('_', '-', $item['locale']) . '#i';
129  if (preg_match($pattern, $locale)) {
130  return array_map('strval', $item['map']);
131  }
132  }
133  return null;
134  }
135 }
‪TYPO3\CMS\Core\Routing\Aspect\UnresolvedValueInterface
Definition: UnresolvedValueInterface.php:24
‪TYPO3\CMS\Core\Routing\Aspect\StaticValueMapper\resolve
‪resolve(string $value)
Definition: StaticValueMapper.php:112
‪TYPO3\CMS\Core\Site\SiteLanguageAwareTrait
Definition: SiteLanguageAwareTrait.php:28
‪TYPO3\CMS\Core\Routing\Aspect\StaticValueMapper\generate
‪generate(string $value)
Definition: StaticValueMapper.php:102
‪TYPO3\CMS\Core\Site\SiteLanguageAwareInterface
Definition: SiteLanguageAwareInterface.php:26
‪TYPO3\CMS\Core\Routing\Aspect\StaticValueMapper\count
‪count()
Definition: StaticValueMapper.php:94
‪TYPO3\CMS\Core\Routing\Aspect\StaticValueMapper\retrieveLocaleMap
‪retrieveLocaleMap()
Definition: StaticValueMapper.php:121
‪TYPO3\CMS\Core\Routing\Aspect\StaticValueMapper
Definition: StaticValueMapper.php:55
‪TYPO3\CMS\Core\Routing\Aspect\StaticValueMapper\$localeMap
‪array $localeMap
Definition: StaticValueMapper.php:69
‪TYPO3\CMS\Core\Routing\Aspect\StaticValueMapper\__construct
‪__construct(array $settings)
Definition: StaticValueMapper.php:74
‪TYPO3\CMS\Core\Routing\Aspect
Definition: AspectFactory.php:18
‪TYPO3\CMS\Core\Routing\Aspect\StaticMappableAspectInterface
Definition: StaticMappableAspectInterface.php:23
‪TYPO3\CMS\Core\Routing\Aspect\StaticValueMapper\$map
‪array $map
Definition: StaticValueMapper.php:65
‪TYPO3\CMS\Core\Routing\Aspect\StaticValueMapper\$settings
‪array $settings
Definition: StaticValueMapper.php:61