|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package Fuel\Validation |
|
4
|
|
|
* @version 2.0 |
|
5
|
|
|
* @author Fuel Development Team |
|
6
|
|
|
* @license MIT License |
|
7
|
|
|
* @copyright 2010 - 2013 Fuel Development Team |
|
8
|
|
|
* @link http://fuelphp.com |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Fuel\Validation\Rule; |
|
12
|
|
|
|
|
13
|
|
|
use Fuel\Validation\AbstractRule; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Checks that a numerical value is between the two given parameters. |
|
17
|
|
|
* |
|
18
|
|
|
* setParameters() should be called with an array where the first value is the lower bound and the second value is the |
|
19
|
|
|
* upper bound. |
|
20
|
|
|
* |
|
21
|
|
|
* @package Fuel\Validation\Rule |
|
22
|
|
|
* @author Fuel Development Team |
|
23
|
|
|
* |
|
24
|
|
|
* @since 2.0 |
|
25
|
|
|
*/ |
|
26
|
|
|
class NumericBetween extends AbstractRule |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Contains the rule failure message |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $message = 'The field is not between the specified values.'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param mixed $value Value to validate |
|
38
|
|
|
* @param string $field Unused by this rule |
|
39
|
|
|
* @param array $allFields Unused by this rule |
|
40
|
|
|
* |
|
41
|
|
|
* @return bool |
|
42
|
|
|
* |
|
43
|
|
|
* @since 2.0 |
|
44
|
|
|
*/ |
|
45
|
10 |
|
public function validate($value, $field = null, $allFields = null) |
|
46
|
|
|
{ |
|
47
|
10 |
|
$params = $this->getParameter(); |
|
48
|
|
|
|
|
49
|
10 |
|
if ( ! $this->paramsValid($params) or ! is_numeric($value)) |
|
50
|
|
|
{ |
|
51
|
4 |
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// We know the params are valid at this point so pull them out |
|
55
|
6 |
|
list($lower, $upper) = $params; |
|
56
|
|
|
|
|
57
|
6 |
|
return ($value >= $lower and $value <= $upper); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Returns true if the given params are not null, an array and has at least two values |
|
62
|
|
|
* |
|
63
|
|
|
* @param $params |
|
64
|
|
|
* |
|
65
|
|
|
* @return bool |
|
66
|
|
|
* |
|
67
|
|
|
* @since 2.0 |
|
68
|
|
|
*/ |
|
69
|
10 |
|
protected function paramsValid($params) |
|
70
|
|
|
{ |
|
71
|
10 |
|
if ($params === null or ! is_array($params) or count($params) < 2) |
|
72
|
|
|
{ |
|
73
|
1 |
|
return false; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
9 |
|
return true; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Returns |
|
81
|
|
|
* |
|
82
|
|
|
* array( |
|
83
|
|
|
* 'lowerBound' => <lower bound number>, |
|
84
|
|
|
* 'upperBound' => <upper bound number>, |
|
85
|
|
|
* ) |
|
86
|
|
|
* |
|
87
|
|
|
* @return string[] |
|
88
|
|
|
* |
|
89
|
|
|
* @since 2.0 |
|
90
|
|
|
*/ |
|
91
|
2 |
|
public function getMessageParameters() |
|
92
|
|
|
{ |
|
93
|
2 |
|
$upperBound = null; |
|
94
|
2 |
|
$lowerBound = null; |
|
95
|
|
|
|
|
96
|
2 |
|
$params = $this->getParameter(); |
|
97
|
|
|
|
|
98
|
2 |
|
if (isset($params[0])) |
|
99
|
|
|
{ |
|
100
|
1 |
|
$lowerBound = $params[0]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
2 |
|
if (isset($params[1])) |
|
104
|
|
|
{ |
|
105
|
1 |
|
$upperBound = $params[1]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return array( |
|
109
|
2 |
|
'lowerBound' => $lowerBound, |
|
110
|
2 |
|
'upperBound' => $upperBound, |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|