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