diff --git a/README.md b/README.md index adb4028..882a214 100644 --- a/README.md +++ b/README.md @@ -624,8 +624,8 @@ public function get(string $key, $default = null) 验证器 | 说明 | 规则示例 ----------|-------------|------------ -`int/integer` | 验证是否是 int | `['userId', 'int']` -`num/number` | 验证是否是 number | `['userId', 'number']` +`int/integer` | 验证是否是 int 支持范围检查 | `['userId', 'int']` `['userId', 'int', 'min'=>4, 'max'=>16]` +`num/number` | 验证是否是 number | `['userId', 'number']` `['userId', 'number', 'min'=>4, 'max'=>16]` `bool/boolean` | 验证是否是 bool | `['open', 'bool']` `float` | 验证是否是 float | `['price', 'float']` `string` | 验证是否是 string. 支持长度检查 | `['name', 'string']`, `['name', 'string', 'min'=>4, 'max'=>16]` diff --git a/src/Filter/FilterList.php b/src/Filter/FilterList.php index 5bdf94b..da352ba 100644 --- a/src/Filter/FilterList.php +++ b/src/Filter/FilterList.php @@ -47,13 +47,14 @@ public static function abs($val) * 过滤器删除浮点数中所有非法的字符。 * @note 该过滤器默认允许所有数字以及 + - * @param mixed $val 要过滤的变量 + * @param null|int $decimal * @param int $flags 标志 * FILTER_FLAG_ALLOW_FRACTION - 允许小数分隔符 (比如 .) * FILTER_FLAG_ALLOW_THOUSAND - 允许千位分隔符(比如 ,) * FILTER_FLAG_ALLOW_SCIENTIFIC - 允许科学记数法(比如 e 和 E) * @return mixed */ - public static function float($val, $flags = FILTER_FLAG_ALLOW_FRACTION) + public static function float($val, $decimal = null, $flags = FILTER_FLAG_ALLOW_FRACTION) { $settings = []; @@ -62,8 +63,13 @@ public static function float($val, $flags = FILTER_FLAG_ALLOW_FRACTION) } $ret = filter_var($val, FILTER_SANITIZE_NUMBER_FLOAT, $settings); + $new = strpos($ret, '.') ? (float)$ret : $ret; - return strpos($ret, '.') ? (float)$ret : $ret; + if (\is_int($decimal)) { + return round($new, $decimal); + } + + return $new; } /** diff --git a/src/ValidatorList.php b/src/ValidatorList.php index d84438b..136af12 100644 --- a/src/ValidatorList.php +++ b/src/ValidatorList.php @@ -47,7 +47,7 @@ public static function isEmpty($val) * @param mixed $val 要验证的数据 * @param mixed $default 设置验证失败时返回默认值 * @param int $flags 标志 FILTER_NULL_ON_FAILURE - * @return mixed + * @return bool */ public static function boolean($val, $default = null, $flags = 0) { @@ -61,7 +61,7 @@ public static function boolean($val, $default = null, $flags = 0) $settings['flags'] = $flags; } - return filter_var($val, FILTER_VALIDATE_BOOLEAN, $settings); + return (bool)filter_var($val, FILTER_VALIDATE_BOOLEAN, $settings); } /** @@ -75,7 +75,8 @@ public static function bool($val, $default = null, $flags = 0) /** * @param mixed $val 要验证的变量 - * @param array $options 可选的选项设置 + * @param null|integer|float $min 最小值 + * @param null|int|float $max 最大值 * $options = [ * 'default' => 'default value', * 'decimal' => 2 @@ -83,19 +84,42 @@ public static function bool($val, $default = null, $flags = 0) * @param int $flags FILTER_FLAG_ALLOW_THOUSAND * @return mixed */ - public static function float($val, array $options = [], $flags = 0) + public static function float($val, $min = null, $max = null, $flags = 0) { $settings = []; - if ($options) { - $settings['options'] = $options; - } - if ($flags !== 0) { $settings['flags'] = $flags; } - return filter_var($val, FILTER_VALIDATE_FLOAT, $settings); + if (filter_var($val, FILTER_VALIDATE_FLOAT, $settings) === false) { + return false; + } + + $minIsNum = is_numeric($min); + $maxIsNum = is_numeric($max); + + if ($minIsNum && $maxIsNum) { + if ($max > $min) { + $minV = $min; + $maxV = $max; + } else { + $minV = $max; + $maxV = $min; + } + + return $val >= $minV && $val <= $maxV; + } + + if ($minIsNum) { + return $val >= $min; + } + + if ($maxIsNum) { + return $val <= $max; + } + + return true; } /** diff --git a/tests/FilterListTest.php b/tests/FilterListTest.php index 548da0a..8898ed7 100644 --- a/tests/FilterListTest.php +++ b/tests/FilterListTest.php @@ -25,6 +25,9 @@ public function testFloat() // $this->assertSame(FilterList::float('4.45'), 4.45); $this->assertSame(FilterList::float(45.78), 45.78); $this->assertSame(FilterList::float(-45.78), -45.78); + + $this->assertSame(FilterList::float(45.78678, 2), 45.79); + $this->assertSame(FilterList::float(457, 2), 457.00); } public function testTrim() diff --git a/tests/ValidatorListTest.php b/tests/ValidatorListTest.php index b7f1a04..4d91464 100644 --- a/tests/ValidatorListTest.php +++ b/tests/ValidatorListTest.php @@ -21,6 +21,30 @@ public function testIsEmpty() $this->assertTrue(ValidatorList::isEmpty(' ')); } + public function testBool() + { + $this->assertFalse(ValidatorList::bool(null)); + $this->assertFalse(ValidatorList::bool([])); + + $this->assertTrue(ValidatorList::bool('1')); + $this->assertTrue(ValidatorList::bool(1)); + } + + public function testFloat() + { + $this->assertFalse(ValidatorList::float(null)); + $this->assertFalse(ValidatorList::float(false)); + $this->assertFalse(ValidatorList::float('')); + + $this->assertTrue(ValidatorList::float('1')); + $this->assertTrue(ValidatorList::float('1.0')); + $this->assertTrue(ValidatorList::float(3.4)); + $this->assertTrue(ValidatorList::float(-3.4)); + $this->assertTrue(ValidatorList::float(3.4, 3.1)); + $this->assertTrue(ValidatorList::float(3.4, 3.1, 5.4)); + $this->assertTrue(ValidatorList::float(3.4, null, 5.4)); + } + public function testInteger() { $this->assertFalse(ValidatorList::integer(''));