-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNumberRange.php
More file actions
129 lines (115 loc) · 3.13 KB
/
Copy pathNumberRange.php
File metadata and controls
129 lines (115 loc) · 3.13 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
<?php
namespace ModStart\Field;
use ModStart\Core\Exception\BizException;
use ModStart\Core\Util\SerializeUtil;
class NumberRange extends AbstractField
{
protected function setup()
{
$this->addVariables([
// 自动着色,正数绿色,负数红色
'autoColor' => false,
// 是否显示符号
'signShow' => false,
// 单位
'unit' => null,
// 最小值
'min' => null,
// 最大值
'max' => null,
// stepping interval
'step' => null,
]);
}
public function min($value)
{
$this->setVariable('min', $value);
return $this;
}
public function max($value)
{
$this->setVariable('max', $value);
return $this;
}
public function step($value)
{
$this->setVariable('step', $value);
return $this;
}
public function autoColor($enable)
{
$this->setVariable('autoColor', $enable);
return $this;
}
public function signShow($enable)
{
$this->setVariable('signShow', $enable);
return $this;
}
public function unit($value)
{
$this->setVariable('unit', $value);
return $this;
}
public function unserializeValue($value, AbstractField $field)
{
if (null === $value) {
return $value;
}
$value = @json_decode($value, true);
if (empty($value)) {
$value = [
'min' => '',
'max' => '',
];
}
return $value;
}
public function serializeValue($value, $model)
{
return SerializeUtil::jsonEncode($value);
}
public function prepareInput($value, $model)
{
$value = @json_decode($value, true);
if (empty($value)) {
$value = [
'min' => '',
'max' => '',
];
}
if (
('' === $value['min'] || null === $value['min'])
&&
('' === $value['max'] || null === $value['max'])
) {
return null;
}
$value['min'] = intval($value['min']);
$value['max'] = intval($value['max']);
if ($value['min'] > $value['max']) {
BizException::throws('最小值不能大于最大值');
}
foreach (['min', 'max'] as $k) {
if (null !== ($v = $this->getVariable('min'))) {
if ($value[$k] < $v) {
BizException::throws(str_replace([
':attribute', ':min'
], [
$this->label, $v
], L('validation.min.numeric')));
}
}
if (null !== ($v = $this->getVariable('max'))) {
if ($value[$k] > $v) {
BizException::throws(str_replace([
':attribute', ':max'
], [
$this->label, $v
], L('validation.max.numeric')));
}
}
}
return $value;
}
}