‪TYPO3CMS  ‪main
CurrencyViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
23 
80 final class ‪CurrencyViewHelper extends AbstractViewHelper
81 {
82  use CompileWithRenderStatic;
83 
89  protected ‪$escapeChildren = false;
90 
91  public function ‪initializeArguments(): void
92  {
93  $this->registerArgument('currencySign', 'string', 'The currency sign, eg $ or €.', false, '');
94  $this->registerArgument('decimalSeparator', 'string', 'The separator for the decimal point.', false, ',');
95  $this->registerArgument('thousandsSeparator', 'string', 'The thousands separator.', false, '.');
96  $this->registerArgument('prependCurrency', 'bool', 'Select if the currency sign should be prepended', false, false);
97  $this->registerArgument('separateCurrency', 'bool', 'Separate the currency sign from the number by a single space, defaults to true due to backwards compatibility', false, true);
98  $this->registerArgument('decimals', 'int', 'Set decimals places.', false, 2);
99  $this->registerArgument('useDash', 'bool', 'Use the dash instead of decimal 00', false, false);
100  }
101 
102  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
103  {
104  $currencySign = $arguments['currencySign'];
105  $decimalSeparator = $arguments['decimalSeparator'];
106  $thousandsSeparator = $arguments['thousandsSeparator'];
107  $prependCurrency = $arguments['prependCurrency'];
108  $separateCurrency = $arguments['separateCurrency'];
109  $decimals = (int)$arguments['decimals'];
110  $useDash = $arguments['useDash'];
111 
112  $floatToFormat = $renderChildrenClosure();
113  if (empty($floatToFormat)) {
114  $floatToFormat = 0.0;
115  } else {
116  $floatToFormat = (float)$floatToFormat;
117  }
118  ‪$output = number_format($floatToFormat, $decimals, $decimalSeparator, $thousandsSeparator);
119 
120  if ($useDash && $floatToFormat === floor($floatToFormat)) {
121  ‪$output = explode($decimalSeparator, ‪$output)[0] . $decimalSeparator . '—';
122  }
123 
124  if ($currencySign !== '') {
125  $currencySeparator = $separateCurrency ? ' ' : '';
126  if ($prependCurrency === true) {
127  ‪$output = $currencySign . $currencySeparator . ‪$output;
128  } else {
129  ‪$output = ‪$output . $currencySeparator . $currencySign;
130  }
131  }
132  return ‪$output;
133  }
134 }
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper\renderStatic
‪static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: CurrencyViewHelper.php:100
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper\$escapeChildren
‪bool $escapeChildren
Definition: CurrencyViewHelper.php:87
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper\initializeArguments
‪initializeArguments()
Definition: CurrencyViewHelper.php:89
‪TYPO3\CMS\Fluid\ViewHelpers\Format
Definition: AbstractEncodingViewHelper.php:18
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CurrencyViewHelper
Definition: CurrencyViewHelper.php:81
‪$output
‪$output
Definition: annotationChecker.php:114