Skip to content

Commit

Permalink
Increase fault tolerance
Browse files Browse the repository at this point in the history
  • Loading branch information
张伟健 committed Jun 12, 2019
1 parent 7f798e1 commit 62ee5b7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 24 deletions.
5 changes: 5 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<php>
</php>
</phpunit>
31 changes: 12 additions & 19 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,34 @@ class Validator
public function validate(array $data, array $rule, array $message)
{
foreach ($rule as $key => $val) {

$temp = explode('|', $val);
$params = $temp;

foreach ($params as $value) {

if (!array_key_exists($key, $rule)) {

continue;
}

if (!isset($data[$key])) {
throw new \Exception('missing parameter');
}

$args = [$data[$key]];

if (strpos($value, ':') !== false) {

$tmp = explode(':', $value);

$func = '_' . $tmp[0];
array_push($args, $tmp[1]);

unset($tmp);
} else {

$func = '_' . $value;
}

$ret = call_user_func_array([$this, $func], $args);

if ($ret === false) {

$this->message = vsprintf($message[$key], $args);

return false;
Expand Down Expand Up @@ -134,10 +132,8 @@ private function _url($value)
private function _min($value, $length)
{
if (is_numeric($value)) {

return $value >= (int)$length ? true : false;
} else {

return strlen($value) >= (int)$length ? true : false;
}
}
Expand All @@ -152,10 +148,8 @@ private function _min($value, $length)
private function _max($value, $length)
{
if (is_numeric($value)) {

return $value <= (int)$length ? true : false;
} else {

return strlen($value) <= (int)$length ? true : false;
}
}
Expand All @@ -166,9 +160,10 @@ private function _max($value, $length)
* @param string $value
* @return bool
*/
private function _ip($value)
private function _ip($value, $type = 'ipv4')
{
return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false ? true : false;
$ipType = strtolower($type) !== 'ipv6' ? FILTER_FLAG_IPV4 : FILTER_FLAG_IPV6;
return filter_var($value, FILTER_VALIDATE_IP, $ipType) !== false ? true : false;
}

/**
Expand Down Expand Up @@ -214,34 +209,32 @@ private function _id_card($value)
{
// 只能是18位
if (strlen($value) != 18) {

return false;
}

// 取出本体码
$idcard_base = substr($value, 0, 17);
$idCardBase = substr($value, 0, 17);

// 取出校验码
$verify_code = substr($value, 17, 1);
$verifyCode = substr($value, 17, 1);

// 加权因子
$factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);

// 校验码对应值
$verify_code_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
$verifyCodeList = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');

// 根据前17位计算校验码
$total = 0;
for ($i = 0; $i < 17; $i++) {

$total += substr($idcard_base, $i, 1) * $factor[$i];
$total += substr($idCardBase, $i, 1) * $factor[$i];
}

// 取模
$mod = $total % 11;

// 比较校验码
return $verify_code == $verify_code_list[$mod] ? true : false;
return $verifyCode == $verifyCodeList[$mod] ? true : false;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
class TestCase extends \PHPUnit_Framework_TestCase
{

}
}
20 changes: 17 additions & 3 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
class ValidatorTest extends TestCase
{

/**
*
* @expectedException \Exception
*
* @return void
*/
public function testException()
{
$validator = new Validator();
$validator->validate(['a' => ''], ['test' => 'required'], ['test' => 'empty']);
}

public function testRequired()
{
$validator = new Validator();
Expand Down Expand Up @@ -69,15 +81,15 @@ public function testPhone()
$ret = $validator->validate(['test' => '17071106666'], ['test' => 'phone'], ['test' => 'nophone']);

$this->assertTrue($ret);

$ret = $validator->validate(['test' => '17571106666'], ['test' => 'phone'], ['test' => 'nophone']);

$this->assertTrue($ret);

$ret = $validator->validate(['test' => '19971106666'], ['test' => 'phone'], ['test' => 'nophone']);

$this->assertTrue($ret);

$ret = $validator->validate(['test' => '16671106666'], ['test' => 'phone'], ['test' => 'nophone']);

$this->assertTrue($ret);
Expand All @@ -95,8 +107,10 @@ public function testIp()
{
$validator = new Validator();
$ret = $validator->validate(['test' => '192.168.1.1'], ['test' => 'ip'], ['test' => 'noip']);
$ret1 = $validator->validate(['test' => '2001:da8:8000:1::81'], ['test' => 'ip:ipv6'], ['test' => 'noip']);

$this->assertTrue($ret);
$this->assertTrue($ret1);
}

public function testIdCard()
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<?php
$loader = require __DIR__ . '/../vendor/autoload.php';
$loader = require __DIR__ . '/../vendor/autoload.php';

0 comments on commit 62ee5b7

Please sign in to comment.