‪TYPO3CMS  9.5
StaticRangeMapper.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
45 {
49  protected ‪$settings;
50 
54  protected ‪$start;
55 
59  protected ‪$end;
60 
64  protected ‪$range;
65 
70  public function ‪__construct(array ‪$settings)
71  {
72  ‪$start = ‪$settings['start'] ?? null;
73  ‪$end = ‪$settings['end'] ?? null;
74 
75  if (!is_string(‪$start)) {
76  throw new \InvalidArgumentException('start must be string', 1537277163);
77  }
78  if (!is_string(‪$end)) {
79  throw new \InvalidArgumentException('end must be string', 1537277164);
80  }
81 
82  $this->settings = ‪$settings;
83  $this->start = ‪$start;
84  $this->end = ‪$end;
85  $this->range = $this->‪applyNumericPrefix($this->‪buildRange());
86  }
87 
91  public function ‪count(): int
92  {
93  return ‪count($this->range);
94  }
95 
99  public function ‪generate(string $value): ?string
100  {
101  return $this->‪respondWhenInRange($value);
102  }
103 
107  public function ‪resolve(string $value): ?string
108  {
109  return $this->‪respondWhenInRange($value);
110  }
111 
116  protected function ‪respondWhenInRange(string $value): ?string
117  {
118  if (in_array($value, $this->range, true)) {
119  return $value;
120  }
121  return null;
122  }
123 
136  protected function ‪buildRange(): array
137  {
138  ‪$range = array_map('strval', range($this->start, $this->end));
139  if (‪count(‪$range) > 1000) {
140  throw new \LengthException(
141  'Range is larger than 1000 items',
142  1537696771
143  );
144  }
145  return ‪$range;
146  }
147 
152  protected function ‪applyNumericPrefix(array ‪$range): array
153  {
154  if (!preg_match('#^\d+$#', $this->start)
155  || !preg_match('#^\d+$#', $this->end)
156  || $this->start === '0' || $this->end === '0'
157  || $this->start[0] !== '0' && $this->end[0] !== '0'
158  ) {
159  return ‪$range;
160  }
161 
162  $length = strlen(max($this->start, $this->end));
163  ‪$range = array_map(
164  function ($value) use ($length) {
165  return str_pad($value, $length, '0', STR_PAD_LEFT);
166  },
167  ‪$range
168  );
169  return ‪$range;
170  }
171 }
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\respondWhenInRange
‪string null respondWhenInRange(string $value)
Definition: StaticRangeMapper.php:112
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\$end
‪string $end
Definition: StaticRangeMapper.php:56
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\buildRange
‪string[] buildRange()
Definition: StaticRangeMapper.php:132
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\$range
‪string[] $range
Definition: StaticRangeMapper.php:60
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\$settings
‪array $settings
Definition: StaticRangeMapper.php:48
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\__construct
‪__construct(array $settings)
Definition: StaticRangeMapper.php:66
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\applyNumericPrefix
‪string[] applyNumericPrefix(array $range)
Definition: StaticRangeMapper.php:148
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\count
‪count()
Definition: StaticRangeMapper.php:87
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\$start
‪string $start
Definition: StaticRangeMapper.php:52
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\resolve
‪resolve(string $value)
Definition: StaticRangeMapper.php:103
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\generate
‪generate(string $value)
Definition: StaticRangeMapper.php:95
‪TYPO3\CMS\Core\Routing\Aspect
Definition: AspectFactory.php:4
‪TYPO3\CMS\Core\Routing\Aspect\StaticMappableAspectInterface
Definition: StaticMappableAspectInterface.php:23
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper
Definition: StaticRangeMapper.php:45