1 | <?php |
||
15 | class RangeValidator extends ValueValidatorObject { |
||
16 | |||
17 | /** |
||
18 | * Lower bound of the range (included). Either a number or false, for no lower limit. |
||
19 | * |
||
20 | * @since 0.1 |
||
21 | * |
||
22 | * @var false|int|float |
||
23 | */ |
||
24 | protected $lowerBound = false; |
||
25 | |||
26 | /** |
||
27 | * Upper bound of the range (included). Either a number or false, for no upper limit. |
||
28 | * |
||
29 | * @since 0.1 |
||
30 | * |
||
31 | * @var false|int|float |
||
32 | */ |
||
33 | protected $upperBound = false; |
||
34 | |||
35 | /** |
||
36 | * Sets the lower bound (included). |
||
37 | * |
||
38 | * @since 0.1 |
||
39 | * |
||
40 | * @param $lowerBound false|int|float |
||
41 | */ |
||
42 | public function setLowerBound( $lowerBound ) { |
||
45 | |||
46 | /** |
||
47 | * Sets the upper bound (included). |
||
48 | * |
||
49 | * @since 0.1 |
||
50 | * |
||
51 | * @param $upperBound false|int|float |
||
52 | */ |
||
53 | public function setUpperBound( $upperBound ) { |
||
56 | |||
57 | /** |
||
58 | * Requires the value to be in the specified range. |
||
59 | * |
||
60 | * @since 0.1 |
||
61 | * |
||
62 | * @param $lowerBound false|int|float |
||
63 | * @param $upperBound false|int|float |
||
64 | */ |
||
65 | public function setRange( $lowerBound, $upperBound ) { |
||
69 | |||
70 | /** |
||
71 | * Requires the value to be within the range of a point. |
||
72 | * Bounds are included, ie 6 will be valid for point 4 within range 2. |
||
73 | * |
||
74 | * @since 0.1 |
||
75 | * |
||
76 | * @param int|float $point |
||
77 | * @param int|float $range |
||
78 | */ |
||
79 | public function setWithinRange( $point, $range ) { |
||
83 | |||
84 | /** |
||
85 | * @see ValueValidatorObject::doValidation |
||
86 | * |
||
87 | * @since 0.1 |
||
88 | * |
||
89 | * @param mixed $value |
||
90 | */ |
||
91 | public function doValidation( $value ) { |
||
100 | |||
101 | /** |
||
102 | * Validates the parameters value and returns the result. |
||
103 | * |
||
104 | * @since 0.1 |
||
105 | * |
||
106 | * @param $value mixed |
||
107 | * @param float|null|false $upperBound |
||
108 | * @param float|null|false $lowerBound |
||
109 | * |
||
110 | * @return boolean |
||
111 | */ |
||
112 | protected function validateBounds( $value, $upperBound = null, $lowerBound = null ) { |
||
113 | $upperBound = $upperBound ?? $this->upperBound; |
||
114 | $lowerBound = $lowerBound ?? $this->lowerBound; |
||
115 | |||
116 | $smallEnough = $upperBound === false || $value <= $upperBound; |
||
117 | $bigEnough = $lowerBound === false || $value >= $lowerBound; |
||
118 | |||
119 | if ( !$smallEnough ) { |
||
120 | $this->addErrorMessage( 'Value exceeding upper bound' ); |
||
121 | } |
||
122 | |||
123 | if ( !$bigEnough ) { |
||
124 | $this->addErrorMessage( 'Value exceeding lower bound' ); |
||
125 | } |
||
126 | |||
127 | return $smallEnough && $bigEnough; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @see ValueValidator::setOptions |
||
132 | * |
||
133 | * @since 0.1 |
||
134 | * |
||
135 | * @param array $options |
||
136 | * @throws Exception |
||
137 | */ |
||
138 | public function setOptions( array $options ) { |
||
165 | |||
166 | } |
||
167 |