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