diff --git a/phpunit.xml b/phpunit.xml
index 45a40e5..fd1a656 100755
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -14,6 +14,11 @@
./tests/
+
+
+ src/
+
+
\ No newline at end of file
diff --git a/src/Validator.php b/src/Validator.php
index e45c62d..853640f 100755
--- a/src/Validator.php
+++ b/src/Validator.php
@@ -28,21 +28,21 @@ 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];
@@ -50,14 +50,12 @@ public function validate(array $data, array $rule, array $message)
unset($tmp);
} else {
-
$func = '_' . $value;
}
$ret = call_user_func_array([$this, $func], $args);
if ($ret === false) {
-
$this->message = vsprintf($message[$key], $args);
return false;
@@ -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;
}
}
@@ -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;
}
}
@@ -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;
}
/**
@@ -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;
}
/**
diff --git a/tests/TestCase.php b/tests/TestCase.php
index 8462a36..88aaae8 100755
--- a/tests/TestCase.php
+++ b/tests/TestCase.php
@@ -5,4 +5,4 @@
class TestCase extends \PHPUnit_Framework_TestCase
{
-}
\ No newline at end of file
+}
diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php
index 8dcd059..fabf814 100755
--- a/tests/ValidatorTest.php
+++ b/tests/ValidatorTest.php
@@ -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();
@@ -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);
@@ -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()
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
index efff6b8..80b4ecf 100755
--- a/tests/bootstrap.php
+++ b/tests/bootstrap.php
@@ -1,2 +1,2 @@