‪TYPO3CMS  ‪main
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 
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 
112  protected function ‪respondWhenInRange(string $value): ?string
113  {
114  if (in_array($value, $this->range, true)) {
115  return $value;
116  }
117  return null;
118  }
119 
132  protected function ‪buildRange(): array
133  {
134  ‪$range = array_map('strval', range($this->start, $this->end));
135  if (‪count(‪$range) > 1000) {
136  throw new \LengthException(
137  'Range is larger than 1000 items',
138  1537696771
139  );
140  }
141  return ‪$range;
142  }
143 
147  protected function ‪applyNumericPrefix(array ‪$range): array
148  {
149  if (!preg_match('#^\d+$#', $this->start)
150  || !preg_match('#^\d+$#', $this->end)
151  || $this->start === '0' || $this->end === '0'
152  || $this->start[0] !== '0' && $this->end[0] !== '0'
153  ) {
154  return ‪$range;
155  }
156 
157  $length = strlen(max($this->start, $this->end));
158  ‪$range = array_map(
159  static function ($value) use ($length) {
160  return str_pad($value, $length, '0', STR_PAD_LEFT);
161  },
162  ‪$range
163  );
164  return ‪$range;
165  }
166 }
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\respondWhenInRange
‪respondWhenInRange(string $value)
Definition: StaticRangeMapper.php:108
‪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:128
‪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:66
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\applyNumericPrefix
‪string[] applyNumericPrefix(array $range)
Definition: StaticRangeMapper.php:143
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\count
‪count()
Definition: StaticRangeMapper.php:87
‪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:103
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper\generate
‪generate(string $value)
Definition: StaticRangeMapper.php:95
‪TYPO3\CMS\Core\Routing\Aspect
Definition: AspectFactory.php:18
‪TYPO3\CMS\Core\Routing\Aspect\StaticMappableAspectInterface
Definition: StaticMappableAspectInterface.php:23
‪TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper
Definition: StaticRangeMapper.php:46