‪TYPO3CMS  9.5
CurrencyViewHelper.php
Go to the documentation of this file.
1 <?php
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 
77 class ‪CurrencyViewHelper extends AbstractViewHelper
78 {
79  use CompileWithRenderStatic;
80 
86  protected ‪$escapeChildren = false;
87 
93  public function ‪initializeArguments()
94  {
95  $this->registerArgument('currencySign', 'string', 'The currency sign, eg $ or €.', false, '');
96  $this->registerArgument('decimalSeparator', 'string', 'The separator for the decimal point.', false, ',');
97  $this->registerArgument('thousandsSeparator', 'string', 'The thousands separator.', false, '.');
98  $this->registerArgument('prependCurrency', 'bool', 'Select if the currency sign should be prepended', false, false);
99  $this->registerArgument('separateCurrency', 'bool', 'Separate the currency sign from the number by a single space, defaults to true due to backwards compatibility', false, true);
100  $this->registerArgument('decimals', 'int', 'Set decimals places.', false, 2);
101  $this->registerArgument('useDash', 'bool', 'Use the dash instead of decimal 00', false, false);
102  }
103 
113  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
114  {
115  $currencySign = $arguments['currencySign'];
116  $decimalSeparator = $arguments['decimalSeparator'];
117  $thousandsSeparator = $arguments['thousandsSeparator'];
118  $prependCurrency = $arguments['prependCurrency'];
119  $separateCurrency = $arguments['separateCurrency'];
120  $decimals = $arguments['decimals'];
121  $useDash = $arguments['useDash'];
122 
123  $floatToFormat = $renderChildrenClosure();
124  if (empty($floatToFormat)) {
125  $floatToFormat = 0.0;
126  } else {
127  $floatToFormat = (float)$floatToFormat;
128  }
129  ‪$output = number_format($floatToFormat, $decimals, $decimalSeparator, $thousandsSeparator);
130 
131  if ($useDash && $floatToFormat === floor($floatToFormat)) {
132  ‪$output = explode($decimalSeparator, ‪$output)[0] . $decimalSeparator . '—';
133  }
134 
135  if ($currencySign !== '') {
136  $currencySeparator = $separateCurrency ? ' ' : '';
137  if ($prependCurrency === true) {
138  ‪$output = $currencySign . $currencySeparator . ‪$output;
139  } else {
140  ‪$output = ‪$output . $currencySeparator . $currencySign;
141  }
142  }
143  return ‪$output;
144  }
145 }
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper\$escapeChildren
‪bool $escapeChildren
Definition: CurrencyViewHelper.php:84
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper\initializeArguments
‪initializeArguments()
Definition: CurrencyViewHelper.php:91
‪TYPO3\CMS\Fluid\ViewHelpers\Format
Definition: AbstractEncodingViewHelper.php:2
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper
Definition: CurrencyViewHelper.php:78
‪$output
‪$output
Definition: annotationChecker.php:113
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper\renderStatic
‪static string renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: CurrencyViewHelper.php:111