Skip to content

Commit 92f3c3d

Browse files
committed
some update. add range check for int/num validator
1 parent 4e1fb41 commit 92f3c3d

File tree

3 files changed

+71
-42
lines changed

3 files changed

+71
-42
lines changed

src/ValidationTrait.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,19 @@ protected function prepareRule(array $rule)
435435
$validator = $rule[0];
436436

437437
switch ($validator) {
438+
case 'num':
439+
case 'number':
440+
case 'string':
441+
case 'length':
442+
// fixed: 当只有 max 时,自动补充一个 min. 字符串最小长度就是 0
443+
if (isset($rule['max']) && !isset($rule['min'])) {
444+
$rule['min'] = 0;
445+
}
446+
break;
447+
case 'int':
438448
case 'size':
439449
case 'range':
440-
case 'string':
450+
case 'integer':
441451
case 'between':
442452
// fixed: 当只有 max 时,自动补充一个 min
443453
if (isset($rule['max']) && !isset($rule['min'])) {

src/ValidatorList.php

+56-41
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ public static function float($val, array $options = [], $flags = 0)
9999
}
100100

101101
/**
102-
* int 验证
102+
* int 验证 (所有的最小、最大都是包含边界值的)
103103
* @param mixed $val 要验证的变量
104-
* @param array $options 可选的选项设置
104+
* @param null|integer $min 最小值
105+
* @param null|int $max 最大值
105106
* @param int $flags 标志
106107
* FILTER_FLAG_ALLOW_OCTAL - 允许八进制数值
107108
* FILTER_FLAG_ALLOW_HEX - 允许十六进制数值
@@ -113,13 +114,29 @@ public static function float($val, array $options = [], $flags = 0)
113114
* // 'default' => 3, // value to return if the filter fails
114115
* ]
115116
*/
116-
public static function integer($val, array $options = [], $flags = 0)
117+
public static function integer($val, $min = null, $max = null, $flags = 0)
117118
{
118119
if (!is_numeric($val)) {
119120
return false;
120121
}
121122

122-
$settings = [];
123+
$options = $settings = [];
124+
$minIsNum = is_numeric($min);
125+
$maxIsNum = is_numeric($max);
126+
127+
if ($minIsNum && $maxIsNum) {
128+
if ($max > $min) {
129+
$options['min_range'] = (int)$min;
130+
$options['max_range'] = (int)$max;
131+
} else {
132+
$options['min_range'] = (int)$max;
133+
$options['max_range'] = (int)$min;
134+
}
135+
} elseif ($minIsNum) {
136+
$options['min_range'] = (int)$min;
137+
} elseif ($maxIsNum) {
138+
$options['max_range'] = (int)$max;
139+
}
123140

124141
if ($options) {
125142
$settings['options'] = $options;
@@ -136,42 +153,60 @@ public static function integer($val, array $options = [], $flags = 0)
136153
* @see ValidatorList::integer()
137154
* {@inheritdoc}
138155
*/
139-
public static function int($val, array $options = [], $flags = 0)
156+
public static function int($val, $min = null, $max = null, $flags = 0)
140157
{
141-
return self::integer($val, $options, $flags);
158+
return self::integer($val, $min, $max, $flags);
142159
}
143160

144161
/**
145162
* check var is a integer and greater than 0
146-
* @param $val
147-
* @param array $options
163+
* @param mixed $val
164+
* @param null|integer $min 最小值
165+
* @param null|int $max 最大值
148166
* @param int $flags
149167
* @return bool
150168
*/
151-
public static function number($val, array $options = [], $flags = 0)
169+
public static function number($val, $min = null, $max = null, $flags = 0)
152170
{
153-
return self::integer($val, $options, $flags) && ($val > 0);
171+
if (!is_numeric($val)) {
172+
return false;
173+
}
174+
175+
if ($val <= 0) {
176+
return false;
177+
}
178+
179+
return self::integer($val, $min, $max, $flags);
154180
}
155181

156182
/**
157183
* @see ValidatorList::number()
158184
* {@inheritdoc}
159185
*/
160-
public static function num($val, array $options = [], $flags = 0)
186+
public static function num($val, $min = null, $max = null, $flags = 0)
161187
{
162-
return self::number($val, $options, $flags);
188+
return self::number($val, $min, $max, $flags);
163189
}
164190

165191
/**
166192
* check val is a string
167193
* @param mixed $val
168-
* @param int $minLength
169-
* @param null|int $maxLength
194+
* @param int $minLen
195+
* @param null|int $maxLen
170196
* @return bool
171197
*/
172-
public static function string($val, $minLength = 0, $maxLength = null)
198+
public static function string($val, $minLen = 0, $maxLen = null)
173199
{
174-
return !\is_string($val) ? false : self::length($val, $minLength, $maxLength);
200+
if (!\is_string($val)) {
201+
return false;
202+
}
203+
204+
// only type check.
205+
if ($minLen === 0 && $maxLen === null) {
206+
return true;
207+
}
208+
209+
return self::integer(Helper::strlen($val), $minLen, $maxLen);
175210
}
176211

177212
/**
@@ -236,27 +271,7 @@ public static function size($val, $min = null, $max = null)
236271
}
237272
}
238273

239-
$options = [];
240-
$minIsNum = is_numeric($min);
241-
$maxIsNum = is_numeric($max);
242-
243-
if ($minIsNum && $maxIsNum) {
244-
if ($max > $min) {
245-
$options['min_range'] = (int)$min;
246-
$options['max_range'] = (int)$max;
247-
} else {
248-
$options['min_range'] = (int)$max;
249-
$options['max_range'] = (int)$min;
250-
}
251-
} elseif ($minIsNum) {
252-
$options['min_range'] = (int)$min;
253-
} elseif ($maxIsNum) {
254-
$options['max_range'] = (int)$max;
255-
} else {
256-
return false;
257-
}
258-
259-
return self::integer($val, $options);
274+
return self::integer($val, $min, $max);
260275
}
261276

262277
/**
@@ -302,17 +317,17 @@ public static function max($val, $maxRange)
302317
/**
303318
* 字符串/数组长度检查
304319
* @param string|array $val 字符串/数组
305-
* @param integer $minLength 最小长度
306-
* @param int $maxLength 最大长度
320+
* @param integer $minLen 最小长度
321+
* @param int $maxLen 最大长度
307322
* @return bool
308323
*/
309-
public static function length($val, $minLength = 0, $maxLength = null)
324+
public static function length($val, $minLen = 0, $maxLen = null)
310325
{
311326
if (!\is_string($val) && !\is_array($val)) {
312327
return false;
313328
}
314329

315-
return self::size($val, $minLength, $maxLength);
330+
return self::size($val, $minLen, $maxLen);
316331
}
317332

318333
/**

tests/ValidatorListTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@ public function testInteger()
2626
$this->assertFalse(ValidatorList::integer(''));
2727
$this->assertFalse(ValidatorList::integer(null));
2828
$this->assertFalse(ValidatorList::integer(false));
29+
$this->assertFalse(ValidatorList::integer(2, 5));
2930

3031
$this->assertTrue(ValidatorList::integer(0));
3132
$this->assertTrue(ValidatorList::integer(1));
3233
$this->assertTrue(ValidatorList::integer(-1));
3334
$this->assertTrue(ValidatorList::integer('1'));
35+
$this->assertTrue(ValidatorList::integer(-2, -3, 1));
36+
$this->assertTrue(ValidatorList::integer(2, 2, 5));
37+
$this->assertTrue(ValidatorList::integer(2, null, 5));
3438
}
3539

3640
public function testNumber()

0 commit comments

Comments
 (0)