[TASK] Move arguments to initializeArguments() in CurrencyVH in ext:fluid
[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 * Initialize arguments.
60 *
61 * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
62 */
63 public function initializeArguments()
64 {
65 parent::initializeArguments();
66 $this->registerArgument('currencySign', 'string', 'The currency sign, eg $ or €.', false, '');
67 $this->registerArgument('decimalSeparator', 'string', 'The separator for the decimal point.', false, ',');
68 $this->registerArgument('thousandsSeparator', 'string', 'The thousands separator.', false, '.');
69 $this->registerArgument('prependCurrency', 'bool', 'Select if the currency sign should be prepended', false, false);
70 $this->registerArgument('separateCurrency', 'bool', 'Separate the currency sign from the number by a single space, defaults to true due to backwards compatibility', false, true);
71 $this->registerArgument('decimals', 'int', 'Set decimals places.', false, 2);
72 }
73
74 /**
75 *
76 * @return string the formatted amount.
77 * @api
78 */
79 public function render()
80 {
81 return static::renderStatic(
82 $this->arguments,
83 $this->buildRenderChildrenClosure(),
84 $this->renderingContext
85 );
86 }
87
88 /**
89 * @param array $arguments
90 * @param \Closure $renderChildrenClosure
91 * @param RenderingContextInterface $renderingContext
92 *
93 * @return string
94 */
95 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
96 {
97 $currencySign = $arguments['currencySign'];
98 $decimalSeparator = $arguments['decimalSeparator'];
99 $thousandsSeparator = $arguments['thousandsSeparator'];
100 $prependCurrency = $arguments['prependCurrency'];
101 $separateCurrency = $arguments['separateCurrency'];
102 $decimals = $arguments['decimals'];
103
104 $floatToFormat = $renderChildrenClosure();
105 if (empty($floatToFormat)) {
106 $floatToFormat = 0.0;
107 } else {
108 $floatToFormat = floatval($floatToFormat);
109 }
110 $output = number_format($floatToFormat, $decimals, $decimalSeparator, $thousandsSeparator);
111 if ($currencySign !== '') {
112 $currencySeparator = $separateCurrency ? ' ' : '';
113 if ($prependCurrency === true) {
114 $output = $currencySign . $currencySeparator . $output;
115 } else {
116 $output = $output . $currencySeparator . $currencySign;
117 }
118 }
119 return $output;
120 }
121 }