diff --git a/README.md b/README.md
index a4ff4d9..9d4071f 100644
--- a/README.md
+++ b/README.md
@@ -15,10 +15,9 @@
- 支持基本的数组检查,数组的子级值检查
- 方便的获取错误信息,验证后的安全数据获取
- 已经内置了40多个常用的验证器[内置验证器](#built-in-validators)
-- 规则设置参考自 yii 的。部分规则参考自 laravel
+- 规则设置参考 yii. 部分规则参考自 laravel, Respect/Validation
- 新增了独立的过滤器 `Inhere\Validate\Filter\Filtration` 用于数据过滤
-
支持两种规则配置方式:
- `Validation/RuleValidation` 规则配置类似于Yii: 每条规则中,允许多个字段,但只能有一个验证器。
@@ -598,17 +597,19 @@ public function get(string $key, $default = null)
过滤器 | 说明 | 示例
-------|-------------|------------
-`int/integer` | 过滤非法字符并转换为`int`类型 | `['userId', 'number', 'filter' => 'int'],`
+`abs` | 返回绝对值 | `['field', 'int', 'filter' => 'abs'],`
+`int/integer` | 过滤非法字符并转换为`int`类型 **支持数组** | `['userId', 'number', 'filter' => 'int'],`
`float` | 过滤非法字符,保留`float`格式的数据 | `['price', 'float', 'filter' => 'float'],`
`string` | 过滤非法字符并转换为`string`类型 | `['userId', 'number', 'filter' => 'string'],`
`trim` | 去除首尾空白字符,支持数组。 | `['username', 'min', 4, 'filter' => 'trim'],`
+`nl2br` | 转换 `\n` `\r\n` `\r` 为 `
` | `['content', 'string', 'filter' => 'nl2br'],`
`lower/lowercase` | 字符串转换为小写 | `['description', 'string', 'filter' => 'lowercase'],`
`upper/uppercase` | 字符串转换为大写 | `['title', 'string', 'filter' => 'uppercase'],`
`snake/snakeCase` | 字符串转换为蛇形风格 | `['title', 'string', 'filter' => 'snakeCase'],`
`camel/camelCase` | 字符串转换为驼峰风格 | `['title', 'string', 'filter' => 'camelCase'],`
`timestamp/strToTime` | 字符串日期转换时间戳 | `['pulishedAt', 'number', 'filter' => 'strToTime'],`
-`abs` | 返回绝对值 | `['field', 'int', 'filter' => 'abs'],`
`url` | URL 过滤,移除所有不符合 URL 的字符 | `['field', 'url', 'filter' => 'url'],`
+`str2list/str2array` | 字符串转数组 `'tag0,tag1' -> ['tag0', 'tag1']` | `['tags', 'strList', 'filter' => 'str2array'],`
`email` | email 过滤,移除所有不符合 email 的字符 | `['field', 'email', 'filter' => 'email'],`
`encoded` | 去除 URL 编码不需要的字符,与 `urlencode()` 函数很类似 | `['imgUrl', 'url', 'filter' => 'encoded'],`
`clearTags/stripTags` | 相当于使用 `strip_tags()` | `['content', 'string', 'filter' => 'clearTags'],`
@@ -624,11 +625,11 @@ public function get(string $key, $default = null)
验证器 | 说明 | 规则示例
----------|-------------|------------
-`int/integer` | 验证是否是 int 支持范围检查 | `['userId', 'int']` `['userId', 'int', 'min'=>4, 'max'=>16]`
-`num/number` | 验证是否是 number | `['userId', 'number']` `['userId', 'number', 'min'=>4, 'max'=>16]`
+`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]`
+`string` | 验证是否是 string. **支持长度检查** | `['name', 'string']`, `['name', 'string', 'min'=>4, 'max'=>16]`
`url` | 验证是否是 url | `['myUrl', 'url']`
`email` | 验证是否是 email | `['userEmail', 'email']`
`alpha` | 验证值是否仅包含字母字符 | `['name', 'alpha']`
@@ -637,6 +638,7 @@ public function get(string $key, $default = null)
`isMap` | 验证值是否是一个非自然数组 map (key - value 形式的) | `['goods', 'isMap']`
`isList` | 验证值是否是一个自然数组 list (key是从0自然增长的) | `['tags', 'isList']`
`isArray` | 验证是否是数组 | `['goods', 'isArray']`
+`hasKey` | 验证数组存在给定的key(s) | `['goods', 'hasKey', 'pear']` `['goods', 'hasKey', ['pear', 'banana']]`
`intList` | 验证字段值是否是一个 int list | `['tagIds', 'intList']`
`numList` | 验证字段值是否是一个 number list | `['tagIds', 'numList']`
`strList` | 验证字段值是否是一个 string list | `['tags', 'strList']`
@@ -645,6 +647,8 @@ public function get(string $key, $default = null)
`size/range/between` | 验证大小范围, 可以支持验证 `int`, `string`, `array` 数据类型 | `['tagId', 'size', 'min'=>4, 'max'=>567]`
`length` | 长度验证( 跟 `size`差不多, 但只能验证 `string`, `array` 的长度 | `['username', 'length', 'min' => 5, 'max' => 20]`
`fixedSize/fixedLength` | 固定的长度/大小 | `['field', 'fixedSize', 12]`
+`startWith` | 值(`string/array`)是以给定的字符串开始 | `['field', 'startWith', 'hell']`
+`endWith` | 值(`string/array`)是以给定的字符串结尾 | `['field', 'endWith', 'world']`
`in/enum` | 枚举验证 | `['status', 'in', [1,2,3]]`
`notIn` | 枚举验证 | `['status', 'notIn', [4,5,6]]`
`mustBe` | 必须是等于给定值 | `['status', 'mustBe', 1]`
@@ -671,6 +675,7 @@ public function get(string $key, $default = null)
`ip` | 验证是否是 IP | `['ipAddr', 'ip']`
`ipv4` | 验证是否是 IPv4 | `['ipAddr', 'ipv4']`
`ipv6` | 验证是否是 IPv6 | `['ipAddr', 'ipv6']`
+`macAddress` | 验证是否是 mac Address | `['field', 'macAddress']`
`md5` | 验证是否是 md5 格式的字符串 | `['passwd', 'md5']`
`sha1` | 验证是否是 sha1 格式的字符串 | `['passwd', 'sha1']`
`color` | 验证是否是html color | `['backgroundColor', 'color']`
diff --git a/examples/help.md b/examples/help.md
new file mode 100644
index 0000000..0beebb0
--- /dev/null
+++ b/examples/help.md
@@ -0,0 +1,60 @@
+# help
+
+
+```php
+
+ /**
+ * @link http://php.net/manual/zh/function.filter-input.php
+ * @param int $type INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV
+ * @param $varName
+ * @param array $filter 过滤/验证器 {@link http://php.net/manual/zh/filter.filters.php}
+ * @param array $options 一个选项的关联数组,或者按位区分的标示。
+ * 如果过滤器接受选项,可以通过数组的 "flags" 位去提供这些标示。
+ * 如果成功的话返回所请求的变量。
+ * 如果成功的话返回所请求的变量。
+ * 如果过滤失败则返回 FALSE ,
+ * 如果 varName 不存在的话则返回 NULL 。
+ * 如果标示 FILTER_NULL_ON_FAILURE 被使用了,那么当变量不存在时返回 FALSE ,当过滤失败时返回 NULL 。
+ */
+ public static function input($type, $varName, $filter, array $options = [])
+ {
+ }
+
+ public static function multi(array $data, array $filters = [])
+ {
+ }
+
+ /**
+ * @link http://php.net/manual/zh/function.filter-input-array.php
+ * 检查(验证/过滤)输入数据中的多个变量名 like filter_input_array()
+ * 当需要获取很多变量却不想重复调用 filter_input()时很有用。
+ * @param int $type One of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV. 要检查的输入数据
+ * @param mixed $definition 一个定义参数的数组。
+ * 一个有效的键必须是一个包含变量名的string,
+ * 一个有效的值要么是一个filter type,或者是一个array 指明了过滤器、标示和选项。
+ * 如果值是一个数组,那么它的有效的键可以是 :
+ * filter, 用于指明 filter type,
+ * flags 用于指明任何想要用于过滤器的标示,
+ * options 用于指明任何想要用于过滤器的选项。
+ * 参考下面的例子来更好的理解这段说明。
+ * @param bool $addEmpty 在返回值中添加 NULL 作为不存在的键。
+ * 如果成功的话返回一个所请求的变量的数组,
+ * 如果失败的话返回 FALSE 。
+ * 对于数组的值,
+ * 如果过滤失败则返回 FALSE ,
+ * 如果 variable_name 不存在的话则返回 NULL 。
+ * 如果标示 FILTER_NULL_ON_FAILURE 被使用了,那么当变量不存在时返回 FALSE ,当过滤失败时返回 NULL 。
+ */
+ public static function inputMulti($type, $definition, $addEmpty = true)
+ {
+ }
+
+ /**
+ * 检查变量名是否存在
+ * @param int $type One of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV. 要检查的输入数据
+ * @param string $varName Name of a variable to check. 要检查的变量名
+ */
+ public static function inputHasVar($type, $varName)
+ {
+ }
+```
\ No newline at end of file
diff --git a/src/Filter/FilterList.php b/src/Filter/FilterList.php
index da352ba..fcf36cd 100644
--- a/src/Filter/FilterList.php
+++ b/src/Filter/FilterList.php
@@ -22,6 +22,10 @@ final class FilterList
*/
public static function integer($val)
{
+ if (\is_array($val)) {
+ return array_map(self::class . '::integer', $val);
+ }
+
return (int)filter_var($val, FILTER_SANITIZE_NUMBER_INT);
}
@@ -95,6 +99,43 @@ public static function string($val, $flags = 0)
return filter_var($val, FILTER_SANITIZE_FULL_SPECIAL_CHARS, $settings);
}
+ /**
+ * Convert \n and \r\n and \r to
+ * @param string $str String to transform
+ * @return string New string
+ */
+ public static function nl2br($str)
+ {
+ return str_replace(["\r\n", "\r", "\n"], '
', $str);
+ }
+
+ /**
+ * @param string $str
+ * @param string $sep
+ * @return array
+ */
+ public static function str2list($str, $sep = ',')
+ {
+ return self::str2array($str, $sep);
+ }
+
+ /**
+ * var_dump(str2array('34,56,678, 678, 89, '));
+ * @param string $str
+ * @param string $sep
+ * @return array
+ */
+ public static function str2array($str, $sep = ',')
+ {
+ $str = trim($str, "$sep ");
+
+ if (!$str) {
+ return [];
+ }
+
+ return preg_split("/\s*$sep\s*/", $str, -1, PREG_SPLIT_NO_EMPTY);
+ }
+
/**
* @see FilterList::string()
* {@inheritdoc}
diff --git a/src/ValidationTrait.php b/src/ValidationTrait.php
index 92dda62..4ad32f9 100644
--- a/src/ValidationTrait.php
+++ b/src/ValidationTrait.php
@@ -520,7 +520,7 @@ public function getScene(): string
*/
public function setScene(string $scene)
{
- $this->scene = $scene;
+ $this->scene = trim($scene);
return $this;
}
@@ -535,6 +535,16 @@ public function atScene(string $scene)
return $this->setScene($scene);
}
+ /**
+ * alias of the `setScene()`
+ * @param string $scene
+ * @return static
+ */
+ public function onScene(string $scene)
+ {
+ return $this->setScene($scene);
+ }
+
/**
* Get all items in collection
* @return array The collection's source data
diff --git a/src/ValidatorList.php b/src/ValidatorList.php
index 222fda5..3df7d56 100644
--- a/src/ValidatorList.php
+++ b/src/ValidatorList.php
@@ -386,9 +386,36 @@ public static function fixedSize($val, $size)
}
/*******************************************************************************
- * custom validators
+ * extra string validators
******************************************************************************/
+ /**
+ * 值是否包含给的数据
+ * @param string|mixed $val
+ * @param string|array $needle
+ * @return bool
+ */
+ public static function contains($val, $needle)
+ {
+ if (!$val || !\is_string($val)) {
+ return false;
+ }
+
+ if (\is_string($needle)) {
+ return stripos($val, $needle) !== false;
+ }
+
+ if (\is_array($needle)) {
+ foreach ((array)$needle as $item) {
+ if (stripos($val, $item) !== false) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
/**
* 用正则验证数据
* @param string $val 要验证的数据
@@ -503,8 +530,56 @@ public static function ipv6($val)
return self::ip($val, false, FILTER_FLAG_IPV6);
}
+ /**
+ * mac Address
+ * @param string $input
+ * @return bool
+ */
+ public static function macAddress($input)
+ {
+ return !empty($input) && preg_match('/^(([0-9a-fA-F]{2}-){5}|([0-9a-fA-F]{2}:){5})[0-9a-fA-F]{2}$/', $input);
+ }
+
+ /**
+ * english chars string
+ * @param string $val
+ * @return bool
+ */
+ public static function english($val)
+ {
+ if (!$val || !\is_string($val)) {
+ return false;
+ }
+
+ return preg_match('/^[A-Za-z]+$/', $val) === 1;
+ }
+
+ /**
+ * 验证字段值是否是一个有效的 JSON 字符串。
+ * @param mixed $val
+ * @param bool $strict
+ * @return bool
+ */
+ public static function json($val, $strict = true)
+ {
+ if (!$val || (!\is_string($val) && !method_exists($val, '__toString'))) {
+ return false;
+ }
+
+ $val = (string)$val;
+
+ // must start with: { OR [
+ if ($strict && '[' !== $val[0] && '{' !== $val[0]) {
+ return false;
+ }
+
+ json_decode($val);
+
+ return json_last_error() === JSON_ERROR_NONE;
+ }
+
/*******************************************************************************
- * list/map/enum validators
+ * array(list/map/enum) validators
******************************************************************************/
/**
@@ -552,57 +627,65 @@ public static function isList($val)
}
/**
- * 验证字段值是否是一个 int list
+ * 验证字段值是否是一个 int list(key是从0自然增长的, val是数字)
* @param array|mixed $val
* @return bool
*/
public static function intList($val)
{
- if (!$val || !\is_array($val)) {
+ if (!\is_array($val) || !isset($val[0])) {
return false;
}
+ $lastK = -1;
+
/** @var array $val */
foreach ($val as $k => $v) {
- if (!\is_int($k)) {
+ if (!\is_int($k) || $k !== $lastK + 1) {
return false;
}
if (!is_numeric($v)) {
return false;
}
+
+ $lastK = $k;
}
return true;
}
/**
- * 验证字段值是否是一个 number list
+ * 验证字段值是否是一个 number list(key是从0自然增长的, val是大于0的数字)
* @param array|mixed $val
* @return bool
*/
public static function numList($val)
{
- if (!$val || !\is_array($val)) {
+ if (!\is_array($val) || !isset($val[0])) {
return false;
}
+ $lastK = -1;
+
/** @var array $val */
foreach ($val as $k => $v) {
- if (!\is_int($k)) {
+ if (!\is_int($k) || $k !== $lastK + 1) {
return false;
}
if (!is_numeric($v) || $v <= 0) {
return false;
}
+
+ $lastK = $k;
}
return true;
}
/**
- * 验证字段值是否是一个 string list
+ * 验证字段值是否是一个 string list(key是从0自然增长的, val是 string)
* @param array|mixed $val
* @return bool
*/
@@ -612,42 +695,46 @@ public static function strList($val)
return false;
}
+ $lastK = -1;
+
/** @var array $val */
foreach ($val as $k => $v) {
- if (!\is_int($k)) {
+ if (!\is_int($k) || $k !== $lastK + 1) {
return false;
}
if (!\is_string($v)) {
return false;
}
+
+ $lastK = $k;
}
return true;
}
/**
- * 验证字段值是否是一个有效的 JSON 字符串。
- * @param mixed $val
- * @param bool $strict
+ * @param array|mixed $val
+ * @param string|int|array $key
* @return bool
*/
- public static function json($val, $strict = true)
+ public static function hasKey($val, $key)
{
- if (!$val || (!\is_string($val) && !method_exists($val, '__toString'))) {
+ if (!$val || !\is_array($val)) {
return false;
}
- $val = (string)$val;
-
- // must start with: { OR [
- if ($strict && '[' !== $val[0] && '{' !== $val[0]) {
- return false;
+ if (\is_string($key) || \is_int($key)) {
+ return array_key_exists($key, $val);
}
- json_decode($val);
+ if (\is_array($key)) {
+ $keys = array_keys($val);
- return json_last_error() === JSON_ERROR_NONE;
+ return !array_diff($key, $keys);
+ }
+
+ return false;
}
/**
@@ -689,6 +776,55 @@ public static function notIn($val, $dict)
return !\in_array($val, (array)$dict, true);
}
+ /*******************************************************************************
+ * mixed data validators
+ ******************************************************************************/
+
+ /**
+ * @param mixed $val
+ * @param string $start
+ * @param bool $identical
+ * @return bool
+ */
+ public static function startWith($val, $start, $identical = true)
+ {
+ $start = (string)$start;
+
+ if (\is_string($val)) {
+ return ($identical ? strpos($val, $start) : stripos($val, $start)) === 0;
+ }
+
+ if (\is_array($val)) {
+ $first = array_shift($val);
+
+ return $identical ? $first === $start : $first == $start;
+ }
+
+ return false;
+ }
+
+ /**
+ * @param mixed $val
+ * @param string $end
+ * @param bool $identical
+ * @return bool
+ */
+ public static function endWith($val, $end, $identical = true)
+ {
+ $last = null;
+ $end = (string)$end;
+
+ if (\is_string($val)) {
+ $last = substr($val, - Helper::strlen($end));
+ }
+
+ if (\is_array($val)) {
+ $last = array_pop($val);
+ }
+
+ return $identical ? $last === $end : $last == $end;
+ }
+
/**
* 必须是等于给定值
* @param mixed $val
@@ -1021,62 +1157,4 @@ public static function dirName($dir)
{
return (bool)preg_match('/^[a-zA-Z0-9_.-]*$/', $dir);
}
-
- ///////////////////////////////////////////
-
- /**
- * @link http://php.net/manual/zh/function.filter-input.php
- * @param int $type INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV
- * @param $varName
- * @param array $filter 过滤/验证器 {@link http://php.net/manual/zh/filter.filters.php}
- * @param array $options 一个选项的关联数组,或者按位区分的标示。
- * 如果过滤器接受选项,可以通过数组的 "flags" 位去提供这些标示。
- * 如果成功的话返回所请求的变量。
- * 如果成功的话返回所请求的变量。
- * 如果过滤失败则返回 FALSE ,
- * 如果 varName 不存在的话则返回 NULL 。
- * 如果标示 FILTER_NULL_ON_FAILURE 被使用了,那么当变量不存在时返回 FALSE ,当过滤失败时返回 NULL 。
- */
- public static function input($type, $varName, $filter, array $options = [])
- {
- }
-
- public static function multi(array $data, array $filters = [])
- {
- }
-
- /**
- * @link http://php.net/manual/zh/function.filter-input-array.php
- * 检查(验证/过滤)输入数据中的多个变量名 like filter_input_array()
- * 当需要获取很多变量却不想重复调用 filter_input()时很有用。
- * @param int $type One of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV. 要检查的输入数据
- * @param mixed $definition 一个定义参数的数组。
- * 一个有效的键必须是一个包含变量名的string,
- * 一个有效的值要么是一个filter type,或者是一个array 指明了过滤器、标示和选项。
- * 如果值是一个数组,那么它的有效的键可以是 :
- * filter, 用于指明 filter type,
- * flags 用于指明任何想要用于过滤器的标示,
- * options 用于指明任何想要用于过滤器的选项。
- * 参考下面的例子来更好的理解这段说明。
- * @param bool $addEmpty 在返回值中添加 NULL 作为不存在的键。
- * 如果成功的话返回一个所请求的变量的数组,
- * 如果失败的话返回 FALSE 。
- * 对于数组的值,
- * 如果过滤失败则返回 FALSE ,
- * 如果 variable_name 不存在的话则返回 NULL 。
- * 如果标示 FILTER_NULL_ON_FAILURE 被使用了,那么当变量不存在时返回 FALSE ,当过滤失败时返回 NULL 。
- */
- public static function inputMulti($type, $definition, $addEmpty = true)
- {
- }
-
- /**
- * 检查变量名是否存在
- * @param int $type One of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV. 要检查的输入数据
- * @param string $varName Name of a variable to check. 要检查的变量名
- */
- public static function inputHasVar($type, $varName)
- {
- }
-
}
diff --git a/tests/FilterListTest.php b/tests/FilterListTest.php
index 8898ed7..3035712 100644
--- a/tests/FilterListTest.php
+++ b/tests/FilterListTest.php
@@ -12,6 +12,8 @@ public function testInteger()
{
$this->assertSame(FilterList::integer('456'), 456);
$this->assertSame(FilterList::integer('4df5dg6'), 456);
+
+ $this->assertSame(FilterList::integer(['34', '67gh']), [34, 67]);
}
public function testAbs()
@@ -46,4 +48,12 @@ public function testUppercase()
{
$this->assertSame(FilterList::uppercase('Test'), 'TEST');
}
+
+ public function testStr2list()
+ {
+ $this->assertSame(FilterList::str2array('a,b,c,'), ['a', 'b', 'c']);
+ $this->assertSame(FilterList::str2array('a, b ,c,'), ['a', 'b', 'c']);
+ $this->assertSame(FilterList::str2array(' a, b , c'), ['a', 'b', 'c']);
+ $this->assertSame(FilterList::str2array(' a,, b ,, c'), ['a', 'b', 'c']);
+ }
}
diff --git a/tests/RuleValidationTest.php b/tests/RuleValidationTest.php
index 9255a31..d4e5aec 100644
--- a/tests/RuleValidationTest.php
+++ b/tests/RuleValidationTest.php
@@ -55,6 +55,45 @@ public function testValidatePassed()
$this->assertSame($v->getSafe('tagId'), 35);
}
+ public function testCollectRules()
+ {
+ $data = [
+ 'userId' => 234,
+ 'tagId' => 35,
+ 'freeTime' => '1456767657',
+ 'status' => 2,
+ 'name' => '1234a2',
+ 'goods' => [
+ 'apple' => 34,
+ 'pear' => 50,
+ ],
+
+ ];
+
+ $rules = [
+ ['tagId,userId,freeTime', 'required'],
+ ['tagId,userId,freeTime', 'number', 'on' => 's1'],
+ ['tagId', 'size', 'max'=> 567, 'min'=> 4, 'on' => 's2'],
+ ['name', 'string', 'on' => 's2'],
+ ['goods.pear', 'max', 60],
+ ];
+
+ $v = RuleValidation::make($data, $rules)->validate();
+
+ $this->assertTrue($v->passed());
+ $this->assertCount(2, $v->getUsedRules());
+
+ $v = RuleValidation::make($data, $rules)->atScene('s1')->validate();
+
+ $this->assertTrue($v->passed());
+ $this->assertCount(3, $v->getUsedRules());
+
+ $v = RuleValidation::make($data, $rules)->atScene('s2')->validate();
+
+ $this->assertTrue($v->passed());
+ $this->assertCount(4, $v->getUsedRules());
+ }
+
public function testValidateFailed()
{
$rules = $this->someRules();
diff --git a/tests/ValidatorListTest.php b/tests/ValidatorListTest.php
index 7c26c98..6fdccb6 100644
--- a/tests/ValidatorListTest.php
+++ b/tests/ValidatorListTest.php
@@ -8,7 +8,6 @@
*/
class ValidatorListTest extends TestCase
{
-
public function testIsEmpty()
{
$this->assertFalse(ValidatorList::isEmpty(1));
@@ -68,6 +67,9 @@ public function testNumber()
$this->assertFalse(ValidatorList::number(0));
$this->assertTrue(ValidatorList::number(1));
+ $this->assertTrue(ValidatorList::number(10, 5, 12));
+ $this->assertTrue(ValidatorList::number(10, 12, 5));
+ $this->assertTrue(ValidatorList::number(10, null, 12));
}
public function testString()
@@ -205,6 +207,7 @@ public function testIsList()
$this->assertFalse(ValidatorList::isList([]));
$this->assertFalse(ValidatorList::isList(['a' => 'v']));
$this->assertFalse(ValidatorList::isList(['value', 'a' => 'v']));
+ $this->assertFalse(ValidatorList::isList([3 => 'abc']));
$this->assertTrue(ValidatorList::isList(['abc']));
$this->assertTrue(ValidatorList::isList(['abc', 565, null]));
@@ -215,8 +218,10 @@ public function testIntList()
$this->assertFalse(ValidatorList::intList('test'));
$this->assertFalse(ValidatorList::intList([]));
$this->assertFalse(ValidatorList::intList(['a', 'v']));
+ $this->assertFalse(ValidatorList::intList(['a', 456]));
$this->assertFalse(ValidatorList::intList(['a' => 'v']));
$this->assertFalse(ValidatorList::intList(['value', 'a' => 'v']));
+ $this->assertFalse(ValidatorList::intList([2 => '343', 45]));
$this->assertTrue(ValidatorList::intList(['343', 45]));
$this->assertTrue(ValidatorList::intList([565, 3234, -56]));
@@ -230,6 +235,7 @@ public function testNumList()
$this->assertFalse(ValidatorList::numList(['a' => 'v']));
$this->assertFalse(ValidatorList::numList(['value', 'a' => 'v']));
$this->assertFalse(ValidatorList::numList([565, 3234, -56]));
+ $this->assertFalse(ValidatorList::numList([2 => 56, 45]));
$this->assertTrue(ValidatorList::numList(['343', 45]));
$this->assertTrue(ValidatorList::numList([56, 45]));
@@ -240,10 +246,11 @@ public function testStrList()
$this->assertFalse(ValidatorList::strList('test'));
$this->assertFalse(ValidatorList::strList([]));
$this->assertFalse(ValidatorList::strList(['a' => 'v']));
+ $this->assertFalse(ValidatorList::strList(['value', 'a' => 'v']));
+ $this->assertFalse(ValidatorList::strList(['abc', 565]));
+ $this->assertFalse(ValidatorList::strList(['abc', 565, null]));
- $this->assertTrue(ValidatorList::strList(['value', 'a' => 'v']));
- $this->assertTrue(ValidatorList::strList(['abc']));
- $this->assertTrue(ValidatorList::strList(['abc', 565, null]));
+ $this->assertTrue(ValidatorList::strList(['abc', 'efg']));
}
public function testJson()
@@ -261,4 +268,46 @@ public function testJson()
$this->assertTrue(ValidatorList::json('[]'));
$this->assertTrue(ValidatorList::json('{"aa": 34}'));
}
+
+ public function testHasKey()
+ {
+ $this->assertFalse(ValidatorList::hasKey('hello, world', 'all'));
+ $this->assertFalse(ValidatorList::hasKey(['a' => 'v0', 'b' => 'v1', 'c' => 'v2'], 'd'));
+ $this->assertFalse(ValidatorList::hasKey(['a' => 'v0', 'b' => 'v1', 'c' => 'v2'], ['c', 'd']));
+
+ $this->assertTrue(ValidatorList::hasKey(['a' => 'v0', 'b' => 'v1', 'c' => 'v2'], 'b'));
+ $this->assertTrue(ValidatorList::hasKey(['a' => 'v0', 'b' => 'v1', 'c' => 'v2'], ['b', 'c']));
+ }
+
+ public function testContains()
+ {
+ $this->assertFalse(ValidatorList::contains('hello, world', 'all'));
+
+ $this->assertTrue(ValidatorList::contains('hello, world', 'llo'));
+ $this->assertTrue(ValidatorList::contains('hello, world', ['llo', 'wor']));
+ }
+
+ public function testStartWith()
+ {
+ $this->assertFalse(ValidatorList::startWith('hello, world', 'ell'));
+
+ $this->assertTrue(ValidatorList::startWith('hello, world', 'hell'));
+ $this->assertTrue(ValidatorList::startWith(['hello', 'world'], 'hello'));
+ }
+
+ public function testEndWith()
+ {
+ $this->assertFalse(ValidatorList::endWith('hello, world', 'ell'));
+
+ $this->assertTrue(ValidatorList::endWith('hello, world', 'world'));
+ $this->assertTrue(ValidatorList::endWith(['hello', 'world'], 'world'));
+ }
+
+ public function testDate()
+ {
+ $this->assertFalse(ValidatorList::date('hello'));
+
+ $this->assertTrue(ValidatorList::date(170526));
+ $this->assertTrue(ValidatorList::date('20170526'));
+ }
}