TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
CurrencyViewHelper.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
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 
18 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
19 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
20 
51 {
52  use CompileWithRenderStatic;
53 
59  protected $escapeChildren = false;
60 
66  public function initializeArguments()
67  {
68  parent::initializeArguments();
69  $this->registerArgument('currencySign', 'string', 'The currency sign, eg $ or €.', false, '');
70  $this->registerArgument('decimalSeparator', 'string', 'The separator for the decimal point.', false, ',');
71  $this->registerArgument('thousandsSeparator', 'string', 'The thousands separator.', false, '.');
72  $this->registerArgument('prependCurrency', 'bool', 'Select if the currency sign should be prepended', false, false);
73  $this->registerArgument('separateCurrency', 'bool', 'Separate the currency sign from the number by a single space, defaults to true due to backwards compatibility', false, true);
74  $this->registerArgument('decimals', 'int', 'Set decimals places.', false, 2);
75  }
76 
86  public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
87  {
88  $currencySign = $arguments['currencySign'];
89  $decimalSeparator = $arguments['decimalSeparator'];
90  $thousandsSeparator = $arguments['thousandsSeparator'];
91  $prependCurrency = $arguments['prependCurrency'];
92  $separateCurrency = $arguments['separateCurrency'];
93  $decimals = $arguments['decimals'];
94 
95  $floatToFormat = $renderChildrenClosure();
96  if (empty($floatToFormat)) {
97  $floatToFormat = 0.0;
98  } else {
99  $floatToFormat = (float)$floatToFormat;
100  }
101  $output = number_format($floatToFormat, $decimals, $decimalSeparator, $thousandsSeparator);
102  if ($currencySign !== '') {
103  $currencySeparator = $separateCurrency ? ' ' : '';
104  if ($prependCurrency === true) {
105  $output = $currencySign . $currencySeparator . $output;
106  } else {
107  $output = $output . $currencySeparator . $currencySign;
108  }
109  }
110  return $output;
111  }
112 }
static renderStatic(array $arguments,\Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)