Created
January 28, 2019 13:40
-
-
Save wazum/e5862338fbc4466bac8c66bf11efdf9e to your computer and use it in GitHub Desktop.
TYPO3 CMS 9.5 StaticPaddedRangeMapper Routing Aspect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace Vendor\Extension\Routing\Aspect; | |
use TYPO3\CMS\Core\Routing\Aspect\StaticRangeMapper; | |
/** | |
* Class StaticPaddedRangeMapper | |
* @package Vendor\Extension\Routing\Aspect | |
* @author Wolfgang Klinger <wolfgang@wazum.com> | |
* | |
* Example: | |
* routeEnhancers: | |
* … | |
* aspects: | |
* field: | |
* type: StaticPaddedRangeMapper | |
* start: '1' | |
* end: '12' | |
* padString: '0' | |
* padLength: 2 | |
* # STR_PAD_LEFT = 0 | |
* padType: 0 | |
*/ | |
class StaticPaddedRangeMapper extends StaticRangeMapper | |
{ | |
/** | |
* @var string | |
*/ | |
protected $padString = ''; | |
/** | |
* @var int | |
*/ | |
protected $padLength = 0; | |
/** | |
* @var int | |
*/ | |
protected $padType = 0; | |
/** | |
* @param array $settings | |
* @throws \InvalidArgumentException | |
*/ | |
public function __construct(array $settings) | |
{ | |
// we can't call the parent::__construct | |
// because the constructor already calls buildRange … | |
$start = $settings['start'] ?? null; | |
$end = $settings['end'] ?? null; | |
$padString = $settings['padString'] ?? null; | |
$padLength = $settings['padLength'] ?? null; | |
$padType = $settings['padType'] ?? null; | |
if (!is_string($start)) { | |
throw new \InvalidArgumentException('start must be string', 1537277163); | |
} | |
if (!is_string($end)) { | |
throw new \InvalidArgumentException('end must be string', 1537277164); | |
} | |
if (!is_string($padString)) { | |
throw new \InvalidArgumentException('padString must be string', 1538277165); | |
} | |
if (!is_int($padLength)) { | |
throw new \InvalidArgumentException('padLength must be string', 1538277166); | |
} | |
if (!is_int($padType)) { | |
throw new \InvalidArgumentException('padType must be string', 1538277167); | |
} | |
if (!in_array($padType, [STR_PAD_LEFT, STR_PAD_RIGHT, STR_PAD_BOTH], true)) { | |
throw new \InvalidArgumentException('padType must be valid', 1538277168); | |
} | |
$this->settings = $settings; | |
$this->start = $start; | |
$this->end = $end; | |
$this->padString = $padString; | |
$this->padLength = $padLength; | |
$this->padType = $padType; | |
$this->range = $this->buildRange(); | |
} | |
/** | |
* @return array | |
*/ | |
protected function buildRange(): array | |
{ | |
$padString = $this->padString; | |
$padLength = $this->padLength; | |
$padType = $this->padType; | |
$range = array_map(function ($item) use ($padString, $padLength, $padType) { | |
return str_pad((string)$item, $padLength, $padString, $padType); | |
}, range($this->start, $this->end)); | |
if (count($range) > 1000) { | |
throw new \LengthException( | |
'Range is larger than 1000 items', | |
1537696771 | |
); | |
} | |
return $range; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment