1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Grido\Components\Columns;
13
14 15 16 17 18 19 20 21 22
23 class Number extends Editable
24 {
25
26 protected $numberFormat = array(
27 self::NUMBER_FORMAT_DECIMALS => 0,
28 self::NUMBER_FORMAT_DECIMAL_POINT => '.',
29 self::NUMBER_FORMAT_THOUSANDS_SEPARATOR => ','
30 );
31
32
33 const NUMBER_FORMAT_DECIMALS = 0;
34 const NUMBER_FORMAT_DECIMAL_POINT = 1;
35 const NUMBER_FORMAT_THOUSANDS_SEPARATOR = 2;
36
37 38 39 40 41 42 43 44
45 public function __construct($grid, $name, $label, $decimals = NULL, $decPoint = NULL, $thousandsSep = NULL)
46 {
47 parent::__construct($grid, $name, $label);
48
49 $this->setNumberFormat($decimals, $decPoint, $thousandsSep);
50 }
51
52 53 54 55 56 57 58
59 public function setNumberFormat($decimals = NULL, $decPoint = NULL, $thousandsSep = NULL)
60 {
61 if ($decimals !== NULL) {
62 $this->numberFormat[self::NUMBER_FORMAT_DECIMALS] = (int) $decimals;
63 }
64
65 if ($decPoint !== NULL) {
66 $this->numberFormat[self::NUMBER_FORMAT_DECIMAL_POINT] = $decPoint;
67 }
68
69 if ($thousandsSep !== NULL) {
70 $this->numberFormat[self::NUMBER_FORMAT_THOUSANDS_SEPARATOR] = $thousandsSep;
71 }
72
73 return $this;
74 }
75
76 77 78
79 public function getNumberFormat()
80 {
81 return $this->numberFormat;
82 }
83
84 85 86 87
88 protected function formatValue($value)
89 {
90 $value = parent::formatValue($value);
91
92 $decimals = $this->numberFormat[self::NUMBER_FORMAT_DECIMALS];
93 $decPoint = $this->numberFormat[self::NUMBER_FORMAT_DECIMAL_POINT];
94 $thousandsSep = $this->numberFormat[self::NUMBER_FORMAT_THOUSANDS_SEPARATOR];
95
96 return is_numeric($value)
97 ? number_format($value, $decimals, $decPoint, $thousandsSep)
98 : $value;
99 }
100 }
101