[TASK] Streamline escaping for ViewHelpers of all sysexts
[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 * Output is escaped already. We must not escape children, to avoid double encoding.
53 *
54 * @var bool
55 */
56 protected $escapeChildren = false;
57
58 /**
59 * @param string $currencySign (optional) The currency sign, eg $ or €.
60 * @param string $decimalSeparator (optional) The separator for the decimal point.
61 * @param string $thousandsSeparator (optional) The thousands separator.
62 * @param bool $prependCurrency (optional) Select if the curreny sign should be prepended
63 * @param bool $separateCurrency (optional) Separate the currency sign from the number by a single space, defaults to true due to backwards compatibility
64 * @param int $decimals (optional) Set decimals places.
65 * @return string the formatted amount.
66 * @api
67 */
68 public function render($currencySign = '', $decimalSeparator = ',', $thousandsSeparator = '.', $prependCurrency = false, $separateCurrency = true, $decimals = 2)
69 {
70 return static::renderStatic(
71 array(
72 'currencySign' => $currencySign,
73 'decimalSeparator' => $decimalSeparator,
74 'thousandsSeparator' => $thousandsSeparator,
75 'prependCurrency' => $prependCurrency,
76 'separateCurrency' => $separateCurrency,
77 'decimals' => $decimals
78 ),
79 $this->buildRenderChildrenClosure(),
80 $this->renderingContext
81 );
82 }
83
84 /**
85 * @param array $arguments
86 * @param \Closure $renderChildrenClosure
87 * @param RenderingContextInterface $renderingContext
88 *
89 * @return string
90 */
91 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
92 {
93 $currencySign = $arguments['currencySign'];
94 $decimalSeparator = $arguments['decimalSeparator'];
95 $thousandsSeparator = $arguments['thousandsSeparator'];
96 $prependCurrency = $arguments['prependCurrency'];
97 $separateCurrency = $arguments['separateCurrency'];
98 $decimals = $arguments['decimals'];
99
100 $floatToFormat = $renderChildrenClosure();
101 if (empty($floatToFormat)) {
102 $floatToFormat = 0.0;
103 } else {
104 $floatToFormat = floatval($floatToFormat);
105 }
106 $output = number_format($floatToFormat, $decimals, $decimalSeparator, $thousandsSeparator);
107 if ($currencySign !== '') {
108 $currencySeparator = $separateCurrency ? ' ' : '';
109 if ($prependCurrency === true) {
110 $output = $currencySign . $currencySeparator . $output;
111 } else {
112 $output = $output . $currencySeparator . $currencySign;
113 }
114 }
115 return $output;
116 }
117 }