[BUGFIX] Use late static binding for compilable viewhelpers
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / NumberViewHelper.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 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
15 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
16 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
17
18 /**
19 * Formats a number with custom precision, decimal point and grouped thousands.
20 *
21 * @see http://www.php.net/manual/en/function.number-format.php
22 *
23 * = Examples =
24 *
25 * <code title="Defaults">
26 * <f:format.number>423423.234</f:format.number>
27 * </code>
28 * <output>
29 * 423,423.20
30 * </output>
31 *
32 * <code title="With all parameters">
33 * <f:format.number decimals="1" decimalSeparator="," thousandsSeparator=".">423423.234</f:format.number>
34 * </code>
35 * <output>
36 * 423.423,2
37 * </output>
38 *
39 * @api
40 */
41 class NumberViewHelper extends AbstractViewHelper implements CompilableInterface {
42
43 /**
44 * Format the numeric value as a number with grouped thousands, decimal point and
45 * precision.
46 *
47 * @param int $decimals The number of digits after the decimal point
48 * @param string $decimalSeparator The decimal point character
49 * @param string $thousandsSeparator The character for grouping the thousand digits
50 *
51 * @return string The formatted number
52 * @api
53 */
54 public function render($decimals = 2, $decimalSeparator = '.', $thousandsSeparator = ',') {
55 return static::renderStatic(
56 array(
57 'decimals' => $decimals,
58 'decimalSeparator' => $decimalSeparator,
59 'thousandsSeparator' => $thousandsSeparator,
60 ),
61 $this->buildRenderChildrenClosure(),
62 $this->renderingContext
63 );
64 }
65
66 /**
67 * @param array $arguments
68 * @param callable $renderChildrenClosure
69 * @param RenderingContextInterface $renderingContext
70 *
71 * @return string
72 */
73 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
74
75 $decimals = $arguments['decimals'];
76 $decimalSeparator = $arguments['decimalSeparator'];
77 $thousandsSeparator = $arguments['thousandsSeparator'];
78
79 $stringToFormat = $renderChildrenClosure();
80 return number_format($stringToFormat, $decimals, $decimalSeparator, $thousandsSeparator);
81 }
82
83 }