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