TYPO3 CMS  TYPO3_7-6
CurrencyFilter.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 
21 {
28  protected $decimalsPoint;
29 
36  protected $thousandSeparator;
37 
43  public function __construct($arguments = [])
44  {
45  $this->setDecimalsPoint($arguments['decimalPoint']);
46  $this->setThousandSeparator($arguments['thousandSeparator']);
47  }
48 
55  public function setDecimalsPoint($decimalsPoint = '.')
56  {
57  if (empty($decimalsPoint)) {
58  $this->decimalsPoint = '.';
59  } else {
60  $this->decimalsPoint = (string)$decimalsPoint;
61  }
62  }
63 
71  {
72  if (empty($thousandSeparator)) {
73  $this->thousandSeparator = ',';
74  } elseif ($thousandSeparator === 'space') {
75  $this->thousandSeparator = ' ';
76  } elseif ($thousandSeparator === 'none') {
77  $this->thousandSeparator = '';
78  } else {
79  $this->thousandSeparator = (string)$thousandSeparator;
80  }
81  }
82 
90  public function filter($value)
91  {
92  $value = str_replace(
93  [
94  $this->thousandSeparator,
95  $this->decimalsPoint,
96  ],
97  [
98  '',
99  '.'
100  ],
101  (string)$value
102  );
103 
104  // replace all non numeric characters, decimalPoint and negativ sign
105  $value = preg_replace('/[^0-9.-]/', '', $value);
106  $value = (double)$value;
107  return number_format($value, 2, $this->decimalsPoint, $this->thousandSeparator);
108  }
109 }