15491ca98c88dd7fd164a938248845de713cdae3
[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 script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
14 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
15 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
16
17 /**
18 * Formats a given float to a currency representation.
19 *
20 * = Examples =
21 *
22 * <code title="Defaults">
23 * <f:format.currency>123.456</f:format.currency>
24 * </code>
25 * <output>
26 * 123,46
27 * </output>
28 *
29 * <code title="All parameters">
30 * <f:format.currency currencySign="$" decimalSeparator="." thousandsSeparator="," prependCurrency="TRUE" separateCurrency="FALSE" decimals="2">54321</f:format.currency>
31 * </code>
32 * <output>
33 * $54,321.00
34 * </output>
35 *
36 * <code title="Inline notation">
37 * {someNumber -> f:format.currency(thousandsSeparator: ',', currencySign: '€')}
38 * </code>
39 * <output>
40 * 54,321,00 €
41 * (depending on the value of {someNumber})
42 * </output>
43 *
44 * @api
45 */
46 class CurrencyViewHelper extends AbstractViewHelper implements CompilableInterface {
47
48 /**
49 * @param string $currencySign (optional) The currency sign, eg $ or €.
50 * @param string $decimalSeparator (optional) The separator for the decimal point.
51 * @param string $thousandsSeparator (optional) The thousands separator.
52 * @param bool $prependCurrency (optional) Select if the curreny sign should be prepended
53 * @param bool $separateCurrency (optional) Separate the currency sign from the number by a single space, defaults to true due to backwards compatibility
54 * @param int $decimals (optional) Set decimals places.
55 * @return string the formatted amount.
56 * @api
57 */
58 public function render($currencySign = '', $decimalSeparator = ',', $thousandsSeparator = '.', $prependCurrency = FALSE, $separateCurrency = TRUE, $decimals = 2) {
59 return self::renderStatic(
60 array(
61 'currencySign' => $currencySign,
62 'decimalSeparator' => $decimalSeparator,
63 'thousandsSeparator' => $thousandsSeparator,
64 'prependCurrency' => $prependCurrency,
65 'separateCurrency' => $separateCurrency,
66 'decimals' => $decimals
67 ),
68 $this->buildRenderChildrenClosure(),
69 $this->renderingContext
70 );
71 }
72
73 /**
74 * @param array $arguments
75 * @param callable $renderChildrenClosure
76 * @param RenderingContextInterface $renderingContext
77 *
78 * @return string
79 */
80 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
81 $currencySign = $arguments['currencySign'];
82 $decimalSeparator = $arguments['decimalSeparator'];
83 $thousandsSeparator = $arguments['thousandsSeparator'];
84 $prependCurrency = $arguments['prependCurrency'];
85 $separateCurrency = $arguments['separateCurrency'];
86 $decimals = $arguments['decimals'];
87
88 $floatToFormat = $renderChildrenClosure();
89 if (empty($floatToFormat)) {
90 $floatToFormat = 0.0;
91 } else {
92 $floatToFormat = floatval($floatToFormat);
93 }
94 $output = number_format($floatToFormat, $decimals, $decimalSeparator, $thousandsSeparator);
95 if ($currencySign !== '') {
96 $currencySeparator = $separateCurrency ? ' ' : '';
97 if ($prependCurrency === TRUE) {
98 $output = $currencySign . $currencySeparator . $output;
99 } else {
100 $output = $output . $currencySeparator . $currencySign;
101 }
102 }
103 return $output;
104 }
105
106 }