‪TYPO3CMS  ‪main
MappableProcessor.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 
21 
26 {
27  public function ‪resolve(‪Route $route, array &$attributes): bool
28  {
29  $mappers = $this->‪fetchMappers($route, $attributes);
30  if (empty($mappers)) {
31  return true;
32  }
33 
34  $values = [];
35  foreach ($mappers as $variableName => $mapper) {
36  $value = $mapper->resolve(
37  (string)($attributes[$variableName] ?? '')
38  );
39  if ($value === null) {
40  if (!$mapper instanceof ‪UnresolvedValueInterface || !$mapper->‪hasFallbackValue()) {
41  return false;
42  }
43  $value = $mapper->getFallbackValue();
44  }
45  $values[$variableName] = $value;
46  }
47 
48  $attributes = array_merge($attributes, $values);
49  return true;
50  }
51 
52  public function ‪generate(‪Route $route, array &$attributes): bool
53  {
54  $mappers = $this->‪fetchMappers($route, $attributes);
55  if (empty($mappers)) {
56  return true;
57  }
58 
59  $values = [];
60  foreach ($mappers as $variableName => $mapper) {
61  $value = $mapper->generate(
62  (string)($attributes[$variableName] ?? '')
63  );
64  if ($value === null) {
65  return false;
66  }
67  $values[$variableName] = $value;
68  }
69 
70  $attributes = array_merge($attributes, $values);
71  return true;
72  }
73 
77  protected function ‪fetchMappers(‪Route $route, array $attributes, string $type = MappableAspectInterface::class): array
78  {
79  if (empty($attributes)) {
80  return [];
81  }
82  return $route->‪filterAspects([$type], array_keys($attributes));
83  }
84 }
‪TYPO3\CMS\Core\Routing\Aspect\UnresolvedValueInterface\hasFallbackValue
‪hasFallbackValue()
‪TYPO3\CMS\Core\Routing\Aspect\MappableProcessor\fetchMappers
‪MappableAspectInterface[] fetchMappers(Route $route, array $attributes, string $type=MappableAspectInterface::class)
Definition: MappableProcessor.php:77
‪TYPO3\CMS\Core\Routing\Aspect\UnresolvedValueInterface
Definition: UnresolvedValueInterface.php:24
‪TYPO3\CMS\Core\Routing\Aspect\MappableProcessor\resolve
‪resolve(Route $route, array &$attributes)
Definition: MappableProcessor.php:27
‪TYPO3\CMS\Core\Routing\Aspect\MappableProcessor\generate
‪generate(Route $route, array &$attributes)
Definition: MappableProcessor.php:52
‪TYPO3\CMS\Core\Routing\Aspect\MappableAspectInterface
Definition: MappableAspectInterface.php:24
‪TYPO3\CMS\Core\Routing\Aspect\MappableProcessor
Definition: MappableProcessor.php:26
‪TYPO3\CMS\Core\Routing\Aspect
Definition: AspectFactory.php:18
‪TYPO3\CMS\Core\Routing\Route
Definition: Route.php:32
‪TYPO3\CMS\Core\Routing\Route\filterAspects
‪AspectInterface[] filterAspects(array $classNames, array $variableNames=[])
Definition: Route.php:158