Skip to content

Commit 395377b

Browse files
committed
update readme. add range check for float validator. add more tests
1 parent 92f3c3d commit 395377b

File tree

5 files changed

+70
-13
lines changed

5 files changed

+70
-13
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -624,8 +624,8 @@ public function get(string $key, $default = null)
624624
625625
验证器 | 说明 | 规则示例
626626
----------|-------------|------------
627-
`int/integer` | 验证是否是 int | `['userId', 'int']`
628-
`num/number` | 验证是否是 number | `['userId', 'number']`
627+
`int/integer` | 验证是否是 int 支持范围检查 | `['userId', 'int']` `['userId', 'int', 'min'=>4, 'max'=>16]`
628+
`num/number` | 验证是否是 number | `['userId', 'number']` `['userId', 'number', 'min'=>4, 'max'=>16]`
629629
`bool/boolean` | 验证是否是 bool | `['open', 'bool']`
630630
`float` | 验证是否是 float | `['price', 'float']`
631631
`string` | 验证是否是 string. 支持长度检查 | `['name', 'string']`, `['name', 'string', 'min'=>4, 'max'=>16]`

src/Filter/FilterList.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ public static function abs($val)
4747
* 过滤器删除浮点数中所有非法的字符。
4848
* @note 该过滤器默认允许所有数字以及 + -
4949
* @param mixed $val 要过滤的变量
50+
* @param null|int $decimal
5051
* @param int $flags 标志
5152
* FILTER_FLAG_ALLOW_FRACTION - 允许小数分隔符 (比如 .)
5253
* FILTER_FLAG_ALLOW_THOUSAND - 允许千位分隔符(比如 ,)
5354
* FILTER_FLAG_ALLOW_SCIENTIFIC - 允许科学记数法(比如 e 和 E)
5455
* @return mixed
5556
*/
56-
public static function float($val, $flags = FILTER_FLAG_ALLOW_FRACTION)
57+
public static function float($val, $decimal = null, $flags = FILTER_FLAG_ALLOW_FRACTION)
5758
{
5859
$settings = [];
5960

@@ -62,8 +63,13 @@ public static function float($val, $flags = FILTER_FLAG_ALLOW_FRACTION)
6263
}
6364

6465
$ret = filter_var($val, FILTER_SANITIZE_NUMBER_FLOAT, $settings);
66+
$new = strpos($ret, '.') ? (float)$ret : $ret;
6567

66-
return strpos($ret, '.') ? (float)$ret : $ret;
68+
if (\is_int($decimal)) {
69+
return round($new, $decimal);
70+
}
71+
72+
return $new;
6773
}
6874

6975
/**

src/ValidatorList.php

+33-9
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static function isEmpty($val)
4747
* @param mixed $val 要验证的数据
4848
* @param mixed $default 设置验证失败时返回默认值
4949
* @param int $flags 标志 FILTER_NULL_ON_FAILURE
50-
* @return mixed
50+
* @return bool
5151
*/
5252
public static function boolean($val, $default = null, $flags = 0)
5353
{
@@ -61,7 +61,7 @@ public static function boolean($val, $default = null, $flags = 0)
6161
$settings['flags'] = $flags;
6262
}
6363

64-
return filter_var($val, FILTER_VALIDATE_BOOLEAN, $settings);
64+
return (bool)filter_var($val, FILTER_VALIDATE_BOOLEAN, $settings);
6565
}
6666

6767
/**
@@ -75,27 +75,51 @@ public static function bool($val, $default = null, $flags = 0)
7575

7676
/**
7777
* @param mixed $val 要验证的变量
78-
* @param array $options 可选的选项设置
78+
* @param null|integer|float $min 最小值
79+
* @param null|int|float $max 最大值
7980
* $options = [
8081
* 'default' => 'default value',
8182
* 'decimal' => 2
8283
* ]
8384
* @param int $flags FILTER_FLAG_ALLOW_THOUSAND
8485
* @return mixed
8586
*/
86-
public static function float($val, array $options = [], $flags = 0)
87+
public static function float($val, $min = null, $max = null, $flags = 0)
8788
{
8889
$settings = [];
8990

90-
if ($options) {
91-
$settings['options'] = $options;
92-
}
93-
9491
if ($flags !== 0) {
9592
$settings['flags'] = $flags;
9693
}
9794

98-
return filter_var($val, FILTER_VALIDATE_FLOAT, $settings);
95+
if (filter_var($val, FILTER_VALIDATE_FLOAT, $settings) === false) {
96+
return false;
97+
}
98+
99+
$minIsNum = is_numeric($min);
100+
$maxIsNum = is_numeric($max);
101+
102+
if ($minIsNum && $maxIsNum) {
103+
if ($max > $min) {
104+
$minV = $min;
105+
$maxV = $max;
106+
} else {
107+
$minV = $max;
108+
$maxV = $min;
109+
}
110+
111+
return $val >= $minV && $val <= $maxV;
112+
}
113+
114+
if ($minIsNum) {
115+
return $val >= $min;
116+
}
117+
118+
if ($maxIsNum) {
119+
return $val <= $max;
120+
}
121+
122+
return true;
99123
}
100124

101125
/**

tests/FilterListTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public function testFloat()
2525
// $this->assertSame(FilterList::float('4.45'), 4.45);
2626
$this->assertSame(FilterList::float(45.78), 45.78);
2727
$this->assertSame(FilterList::float(-45.78), -45.78);
28+
29+
$this->assertSame(FilterList::float(45.78678, 2), 45.79);
30+
$this->assertSame(FilterList::float(457, 2), 457.00);
2831
}
2932

3033
public function testTrim()

tests/ValidatorListTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,30 @@ public function testIsEmpty()
2121
$this->assertTrue(ValidatorList::isEmpty(' '));
2222
}
2323

24+
public function testBool()
25+
{
26+
$this->assertFalse(ValidatorList::bool(null));
27+
$this->assertFalse(ValidatorList::bool([]));
28+
29+
$this->assertTrue(ValidatorList::bool('1'));
30+
$this->assertTrue(ValidatorList::bool(1));
31+
}
32+
33+
public function testFloat()
34+
{
35+
$this->assertFalse(ValidatorList::float(null));
36+
$this->assertFalse(ValidatorList::float(false));
37+
$this->assertFalse(ValidatorList::float(''));
38+
39+
$this->assertTrue(ValidatorList::float('1'));
40+
$this->assertTrue(ValidatorList::float('1.0'));
41+
$this->assertTrue(ValidatorList::float(3.4));
42+
$this->assertTrue(ValidatorList::float(-3.4));
43+
$this->assertTrue(ValidatorList::float(3.4, 3.1));
44+
$this->assertTrue(ValidatorList::float(3.4, 3.1, 5.4));
45+
$this->assertTrue(ValidatorList::float(3.4, null, 5.4));
46+
}
47+
2448
public function testInteger()
2549
{
2650
$this->assertFalse(ValidatorList::integer(''));

0 commit comments

Comments
 (0)