‪TYPO3CMS  10.4
StaticRangeMapper.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 
46 {
50  protected ‪$settings;
51 
55  protected ‪$start;
56 
60  protected ‪$end;
61 
65  protected ‪$range;
66 
71  public function ‪__construct(array ‪$settings)
72  {
73  ‪$start = ‪$settings['start'] ?? null;
74  ‪$end = ‪$settings['end'] ?? null;
75 
76  if (!is_string(‪$start)) {
77  throw new \InvalidArgumentException('start must be string', 1537277163);
78  }
79  if (!is_string(‪$end)) {
80  throw new \InvalidArgumentException('end must be string', 1537277164);
81  }
82 
83  $this->settings = ‪$settings;
84  $this->start = ‪$start;
85  $this->end = ‪$end;
86  $this->range = $this->‪applyNumericPrefix($this->‪buildRange());
87  }
88 
92  public function ‪count(): int
93  {
94  return ‪count($this->range);
95  }
96 
100  public function ‪generate(string $value): ?string
101  {
102  return $this->‪respondWhenInRange($value);
103  }
104 
108  public function ‪resolve(string $value): ?string
109  {
110  return $this->‪respondWhenInRange($value);
111  }
112 
117  protected function ‪respondWhenInRange(string $value): ?string
118  {
119  if (in_array($value, $this->range, true)) {
120  return $value;
121  }
122  return null;
123  }
124 
137  protected function ‪buildRange(): array
138  {
139  ‪$range = array_map('strval', range($this->start, $this->end));
140  if (‪count(‪$range) > 1000) {
141  throw new \LengthException(
142  'Range is larger than 1000 items',
143  1537696771
144  );
145  }
146  return ‪$range;
147  }
148 
153  protected function ‪applyNumericPrefix(array ‪$range): array
154  {
155  if (!preg_match('#^\d+$#', $this->start)
156  || !preg_match('#^\d+$#', $this->end)
157  || $this->start === '0' || $this->end === '0'
158  || $this->start[0] !== '0' && $this->end[0] !== '0'
159  ) {
160  return ‪$range;
161  }
162 
163  $length = strlen(max($this->start, $this->end));
164  ‪$range = array_map(
165  function ($value) use ($length) {
166  return str_pad($value, $length, '0', STR_PAD_LEFT);
167  },
168  ‪$range
169  );
170  return ‪$range;
171  }
172 }
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\respondWhenInRange
‪string null respondWhenInRange(string $value)
Definition: StaticRangeMapper.php:113
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\$end
‪string $end
Definition: StaticRangeMapper.php:57
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\buildRange
‪string[] buildRange()
Definition: StaticRangeMapper.php:133
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\$range
‪string[] $range
Definition: StaticRangeMapper.php:61
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\$settings
‪array $settings
Definition: StaticRangeMapper.php:49
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\__construct
‪__construct(array $settings)
Definition: StaticRangeMapper.php:67
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\applyNumericPrefix
‪string[] applyNumericPrefix(array $range)
Definition: StaticRangeMapper.php:149
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\count
‪count()
Definition: StaticRangeMapper.php:88
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\$start
‪string $start
Definition: StaticRangeMapper.php:53
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\resolve
‪resolve(string $value)
Definition: StaticRangeMapper.php:104
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\generate
‪generate(string $value)
Definition: StaticRangeMapper.php:96
‪TYPO3\CMS\Core\Routing\Aspect
Definition: AspectFactory.php:18
‪TYPO3\CMS\Core\Routing\Aspect\StaticMappableAspectInterface
Definition: StaticMappableAspectInterface.php:24
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper
Definition: StaticRangeMapper.php:46