‪TYPO3CMS  11.5
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 
61  protected ‪$settings;
62 
66  protected ‪$map;
67 
71  protected ‪$localeMap;
72 
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 
126  protected function ‪retrieveLocaleMap(): ?array
127  {
128  $locale = $this->siteLanguage->getLocale();
129  foreach ($this->localeMap as $item) {
130  $pattern = '#^' . $item['locale'] . '#i';
131  if (preg_match($pattern, $locale)) {
132  return array_map('strval', $item['map']);
133  }
134  }
135  return null;
136  }
137 }
‪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
Definition: StaticValueMapper.php:55
‪TYPO3\CMS\Core\Routing\Aspect\StaticValueMapper\$localeMap
‪array $localeMap
Definition: StaticValueMapper.php:68
‪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:64
‪TYPO3\CMS\Core\Routing\Aspect\StaticValueMapper\$settings
‪array $settings
Definition: StaticValueMapper.php:60
‪TYPO3\CMS\Core\Routing\Aspect\StaticValueMapper\retrieveLocaleMap
‪array null retrieveLocaleMap()
Definition: StaticValueMapper.php:123