1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Property; |
4
|
|
|
|
5
|
|
|
use PDO; |
6
|
|
|
|
7
|
|
|
// From 'charcoal-property' |
8
|
|
|
use Charcoal\Property\AbstractProperty; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Number Property |
12
|
|
|
*/ |
13
|
|
|
class NumberProperty extends AbstractProperty |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Minimal value. |
17
|
|
|
* |
18
|
|
|
* If null (default), then skip minimum validation (no constraint). |
19
|
|
|
* |
20
|
|
|
* @var mixed|null |
21
|
|
|
*/ |
22
|
|
|
private $min; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Maximal value. |
26
|
|
|
* |
27
|
|
|
* If null (default), then skip maximum validation (no constrant). |
28
|
|
|
* |
29
|
|
|
* @var mixed|null |
30
|
|
|
*/ |
31
|
|
|
private $max; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public function type() |
37
|
|
|
{ |
38
|
|
|
return 'number'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Set the minimal value. |
43
|
|
|
* |
44
|
|
|
* @param mixed|null $min The minimal value. |
45
|
|
|
* @return self |
46
|
|
|
*/ |
47
|
|
|
public function setMin($min) |
48
|
|
|
{ |
49
|
|
|
$this->min = $min; |
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Retrieves the minimal value. |
55
|
|
|
* |
56
|
|
|
* @return mixed|null |
57
|
|
|
*/ |
58
|
|
|
public function min() |
59
|
|
|
{ |
60
|
|
|
return $this->min; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Set the maximal value. |
65
|
|
|
* |
66
|
|
|
* @param mixed|null $max The maximal value. |
67
|
|
|
* @return self |
68
|
|
|
*/ |
69
|
|
|
public function setMax($max) |
70
|
|
|
{ |
71
|
|
|
$this->max = $max; |
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Retrieves the maximal value. |
77
|
|
|
* |
78
|
|
|
* @return mixed|null |
79
|
|
|
*/ |
80
|
|
|
public function max() |
81
|
|
|
{ |
82
|
|
|
return $this->max; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* The property's default validation methods. |
87
|
|
|
* |
88
|
|
|
* @return string[] |
89
|
|
|
*/ |
90
|
|
|
public function validationMethods() |
91
|
|
|
{ |
92
|
|
|
$parentMethods = parent::validationMethods(); |
93
|
|
|
|
94
|
|
|
return array_merge($parentMethods, [ |
95
|
|
|
'max', |
96
|
|
|
'min', |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return boolean |
102
|
|
|
*/ |
103
|
|
|
public function validateRequired() |
104
|
|
|
{ |
105
|
|
|
if ($this['required'] && !is_numeric($this->val())) { |
|
|
|
|
106
|
|
|
$this->validator()->error('Value is required.', 'required'); |
|
|
|
|
107
|
|
|
|
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return true; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return boolean |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function validateMin() |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$min = $this->min(); |
120
|
|
|
if (!$min) { |
121
|
|
|
return true; |
122
|
|
|
} |
123
|
|
|
$valid = ($this->val() >= $min); |
|
|
|
|
124
|
|
|
if ($valid === false) { |
125
|
|
|
$this->validator()->error('The number is smaller than the minimum value', 'min'); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
return $valid; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return boolean |
132
|
|
|
*/ |
133
|
|
View Code Duplication |
public function validateMax() |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
$max = $this->max(); |
136
|
|
|
if (!$max) { |
137
|
|
|
return true; |
138
|
|
|
} |
139
|
|
|
$valid = ($this->val() <= $max); |
|
|
|
|
140
|
|
|
if ($valid === false) { |
141
|
|
|
$this->validator()->error('The number is bigger than the maximum value', 'max'); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
return $valid; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get the SQL type (Storage format) |
148
|
|
|
* |
149
|
|
|
* Stored as `VARCHAR` for max_length under 255 and `TEXT` for other, longer strings |
150
|
|
|
* |
151
|
|
|
* @see StorablePropertyTrait::sqlType() |
152
|
|
|
* @return string The SQL type |
153
|
|
|
*/ |
154
|
|
|
public function sqlType() |
155
|
|
|
{ |
156
|
|
|
// Multiple number are stocked as TEXT because we do not know the maximum length |
157
|
|
|
if ($this['multiple']) { |
158
|
|
|
return 'TEXT'; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return 'DOUBLE'; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @see StorablePropertyTrait::sqlPdoType() |
166
|
|
|
* @return integer |
167
|
|
|
*/ |
168
|
|
|
public function sqlPdoType() |
169
|
|
|
{ |
170
|
|
|
return PDO::PARAM_STR; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
This method has been deprecated.