TYPO3 CMS  TYPO3_6-2
CurrencyFilter.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Form\Filter;
3 
23 
30  protected $decimalsPoint;
31 
38  protected $thousandSeparator;
39 
45  public function __construct($arguments = array()) {
46  $this->setDecimalsPoint($arguments['decimalPoint']);
47  $this->setThousandSeparator($arguments['thousandSeparator']);
48  }
49 
56  public function setDecimalsPoint($decimalsPoint = '.') {
57  if (empty($decimalsPoint)) {
58  $this->decimalsPoint = '.';
59  } else {
60  $this->decimalsPoint = (string) $decimalsPoint;
61  }
62  return $this;
63  }
64 
71  public function setThousandSeparator($thousandSeparator = ',') {
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  return $this;
82  }
83 
91  public function filter($value) {
92  $value = str_replace(
93  array(
94  $this->thousandSeparator,
95  $this->decimalsPoint,
96  ),
97  array(
98  '',
99  '.'
100  ),
101  (string)$value
102  );
103 
104  // replace all non numeric characters, decimalPoint and negative sign
105  $value = preg_replace("/[^0-9.-]/", "", $value);
106  $value = (double)$value;
107  return number_format($value, 2, $this->decimalsPoint, $this->thousandSeparator);
108  }
109 
110 }
setThousandSeparator($thousandSeparator=',')