TYPO3 * ``` * * @see https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-format-padding * @see https://www.php.net/manual/en/function.str-pad */ final class PaddingViewHelper extends AbstractViewHelper { /** * Output is escaped already. We must not escape children, to avoid double encoding. * * @var bool */ protected $escapeChildren = false; public function initializeArguments(): void { $this->registerArgument('value', 'string', 'string to format'); $this->registerArgument('padLength', 'int', 'Length of the resulting string. If the value of pad_length is negative or less than the length of the input string, no padding takes place.', true); $this->registerArgument('padString', 'string', 'The padding string', false, ' '); $this->registerArgument('padType', 'string', 'Append the padding at this site (Possible values: right,left,both. Default: right)', false, 'right'); } /** * Pad a string to a certain length with another string. */ public function render(): string { $value = $this->renderChildren(); $padTypes = [ 'left' => STR_PAD_LEFT, 'right' => STR_PAD_RIGHT, 'both' => STR_PAD_BOTH, ]; $padType = $this->arguments['padType']; if (!isset($padTypes[$padType])) { $padType = 'right'; } $value = (string)$value; $padString = (string)$this->arguments['padString']; // mb_str_pad() throws a ValueError on an empty pad string, so return the value unchanged in that case. if ($padString === '') { return $value; } return mb_str_pad($value, (int)$this->arguments['padLength'], $padString, $padTypes[$padType]); } /** * Explicitly set argument name to be used as content. */ public function getContentArgumentName(): string { return 'value'; } }