Skip to content

Commit

Permalink
update readme. add range check for float validator. add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 7, 2017
1 parent 92f3c3d commit 395377b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]`
Expand Down
10 changes: 8 additions & 2 deletions src/Filter/FilterList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand All @@ -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;
}

/**
Expand Down
42 changes: 33 additions & 9 deletions src/ValidatorList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
}

/**
Expand All @@ -75,27 +75,51 @@ 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
* ]
* @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;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions tests/FilterListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
24 changes: 24 additions & 0 deletions tests/ValidatorListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(''));
Expand Down

0 comments on commit 395377b

Please sign in to comment.