1
|
|
|
<?php |
2
|
|
|
namespace Comfort\Validator; |
3
|
|
|
|
4
|
|
|
use Comfort\Comfort; |
5
|
|
|
|
6
|
|
|
class NumberValidator extends AbstractValidator |
7
|
|
|
{ |
8
|
|
|
public function __construct(Comfort $comfort) |
9
|
|
|
{ |
10
|
|
|
parent::__construct($comfort); |
11
|
|
|
|
12
|
|
|
$this->toBool(false); |
13
|
|
|
|
14
|
|
|
$this->errorHandlers += [ |
15
|
|
|
'number.min' => [ |
16
|
|
|
'message' => '%s must be above %s' |
17
|
|
|
], |
18
|
|
|
'number.max' => [ |
19
|
|
|
'message' => '%s must be below %s' |
20
|
|
|
], |
21
|
|
|
'number.precision' => [ |
22
|
|
|
'message' => '%s must be precision %s' |
23
|
|
|
], |
24
|
|
|
'number.is_int' => [ |
25
|
|
|
'message' => '%s must be an int' |
26
|
|
|
], |
27
|
|
|
'number.is_float' => [ |
28
|
|
|
'message' => '%s must be a float' |
29
|
|
|
], |
30
|
|
|
'number.is_number' => [ |
31
|
|
|
'message' => '%s must be a number' |
32
|
|
|
], |
33
|
|
|
]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Validate number is no less than $min |
38
|
|
|
* |
39
|
|
|
* @param $min |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
|
|
public function min($min) |
43
|
|
|
{ |
44
|
|
|
return $this->add(function($value, $nameKey) use ($min) { |
45
|
|
|
if ($value < $min) { |
46
|
|
|
return $this->createError('number.min', $value, $nameKey); |
47
|
|
|
} |
48
|
|
|
}); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Validate number is no more than $max |
53
|
|
|
* |
54
|
|
|
* @param $max |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
|
|
public function max($max) |
58
|
|
|
{ |
59
|
|
|
return $this->add(function($value, $nameKey) use ($max) { |
60
|
|
|
if ($value > $max) { |
61
|
|
|
return $this->createError('number.max', $value, $nameKey); |
62
|
|
|
} |
63
|
|
|
}); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Validate precision of number is $precision |
68
|
|
|
* |
69
|
|
|
* @todo think if less kludgy way to do this... |
70
|
|
|
* @param $precision |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function precision($precision) |
74
|
|
|
{ |
75
|
|
|
return $this->add(function($value, $nameKey) use ($precision) { |
76
|
|
|
if (strlen(substr($value, strpos($value, '.') + 1)) != $precision) { |
77
|
|
|
return $this->createError('number.precision', $value, $nameKey); |
78
|
|
|
} |
79
|
|
|
}); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Validate value os an integer |
84
|
|
|
* |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
|
|
public function isInt() |
88
|
|
|
{ |
89
|
|
|
return $this->add(function($value, $nameKey) { |
90
|
|
|
if (!is_int($value)) { |
91
|
|
|
return $this->createError('number.is_int', $value, $nameKey); |
92
|
|
|
} |
93
|
|
|
}); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Validate value is a float |
98
|
|
|
* |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function isFloat() |
102
|
|
|
{ |
103
|
|
|
return $this->add(function($value, $nameKey) { |
104
|
|
|
if (!is_float($value)) { |
105
|
|
|
return $this->createError('number.is_float', $value, $nameKey); |
106
|
|
|
} |
107
|
|
|
}); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Validate value is numeric, be it float or int. |
112
|
|
|
* |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
|
|
public function isNumber() |
116
|
|
|
{ |
117
|
|
|
return $this->add(function($value, $nameKey) { |
118
|
|
|
if (!is_numeric($value)) { |
119
|
|
|
return $this->createError('number.is_numeric', $value, $nameKey); |
120
|
|
|
} |
121
|
|
|
}); |
122
|
|
|
} |
123
|
|
|
} |