[TASK] Sync CMS Fluid with Flow Fluid 1.1 (part1)
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / PaddingViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13
14 /**
15 * Formats a string using PHPs str_pad function.
16 * @see http://www.php.net/manual/en/function.str_pad.php
17 *
18 * = Examples =
19 *
20 * <code title="Defaults">
21 * <f:format.padding padLength="10">TYPO3</f:format.padding>
22 * </code>
23 * <output>
24 * TYPO3 (note the trailing whitespace)
25 * <output>
26 *
27 * <code title="Specify padding string">
28 * <f:format.padding padLength="10" padString="-=">TYPO3</f:format.padding>
29 * </code>
30 * <output>
31 * TYPO3-=-=-
32 * </output>
33 *
34 * <code title="Specify padding type">
35 * <f:format.padding padLength="10" padString="-" padType="both">TYPO3</f:format.padding>
36 * </code>
37 * <output>
38 * --TYPO3---
39 * </output>
40 *
41 * @api
42 */
43 class PaddingViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
44
45 /**
46 * Pad a string to a certain length with another string
47 *
48 * @param integer $padLength 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.
49 * @param string $padString The padding string
50 * @param string $padType Append the padding at this site (Possible values: right,left,both. Default: right)
51 * @return string The formatted value
52 * @api
53 */
54 public function render($padLength, $padString = ' ', $padType = 'right') {
55 $string = $this->renderChildren();
56 $padTypes = array(
57 'left' => STR_PAD_LEFT,
58 'right' => STR_PAD_RIGHT,
59 'both' => STR_PAD_BOTH
60 );
61 if (!isset($padTypes[$padType])) {
62 $padType = 'right';
63 }
64 return str_pad($string, $padLength, $padString, $padTypes[$padType]);
65 }
66 }
67
68 ?>