[CLEANUP] Fix type hints for renderChildrenArgument
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / CurrencyViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
18 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
19
20 /**
21 * Formats a given float to a currency representation.
22 *
23 * = Examples =
24 *
25 * <code title="Defaults">
26 * <f:format.currency>123.456</f:format.currency>
27 * </code>
28 * <output>
29 * 123,46
30 * </output>
31 *
32 * <code title="All parameters">
33 * <f:format.currency currencySign="$" decimalSeparator="." thousandsSeparator="," prependCurrency="TRUE" separateCurrency="FALSE" decimals="2">54321</f:format.currency>
34 * </code>
35 * <output>
36 * $54,321.00
37 * </output>
38 *
39 * <code title="Inline notation">
40 * {someNumber -> f:format.currency(thousandsSeparator: ',', currencySign: '€')}
41 * </code>
42 * <output>
43 * 54,321,00 €
44 * (depending on the value of {someNumber})
45 * </output>
46 *
47 * @api
48 */
49 class CurrencyViewHelper extends AbstractViewHelper
50 {
51 /**
52 * @param string $currencySign (optional) The currency sign, eg $ or €.
53 * @param string $decimalSeparator (optional) The separator for the decimal point.
54 * @param string $thousandsSeparator (optional) The thousands separator.
55 * @param bool $prependCurrency (optional) Select if the curreny sign should be prepended
56 * @param bool $separateCurrency (optional) Separate the currency sign from the number by a single space, defaults to true due to backwards compatibility
57 * @param int $decimals (optional) Set decimals places.
58 * @return string the formatted amount.
59 * @api
60 */
61 public function render($currencySign = '', $decimalSeparator = ',', $thousandsSeparator = '.', $prependCurrency = false, $separateCurrency = true, $decimals = 2)
62 {
63 return static::renderStatic(
64 array(
65 'currencySign' => $currencySign,
66 'decimalSeparator' => $decimalSeparator,
67 'thousandsSeparator' => $thousandsSeparator,
68 'prependCurrency' => $prependCurrency,
69 'separateCurrency' => $separateCurrency,
70 'decimals' => $decimals
71 ),
72 $this->buildRenderChildrenClosure(),
73 $this->renderingContext
74 );
75 }
76
77 /**
78 * @param array $arguments
79 * @param \Closure $renderChildrenClosure
80 * @param RenderingContextInterface $renderingContext
81 *
82 * @return string
83 */
84 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
85 {
86 $currencySign = $arguments['currencySign'];
87 $decimalSeparator = $arguments['decimalSeparator'];
88 $thousandsSeparator = $arguments['thousandsSeparator'];
89 $prependCurrency = $arguments['prependCurrency'];
90 $separateCurrency = $arguments['separateCurrency'];
91 $decimals = $arguments['decimals'];
92
93 $floatToFormat = $renderChildrenClosure();
94 if (empty($floatToFormat)) {
95 $floatToFormat = 0.0;
96 } else {
97 $floatToFormat = floatval($floatToFormat);
98 }
99 $output = number_format($floatToFormat, $decimals, $decimalSeparator, $thousandsSeparator);
100 if ($currencySign !== '') {
101 $currencySeparator = $separateCurrency ? ' ' : '';
102 if ($prependCurrency === true) {
103 $output = $currencySign . $currencySeparator . $output;
104 } else {
105 $output = $output . $currencySeparator . $currencySign;
106 }
107 }
108 return $output;
109 }
110 }