[FOLLOWUP][BUGFIX] Use Fluid standalone abstract classes
[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 TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
18 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
19 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
20
21 /**
22 * Formats a given float to a currency representation.
23 *
24 * = Examples =
25 *
26 * <code title="Defaults">
27 * <f:format.currency>123.456</f:format.currency>
28 * </code>
29 * <output>
30 * 123,46
31 * </output>
32 *
33 * <code title="All parameters">
34 * <f:format.currency currencySign="$" decimalSeparator="." thousandsSeparator="," prependCurrency="true" separateCurrency="false" decimals="2">54321</f:format.currency>
35 * </code>
36 * <output>
37 * $54,321.00
38 * </output>
39 *
40 * <code title="Inline notation">
41 * {someNumber -> f:format.currency(thousandsSeparator: ',', currencySign: '€')}
42 * </code>
43 * <output>
44 * 54,321,00 €
45 * (depending on the value of {someNumber})
46 * </output>
47 *
48 * @api
49 */
50 class CurrencyViewHelper extends AbstractViewHelper
51 {
52 use CompileWithRenderStatic;
53
54 /**
55 * Output is escaped already. We must not escape children, to avoid double encoding.
56 *
57 * @var bool
58 */
59 protected $escapeChildren = false;
60
61 /**
62 * Initialize arguments.
63 *
64 * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
65 */
66 public function initializeArguments()
67 {
68 $this->registerArgument('currencySign', 'string', 'The currency sign, eg $ or €.', false, '');
69 $this->registerArgument('decimalSeparator', 'string', 'The separator for the decimal point.', false, ',');
70 $this->registerArgument('thousandsSeparator', 'string', 'The thousands separator.', false, '.');
71 $this->registerArgument('prependCurrency', 'bool', 'Select if the currency sign should be prepended', false, false);
72 $this->registerArgument('separateCurrency', 'bool', 'Separate the currency sign from the number by a single space, defaults to true due to backwards compatibility', false, true);
73 $this->registerArgument('decimals', 'int', 'Set decimals places.', false, 2);
74 }
75
76 /**
77 * Formats a float to currency formatting
78 *
79 * @param array $arguments
80 * @param \Closure $renderChildrenClosure
81 * @param RenderingContextInterface $renderingContext
82 *
83 * @return string the formatted amount
84 */
85 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
86 {
87 $currencySign = $arguments['currencySign'];
88 $decimalSeparator = $arguments['decimalSeparator'];
89 $thousandsSeparator = $arguments['thousandsSeparator'];
90 $prependCurrency = $arguments['prependCurrency'];
91 $separateCurrency = $arguments['separateCurrency'];
92 $decimals = $arguments['decimals'];
93
94 $floatToFormat = $renderChildrenClosure();
95 if (empty($floatToFormat)) {
96 $floatToFormat = 0.0;
97 } else {
98 $floatToFormat = (float)$floatToFormat;
99 }
100 $output = number_format($floatToFormat, $decimals, $decimalSeparator, $thousandsSeparator);
101 if ($currencySign !== '') {
102 $currencySeparator = $separateCurrency ? ' ' : '';
103 if ($prependCurrency === true) {
104 $output = $currencySign . $currencySeparator . $output;
105 } else {
106 $output = $output . $currencySeparator . $currencySign;
107 }
108 }
109 return $output;
110 }
111 }