[TASK] Move documentation files into 9.4 folder - part 2
[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 * <code title="use dash for decimals without value">
49 * <f:format.currency useDash="true">123.00</f:format.currency>
50 * </code>
51 * <output>
52 * 123,-
53 * </output>
54 *
55 * @api
56 */
57 class CurrencyViewHelper extends AbstractViewHelper
58 {
59 use CompileWithRenderStatic;
60
61 /**
62 * Output is escaped already. We must not escape children, to avoid double encoding.
63 *
64 * @var bool
65 */
66 protected $escapeChildren = false;
67
68 /**
69 * Initialize arguments.
70 *
71 * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
72 */
73 public function initializeArguments()
74 {
75 $this->registerArgument('currencySign', 'string', 'The currency sign, eg $ or €.', false, '');
76 $this->registerArgument('decimalSeparator', 'string', 'The separator for the decimal point.', false, ',');
77 $this->registerArgument('thousandsSeparator', 'string', 'The thousands separator.', false, '.');
78 $this->registerArgument('prependCurrency', 'bool', 'Select if the currency sign should be prepended', false, false);
79 $this->registerArgument('separateCurrency', 'bool', 'Separate the currency sign from the number by a single space, defaults to true due to backwards compatibility', false, true);
80 $this->registerArgument('decimals', 'int', 'Set decimals places.', false, 2);
81 $this->registerArgument('useDash', 'bool', 'Use the dash instead of decimal 00', false, false);
82 }
83
84 /**
85 * Formats a float to currency formatting
86 *
87 * @param array $arguments
88 * @param \Closure $renderChildrenClosure
89 * @param RenderingContextInterface $renderingContext
90 *
91 * @return string the formatted amount
92 */
93 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
94 {
95 $currencySign = $arguments['currencySign'];
96 $decimalSeparator = $arguments['decimalSeparator'];
97 $thousandsSeparator = $arguments['thousandsSeparator'];
98 $prependCurrency = $arguments['prependCurrency'];
99 $separateCurrency = $arguments['separateCurrency'];
100 $decimals = $arguments['decimals'];
101 $useDash = $arguments['useDash'];
102
103 $floatToFormat = $renderChildrenClosure();
104 if (empty($floatToFormat)) {
105 $floatToFormat = 0.0;
106 } else {
107 $floatToFormat = (float)$floatToFormat;
108 }
109 $output = number_format($floatToFormat, $decimals, $decimalSeparator, $thousandsSeparator);
110
111 if ($useDash && $floatToFormat === floor($floatToFormat)) {
112 $output = explode($decimalSeparator, $output)[0] . $decimalSeparator . '—';
113 }
114
115 if ($currencySign !== '') {
116 $currencySeparator = $separateCurrency ? ' ' : '';
117 if ($prependCurrency === true) {
118 $output = $currencySign . $currencySeparator . $output;
119 } else {
120 $output = $output . $currencySeparator . $currencySign;
121 }
122 }
123 return $output;
124 }
125 }