NumberFormat.php
4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
namespace AC\Settings\Column;
use AC\Settings;
use AC\View;
class NumberFormat extends Settings\Column
implements Settings\FormatValue {
/**
* @var string
*/
private $number_format;
/**
* @var int
*/
private $number_decimals;
/**
* @var string
*/
private $number_decimal_point;
/**
* @var string
*/
private $number_thousands_separator;
protected function define_options() {
return [
'number_format' => '',
'number_decimals' => 0,
'number_decimal_point' => '.',
'number_thousands_separator' => '',
];
}
private function get_decimals_setting() {
$decimals = $this->create_element( 'number', 'number_decimals' );
$decimals->set_attribute( 'placeholder', $this->get_default( 'number_decimals' ) )
->set_attribute( 'step', 1 )
->set_attribute( 'max', 20 )
->set_attribute( 'min', 0 );
$decimals_view = new View( [
'label' => __( 'Decimals', 'codepress-admin-columns' ),
'setting' => $decimals,
] );
return $decimals_view;
}
private function get_decimal_point_setting() {
$decimal_point = $this->create_element( 'text', 'number_decimal_point' );
$decimal_point->set_attribute( 'placeholder', $this->get_default( 'number_decimal_point' ) );
$decimal_point_view = new View( [
'label' => __( 'Decimal point', 'codepress-admin-columns' ),
'setting' => $decimal_point,
] );
return $decimal_point_view;
}
private function get_thousands_point_setting() {
$setting = $this->create_element( 'text', 'number_thousands_separator' );
$setting->set_attribute( 'placeholder', $this->get_default( 'number_thousands_separator' ) );
$view = new View( [
'label' => __( 'Thousands separator', 'codepress-admin-columns' ),
'setting' => $setting,
] );
return $view;
}
private function get_preview_setting() {
return new View( [
'label' => __( 'Preview', 'codepress-admin-columns' ),
'setting' => '<code data-preview="">75000</code>',
] );
}
public function create_view() {
$sections = [];
$select = $this->create_element( 'select' )
->set_attribute( 'data-refresh', 'column' )
->set_options( [
'' => __( 'Default', 'codepress-admin-column' ),
'formatted' => __( 'Formatted', 'codepress-admin-column' ),
] );
if ( $this->get_number_format() ) {
$sections[] = $this->get_decimals_setting();
$sections[] = $this->get_decimal_point_setting();
$sections[] = $this->get_thousands_point_setting();
$sections[] = $this->get_preview_setting();
}
$view = new View( [
'label' => __( 'Number Format', 'codepress-admin-columns' ),
'setting' => $select,
'sections' => $sections,
] );
return $view;
}
/**
* @return string
*/
public function get_number_format() {
return $this->number_format;
}
/**
* @param string $number_format
*
* @return NumberFormat
*/
public function set_number_format( $number_format ) {
$this->number_format = $number_format;
return $this;
}
/**
* @return string
*/
public function get_number_decimals() {
return $this->number_decimals;
}
/**
* @param string $number_decimals
*
* @return NumberFormat
*/
public function set_number_decimals( $number_decimals ) {
$this->number_decimals = $number_decimals;
return $this;
}
/**
* @return string
*/
public function get_number_decimal_point() {
return $this->number_decimal_point;
}
/**
* @param string $number_decimal_point
*
* @return NumberFormat
*/
public function set_number_decimal_point( $number_decimal_point ) {
$this->number_decimal_point = $number_decimal_point;
return $this;
}
/**
* @return string
*/
public function get_number_thousands_separator() {
return $this->number_thousands_separator;
}
/**
* @param string $number_thousands_separator
*
* @return NumberFormat
*/
public function set_number_thousands_separator( $number_thousands_separator ) {
$this->number_thousands_separator = $number_thousands_separator;
return $this;
}
public function format( $value, $original_value ) {
if ( ! is_numeric( $value ) ) {
return $value;
}
switch ( $this->get_number_format() ) {
case 'formatted' :
return number_format( $value, (int) $this->get_number_decimals(), $this->get_number_decimal_point(), $this->get_number_thousands_separator() );
default:
return $value;
}
}
}