-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathDecimal.php
193 lines (166 loc) · 5.62 KB
/
Decimal.php
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
<?php
namespace dokuwiki\plugin\struct\types;
use dokuwiki\plugin\struct\meta\QueryBuilder;
use dokuwiki\plugin\struct\meta\QueryBuilderWhere;
use dokuwiki\plugin\struct\meta\ValidationException;
/**
* Class Decimal
*
* A field accepting decimal numbers
*
* @package dokuwiki\plugin\struct\types
*/
class Decimal extends AbstractMultiBaseType
{
protected $config = [
'min' => '',
'max' => '',
'roundto' => '-1',
'decpoint' => '.',
'thousands' => "\xE2\x80\xAF", // narrow no-break space
'trimzeros' => true,
'prefix' => '',
'postfix' => '',
'engineering' => false,
];
/**
* Output the stored data
*
* @param string|int $value the value stored in the database
* @param \Doku_Renderer $R the renderer currently used to render the data
* @param string $mode The mode the output is rendered in (eg. XHTML)
* @return bool true if $mode could be satisfied
*/
public function renderValue($value, \Doku_Renderer $R, $mode)
{
if ($this->config['engineering']) {
$unitsh = ['', 'k', 'M', 'G', 'T'];
$unitsl = ['', 'm', 'µ', 'n', 'p', 'f', 'a'];
$exp = floor(log10($value) / 3);
if ($exp < 0) {
$units = $unitsl;
$pfkey = -1 * $exp;
} else {
$units = $unitsh;
$pfkey = $exp;
}
if (count($units) <= ($pfkey + 1)) { //check if number is within prefixes
$pfkey = count($units) - 1;
$exp = $pfkey * $exp / abs($exp);
}
$R->cdata(
$this->config['prefix'] .
$value / 10 ** ($exp * 3) . "\xE2\x80\xAF" . $units[$pfkey] .
$this->config['postfix']
);
return true;
}
if ($this->config['roundto'] == -1) {
$value = $this->formatWithoutRounding(
$value,
$this->config['decpoint'],
$this->config['thousands']
);
} else {
$value = (float) $value;
$value = number_format(
$value,
(int)$this->config['roundto'],
$this->config['decpoint'],
$this->config['thousands']
);
}
if ($this->config['trimzeros'] && (strpos($value, (string) $this->config['decpoint']) !== false)) {
$value = rtrim($value, '0');
$value = rtrim($value, $this->config['decpoint']);
}
$R->cdata($this->config['prefix'] . $value . $this->config['postfix']);
return true;
}
/**
* @param int|string $rawvalue
* @return int|string
* @throws ValidationException
*/
public function validate($rawvalue)
{
$rawvalue = parent::validate($rawvalue);
$rawvalue = (float) str_replace(',', '.', $rawvalue); // we accept both
if ((string)$rawvalue != (string) $rawvalue) {
throw new ValidationException('Decimal needed');
}
if ($this->config['min'] !== '' && $rawvalue < (float) $this->config['min']) {
throw new ValidationException('Decimal min', (float) $this->config['min']);
}
if ($this->config['max'] !== '' && $rawvalue > (float) $this->config['max']) {
throw new ValidationException('Decimal max', (float) $this->config['max']);
}
return $rawvalue;
}
/**
* Works like number_format but keeps the decimals as is
*
* @link http://php.net/manual/en/function.number-format.php#91047
* @author info at daniel-marschall dot de
* @param float $number
* @param string $dec_point
* @param string $thousands_sep
* @return string
*/
protected function formatWithoutRounding($number, $dec_point, $thousands_sep)
{
$was_neg = $number < 0; // Because +0 == -0
$tmp = explode('.', $number);
$out = number_format(abs((float) $tmp[0]), 0, $dec_point, $thousands_sep);
if (isset($tmp[1])) $out .= $dec_point . $tmp[1];
if ($was_neg) $out = "-$out";
return $out;
}
/**
* Decimals need to be casted to the proper type for sorting
*
* @param QueryBuilder $QB
* @param string $tablealias
* @param string $colname
* @param string $order
*/
public function sort(QueryBuilder $QB, $tablealias, $colname, $order)
{
$QB->addOrderBy("CAST($tablealias.$colname AS DECIMAL) $order");
}
/**
* Decimals need to be casted to proper type for comparison
*
* @param QueryBuilderWhere $add
* @param string $tablealias
* @param string $colname
* @param string $comp
* @param string|\string[] $value
* @param string $op
*/
public function filter(QueryBuilderWhere $add, $tablealias, $colname, $comp, $value, $op)
{
$add = $add->where($op); // open a subgroup
$add->where('AND', "$tablealias.$colname != ''");
// make sure the field isn't empty
$op = 'AND';
/** @var QueryBuilderWhere $add Where additionional queries are added to */
if (is_array($value)) {
$add = $add->where($op); // sub where group
$op = 'OR';
}
foreach ((array)$value as $item) {
$pl = $add->getQB()->addValue($item);
$add->where($op, "CAST($tablealias.$colname AS DECIMAL) $comp CAST($pl AS DECIMAL)");
}
}
/**
* Only exact matches for numbers
*
* @return string
*/
public function getDefaultComparator()
{
return '=';
}
}