[BUGFIX] Speed up f:format.currency
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / CurrencyViewHelper.php
1 <?php
2
3 /* *
4 * This script is backported from the FLOW3 package "TYPO3.Fluid". *
5 * *
6 * It is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU Lesser General Public License, either version 3 *
8 * of the License, or (at your option) any later version. *
9 * *
10 * The TYPO3 project - inspiring people to share! *
11 * */
12
13
14 /**
15 * Formats a given float to a currency representation.
16 *
17 * = Examples =
18 *
19 * <code title="Defaults">
20 * <f:format.currency>123.456</f:format.currency>
21 * </code>
22 * <output>
23 * 123,46
24 * </output>
25 *
26 * <code title="All parameters">
27 * <f:format.currency currencySign="$" decimalSeparator="." thousandsSeparator="," prependCurrency="TRUE" separateCurrency="FALSE">54321</f:format.currency>
28 * </code>
29 * <output>
30 * $54,321.00
31 * </output>
32 *
33 * <code title="Inline notation">
34 * {someNumber -> f:format.currency(thousandsSeparator: ',', currencySign: '€')}
35 * </code>
36 * <output>
37 * 54,321,00 €
38 * (depending on the value of {someNumber})
39 * </output>
40 *
41 * @api
42 */
43 class Tx_Fluid_ViewHelpers_Format_CurrencyViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
44
45 /**
46 * @param string $currencySign (optional) The currency sign, eg $ or €.
47 * @param string $decimalSeparator (optional) The separator for the decimal point.
48 * @param string $thousandsSeparator (optional) The thousands separator.
49 * @param boolean $prependCurrency (optional) Select if the curreny sign should be prepended
50 * @param boolean $separateCurrency (optional) Separate the currency sign from the number by a single space, defaults to true due to backwards compatibility
51 * @return string the formatted amount.
52 * @api
53 */
54 public function render($currencySign = '', $decimalSeparator = ',', $thousandsSeparator = '.', $prependCurrency = FALSE, $separateCurrency = TRUE) {
55 $floatToFormat = $this->renderChildren();
56
57 if (empty($floatToFormat)) {
58 $floatToFormat = 0.00;
59 } else {
60 $floatToFormat = floatval($floatToFormat);
61 }
62
63 $output = number_format($floatToFormat, 2, $decimalSeparator, $thousandsSeparator);
64
65 if ($currencySign !== '') {
66 $currencySeparator = ($separateCurrency) ? ' ' : '';
67 if ($prependCurrency === TRUE) {
68 $output = $currencySign . $currencySeparator . $output;
69 } else {
70 $output = $output . $currencySeparator . $currencySign;
71 }
72 }
73
74 return $output;
75 }
76 }
77 ?>