|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Grido (https://github.com/o5/grido) |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2011 Petr Bugyík (http://petr.bugyik.cz) |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the file LICENSE.md that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Grido\Components\Columns; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Number column. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Grido |
|
18
|
|
|
* @subpackage Components\Columns |
|
19
|
|
|
* @author Petr Bugyík |
|
20
|
|
|
* |
|
21
|
|
|
* @property array $numberFormat |
|
22
|
|
|
*/ |
|
23
|
|
|
class Number extends Editable |
|
24
|
1 |
|
{ |
|
25
|
|
|
/** @var array */ |
|
26
|
|
|
protected $numberFormat = [ |
|
27
|
|
|
self::NUMBER_FORMAT_DECIMALS => 0, |
|
28
|
|
|
self::NUMBER_FORMAT_DECIMAL_POINT => '.', |
|
29
|
|
|
self::NUMBER_FORMAT_THOUSANDS_SEPARATOR => ',' |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
/** @const keys of array $numberFormat */ |
|
33
|
|
|
const NUMBER_FORMAT_DECIMALS = 0; |
|
34
|
|
|
const NUMBER_FORMAT_DECIMAL_POINT = 1; |
|
35
|
|
|
const NUMBER_FORMAT_THOUSANDS_SEPARATOR = 2; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param \Grido\Grid $grid |
|
39
|
|
|
* @param string $name |
|
40
|
|
|
* @param string $label |
|
41
|
|
|
* @param int $decimals number of decimal points |
|
42
|
|
|
* @param string $decPoint separator for the decimal point |
|
43
|
|
|
* @param string $thousandsSep thousands separator |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct($grid, $name, $label, $decimals = NULL, $decPoint = NULL, $thousandsSep = NULL) |
|
46
|
|
|
{ |
|
47
|
1 |
|
parent::__construct($grid, $name, $label); |
|
48
|
|
|
|
|
49
|
1 |
|
$this->setNumberFormat($decimals, $decPoint, $thousandsSep); |
|
50
|
1 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Sets number format. Params are same as internal function number_format(). |
|
54
|
|
|
* @param int $decimals number of decimal points |
|
55
|
|
|
* @param string $decPoint separator for the decimal point |
|
56
|
|
|
* @param string $thousandsSep thousands separator |
|
57
|
|
|
* @return Number |
|
58
|
|
|
*/ |
|
59
|
|
|
public function setNumberFormat($decimals = NULL, $decPoint = NULL, $thousandsSep = NULL) |
|
60
|
|
|
{ |
|
61
|
1 |
|
if ($decimals !== NULL) { |
|
62
|
1 |
|
$this->numberFormat[self::NUMBER_FORMAT_DECIMALS] = (int) $decimals; |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
if ($decPoint !== NULL) { |
|
66
|
1 |
|
$this->numberFormat[self::NUMBER_FORMAT_DECIMAL_POINT] = $decPoint; |
|
67
|
1 |
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
if ($thousandsSep !== NULL) { |
|
70
|
1 |
|
$this->numberFormat[self::NUMBER_FORMAT_THOUSANDS_SEPARATOR] = $thousandsSep; |
|
71
|
1 |
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getNumberFormat() |
|
80
|
|
|
{ |
|
81
|
1 |
|
return $this->numberFormat; |
|
82
|
1 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param mixed $value |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function formatValue($value) |
|
89
|
|
|
{ |
|
90
|
1 |
|
$value = parent::formatValue($value); |
|
91
|
|
|
|
|
92
|
1 |
|
$decimals = $this->numberFormat[self::NUMBER_FORMAT_DECIMALS]; |
|
93
|
1 |
|
$decPoint = $this->numberFormat[self::NUMBER_FORMAT_DECIMAL_POINT]; |
|
94
|
1 |
|
$thousandsSep = $this->numberFormat[self::NUMBER_FORMAT_THOUSANDS_SEPARATOR]; |
|
95
|
|
|
|
|
96
|
1 |
|
return is_numeric($value) |
|
97
|
1 |
|
? number_format($value, $decimals, $decPoint, $thousandsSep) |
|
98
|
1 |
|
: $value; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|