diff --git a/README.md b/README.md index 2874866..15e967a 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ - 方便的获取错误信息,验证后的安全数据获取 - 已经内置了40多个常用的验证器[内置验证器](#built-in-validators) - 规则设置参考自 yii 的。部分规则参考自 laravel -- `RuleValidation` 规则配置类似于Yii: 每条规则中,允许多个字段,但只能有一个验证器。 +- `Validation/RuleValidation` 规则配置类似于Yii: 每条规则中,允许多个字段,但只能有一个验证器。 - e.g `['tagId,userId,name,email,freeTime', 'required', ...]`(下面的示例都是这种) - `FieldValidation` 规则配置类似于Laravel: 每条规则中,只能有一个字段,但允许多个验证器。 - e.g `['field', 'required|string:5,10|...', ...]` @@ -26,7 +26,7 @@ ## 项目地址 - **github** https://github.com/inhere/php-console.git -- **git@osc** https://git.oschina.net/inhere/php-console.git +- **git@osc** https://gitee.com/inhere/php-console.git **注意:** @@ -39,6 +39,7 @@ ```bash composer require inhere/php-validate +// composer require inhere/php-validate ^1.2 ``` - 使用 composer.json @@ -56,7 +57,7 @@ composer require inhere/php-validate ``` git clone https://github.com/inhere/php-validate.git // github -git clone https://git.oschina.net/inhere/php-validate.git // git@osc +git clone https://gitee.com/inhere/php-validate.git // git@osc ``` ## 使用 @@ -69,7 +70,6 @@ git clone https://git.oschina.net/inhere/php-validate.git // git@osc > 此方式是最为完整的使用方式 ```php - use Inhere\Validate\Validation; class PageRequest extends Validation @@ -88,7 +88,7 @@ class PageRequest extends Validation ['username', 'string', 'on' => 'scene2' ], ['username', 'regexp' ,'/^[a-z]\w{2,12}$/'], ['title', 'customValidator', 'msg' => '{attr} error msg!' ], // 指定当前规则的消息 - ['status', function($status) { + ['status', function($status) { // 直接使用闭包验证 if (is_int($status) && $status > 3) { return true; } @@ -114,7 +114,7 @@ class PageRequest extends Validation ]; } - // 自定义验证器的提示消息, 更多请看 {@see ValidationTrait::$messages} + // 自定义验证器的提示消息, 默认消息请看 {@see ErrorMessageTrait::$messages} public function messages() { return [ @@ -175,7 +175,7 @@ class SomeController } ``` -### 方式 1: 创建一个新的class,使用 ValidationTrait +### 方式 3: 创建一个新的class,使用 ValidationTrait 创建一个新的class,并使用 Trait `Inhere\Validate\ValidationTrait`。 此方式是高级自定义的使用方式, 可以方便的嵌入到其他类中 @@ -565,8 +565,10 @@ public function get(string $key, $default = null) `float` | 过滤非法字符,保留`float`格式的数据 | `['price', 'float', 'filter' => 'float'],` `string` | 过滤非法字符并转换为`string`类型 | `['userId', 'number', 'filter' => 'string'],` `trim` | 去除首尾空白字符,支持数组。 | `['username', 'min', 4, 'filter' => 'trim'],` -`lowercase` | 字符串转换为小写 | `['description', 'min', 4, 'filter' => 'lowercase'],` -`uppercase` | 字符串转换为大写 | `['title', 'min', 4, 'filter' => 'uppercase'],` +`lowercase` | 字符串转换为小写 | `['description', 'string', 'filter' => 'lowercase'],` +`uppercase` | 字符串转换为大写 | `['title', 'string', 'filter' => 'uppercase'],` +`snakeCase` | 字符串转换为蛇形风格 | `['title', 'string', 'filter' => 'snakeCase'],` +`camelCase` | 字符串转换为驼峰风格 | `['title', 'string', 'filter' => 'camelCase'],` `timestamp/strToTime` | 字符串日期转换时间戳 | `['pulishedAt', 'number', 'filter' => 'strToTime'],` `abs` | 返回绝对值 | `['field', 'int', 'filter' => 'abs'],` `url` | URL 过滤,移除所有不符合 URL 的字符 | `['field', 'url', 'filter' => 'url'],` @@ -596,6 +598,7 @@ public function get(string $key, $default = null) `isList` | 验证值是否是一个自然数组 list (key是从0自然增长的) | `['tags', 'isList']` `isArray` | 验证是否是数组 | `['goods', 'isArray']` `intList` | 验证字段值是否是一个 int list | `['tagIds', 'intList']` +`numList` | 验证字段值是否是一个 number list | `['tagIds', 'numList']` `strList` | 验证字段值是否是一个 string list | `['tags', 'strList']` `min` | 最小边界值验证 | `['title', 'min', 40]` `max` | 最大边界值验证 | `['title', 'max', 40]` @@ -630,7 +633,7 @@ public function get(string $key, $default = null) `md5` | 验证是否是 md5 格式的字符串 | `['passwd', 'md5']` `sha1` | 验证是否是 sha1 格式的字符串 | `['passwd', 'sha1']` `color` | 验证是否是html color | `['backgroundColor', 'color']` -`regexp` | 使用正则进行验证 | `['name', 'regexp', '/^\w+$/']` +`regex/regexp` | 使用正则进行验证 | `['name', 'regexp', '/^\w+$/']` `safe` | 用于标记字段是安全的,无需验证 | `['createdAt, updatedAt', 'safe']` ### `safe` 验证器,标记属性/字段是安全的 @@ -664,6 +667,7 @@ $v = Validation::make($_POST, [ - 关于布尔值验证 * 如果是 "1"、"true"、"on" 和 "yes",则返回 TRUE * 如果是 "0"、"false"、"off"、"no" 和 "",则返回 FALSE +- `size/range` `length` 可以只定义 min 最小值。 但是 **当定义了max 值时,必须同时定义最小值** - 支持对数组的子级值验证 ```php @@ -681,9 +685,8 @@ $v = Validation::make($_POST, [ ['goods.pear', 'max', 30], //goods 下的 pear 值最大不能超过 30 ``` -- 验证大小范围 `int` 是比较大小。 `string` 和 `array` 是检查长度 +- 验证大小范围 `int` 是比较大小。 `string` 和 `array` 是检查长度。大小范围 是包含边界值的 - `required*` 系列规则参考自 laravel -- `size/range` `length` 可以只定义 min 最小值。 但是当定义了max 值时,必须同时定义最小值 ## 代码示例 @@ -692,9 +695,23 @@ $v = Validation::make($_POST, [ ## 单元测试 ```sh -./tests/test.sh +phpunit ``` ## License MIT + +## 我的其他项目 + +### `inhere/console` [github](https://github.com/inhere/php-console) [git@osc](https://git.oschina.net/inhere/php-console) + +轻量且功能丰富的命令行应用,工具库, 控制台交互. + +### `inhere/sroute` [github](https://github.com/inhere/php-srouter) [git@osc](https://git.oschina.net/inhere/php-srouter) + +轻量且快速的路由库 + +### `inhere/http` [github](https://github.com/inhere/php-http) [git@osc](https://git.oschina.net/inhere/php-http) + +http message 工具库(PSR 7 实现) diff --git a/composer.json b/composer.json index 5b8af3b..cb6c4d1 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "inhere/php-validate", "type": "library", - "description": "a simple validate library of the php", + "description": "a simple validate, filter library of the php", "keywords": ["library", "validate"], "homepage": "http://github.com/inhere/php-validate", "license": "MIT", diff --git a/src/Filter/FilterList.php b/src/Filter/FilterList.php index 8f2528f..2a2fda1 100644 --- a/src/Filter/FilterList.php +++ b/src/Filter/FilterList.php @@ -136,6 +136,36 @@ public static function uppercase($var) return Helper::strToUpper($var); } + /** + * string to snakeCase + * @param string $var + * @param string $sep + * @return string + */ + public static function snakeCase($var, $sep = '_') + { + if (!$var || !\is_string($var)) { + return ''; + } + + return Helper::toSnakeCase($var, $sep); + } + + /** + * string to camelcase + * @param string $var + * @param bool $ucFirst + * @return string + */ + public static function camelCase($var, $ucFirst = false) + { + if (!$var || !\is_string($var)) { + return ''; + } + + return Helper::toCamelCase($var, $ucFirst); + } + /** * string to time * @param string $var @@ -307,4 +337,4 @@ public static function callback($val, $callback) { return filter_var($val, FILTER_CALLBACK, ['options' => $callback]); } -} \ No newline at end of file +} diff --git a/src/Utils/ErrorMessageTrait.php b/src/Utils/ErrorMessageTrait.php index 3b4695a..a78964e 100644 --- a/src/Utils/ErrorMessageTrait.php +++ b/src/Utils/ErrorMessageTrait.php @@ -42,6 +42,7 @@ trait ErrorMessageTrait 'notIn' => '{attr} cannot in ({value0})', 'string' => ['{attr} must be a string', '{attr} must be a string and minimum length be {min}', '{attr} must be a string and length range must be {min} ~ {max}'], + 'regex' => '{attr} does not match the {value0} conditions', 'regexp' => '{attr} does not match the {value0} conditions', 'mustBe' => '{attr} must be equals to {value0}', @@ -50,7 +51,10 @@ trait ErrorMessageTrait 'compare' => '{attr} must be equals to {value0}', 'same' => '{attr} must be equals to {value0}', 'equal' => '{attr} must be equals to {value0}', 'notEqual' => '{attr} can not be equals to {value0}', - 'isArray' => '{attr} must be an array', 'isMap' => '{attr} must be an array and is key-value format', 'isList' => '{attr} must be an array of nature', 'intList' => '{attr} must be an array and value is all integers', 'strList' => '{attr} must be an array and value is all strings', 'json' => '{attr} must be an json string', 'file' => '{attr} must be an uploaded file', 'image' => '{attr} must be an uploaded image file', 'callback' => '{attr} don\'t pass the test and verify!', '_' => '{attr} validation is not through!']; + 'isArray' => '{attr} must be an array', 'isMap' => '{attr} must be an array and is key-value format', 'isList' => '{attr} must be an array of nature', + 'intList' => '{attr} must be an array and value is all integers', + 'numList' => '{attr} must be an array and value is all numbers', + 'strList' => '{attr} must be an array and value is all strings', 'json' => '{attr} must be an json string', 'file' => '{attr} must be an uploaded file', 'image' => '{attr} must be an uploaded image file', 'callback' => '{attr} don\'t pass the test and verify!', '_' => '{attr} validation is not through!']; /** * attribute field translate list * @var array @@ -320,7 +324,7 @@ public function getTranslate($attr) { $trans = $this->getTranslates(); - return isset($trans[$attr]) ? $trans[$attr] : Helper::toSnakeCase($attr, ' '); + return isset($trans[$attr]) ? $trans[$attr] : Helper::beautifyFieldName($attr, ' '); } /** @@ -335,4 +339,4 @@ public function setAttrTrans(array $attrTrans) return $this; } -} \ No newline at end of file +} diff --git a/src/Utils/Helper.php b/src/Utils/Helper.php index 8798d42..76c5c8d 100644 --- a/src/Utils/Helper.php +++ b/src/Utils/Helper.php @@ -192,13 +192,13 @@ public static function ucwords($str) * Translates a string with underscores into camel case (e.g. first_name -> firstName) * @prototype string public static function toCamelCase(string $str[, bool $capitalise_first_char = false]) * @param $str - * @param bool $upper_case_first_char + * @param bool $upperCaseFirstChar * @return mixed */ - public static function toCamelCase($str, $upper_case_first_char = false) + public static function toCamelCase($str, $upperCaseFirstChar = false) { $str = self::strToLower($str); - if ($upper_case_first_char) { + if ($upperCaseFirstChar) { $str = self::ucfirst($str); } @@ -220,6 +220,17 @@ public static function toSnakeCase($string, $sep = '_') return self::strToLower(trim(preg_replace('/([A-Z][a-z])/', $sep . '$1', $string), $sep)); } + /** + * @param string $field + * @return mixed|string + */ + public static function beautifyFieldName($field) + { + $str = self::toSnakeCase($field, ' '); + + return strpos($str, '_') ? str_replace('_', ' ', $str) : $str; + } + /** * getValueOfArray 支持以 '.' 分割进行子级值获取 eg: 'goods.apple' * @param array $array @@ -247,7 +258,7 @@ public static function getValueOfArray(array $array, $key, $default = null) } /** - * @param $cb + * @param callable $cb * @param array $args * @return mixed * @throws \InvalidArgumentException @@ -271,4 +282,4 @@ public static function call($cb, ...$args) } throw new \InvalidArgumentException('The parameter is not a callable'); } -} \ No newline at end of file +} diff --git a/src/ValidationTrait.php b/src/ValidationTrait.php index 8d9cf13..31abe3f 100644 --- a/src/ValidationTrait.php +++ b/src/ValidationTrait.php @@ -17,7 +17,7 @@ /** * Trait ValidationTrait * @package Inhere\Validate - * @property array $data To verify the data list. please define it on main class. 待验证的数据列表 + * property array $data To verify the data list. please define it on main class. 待验证的数据列表 */ trait ValidationTrait { @@ -167,7 +167,7 @@ public function validate(array $onlyChecked = null, $stopOnError = null) if ($when && $when instanceof \Closure && $when($data, $this) !== true) { continue; } - // clear all options + // clear all keywords options unset($rule['msg'], $rule['default'], $rule['skipOnEmpty'], $rule['isEmpty'], $rule['when'], $rule['filter']); // 验证设置, 有一些验证器需要参数。 e.g. size() $args = $rule; @@ -547,4 +547,4 @@ public function getSafeFields() { return array_keys($this->_safeData); } -} \ No newline at end of file +} diff --git a/src/ValidatorList.php b/src/ValidatorList.php index 10378e2..b235209 100644 --- a/src/ValidatorList.php +++ b/src/ValidatorList.php @@ -46,7 +46,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) { @@ -78,7 +78,7 @@ public static function bool($val, $default = null, $flags = 0) * 'decimal' => 2 * ] * @param int $flags FILTER_FLAG_ALLOW_THOUSAND - * @return mixed + * @return bool */ public static function float($val, array $options = [], $flags = 0) { @@ -212,20 +212,21 @@ public static function alphaDash($val) * @param int|string|array $val 待检测的值。 数字检查数字范围; 字符串、数组则检查长度 * @param null|integer $min 最小值 * @param null|int $max 最大值 - * @return mixed + * @return bool */ public static function size($val, $min = null, $max = null) { - $options = []; - if (\is_int($val)) { - $val = (int)$val; - } elseif (\is_string($val)) { - $val = Helper::strlen(trim($val)); - } elseif (\is_array($val)) { - $val = \count($val); - } else { - return false; + if (!\is_int($val)) { + if (\is_string($val)) { + $val = Helper::strlen(trim($val)); + } elseif (\is_array($val)) { + $val = \count($val); + } else { + return false; + } } + + $options = []; $minIsNum = is_numeric($min); $maxIsNum = is_numeric($max); if ($minIsNum && $maxIsNum) { @@ -278,7 +279,7 @@ public static function mustBe($val, $excepted) /** * 最小值检查 - * @param int $val + * @param int|string|array $val * @param integer $minRange * @return bool */ @@ -289,7 +290,7 @@ public static function min($val, $minRange) /** * 最大值检查 - * @param int $val + * @param int|string|array $val * @param int $maxRange * @return bool */ @@ -330,7 +331,7 @@ public static function regexp($val, $regexp, $default = null) $options['default'] = $default; } - return filter_var($val, FILTER_VALIDATE_REGEXP, ['options' => $options]); + return (bool)filter_var($val, FILTER_VALIDATE_REGEXP, ['options' => $options]); } public static function regex($val, $regexp, $default = null) @@ -347,7 +348,7 @@ public static function regex($val, $regexp, $default = null) * FILTER_FLAG_HOST_REQUIRED - 要求 URL 包含主机名(比如 http://www.example.com) * FILTER_FLAG_PATH_REQUIRED - 要求 URL 在域名后存在路径(比如 www.example.com/example1/test2/) * FILTER_FLAG_QUERY_REQUIRED - 要求 URL 存在查询字符串(比如 "example.php?name=Peter&age=37") - * @return mixed + * @return bool */ public static function url($val, $default = null, $flags = 0) { @@ -359,14 +360,14 @@ public static function url($val, $default = null, $flags = 0) $settings['flags'] = $flags; } - return filter_var($val, FILTER_VALIDATE_URL, $settings); + return (bool)filter_var($val, FILTER_VALIDATE_URL, $settings); } /** * email 地址验证 * @param string $val 要验证的数据 * @param mixed $default 设置验证失败时返回默认值 - * @return mixed + * @return bool */ public static function email($val, $default = null) { @@ -375,7 +376,7 @@ public static function email($val, $default = null) $options['default'] = $default; } - return filter_var($val, FILTER_VALIDATE_EMAIL, ['options' => $options]); + return (bool)filter_var($val, FILTER_VALIDATE_EMAIL, ['options' => $options]); } /** @@ -387,7 +388,7 @@ public static function email($val, $default = null) * FILTER_FLAG_IPV6 - 要求值是合法的 IPv6 IP(比如 2001:0db8:85a3:08d3:1319:8a2e:0370:7334) * FILTER_FLAG_NO_PRIV_RANGE - 要求值不在 RFC 指定的私有范围 IP 内(比如 192.168.0.1) * FILTER_FLAG_NO_RES_RANGE - 要求值不在保留的 IP 范围内。该标志接受 IPV4 和 IPV6 值 - * @return mixed + * @return bool */ public static function ip($val, $default = null, $flags = 0) { @@ -399,7 +400,7 @@ public static function ip($val, $default = null, $flags = 0) $settings['flags'] = $flags; } - return filter_var($val, FILTER_VALIDATE_IP, $settings); + return (bool)filter_var($val, FILTER_VALIDATE_IP, $settings); } /** @@ -436,7 +437,7 @@ public static function isArray($val) /** * 验证值是否是一个非自然数组 map (key - value 形式的) - * @param array $val + * @param mixed $val * @return bool */ public static function isMap($val) @@ -444,6 +445,8 @@ public static function isMap($val) if (!\is_array($val)) { return false; } + + /** @var array $val */ foreach ($val as $k => $v) { if (\is_string($k)) { return true; @@ -455,7 +458,7 @@ public static function isMap($val) /** * 验证值是否是一个自然数组 list (key是从0自然增长的) - * @param array $val + * @param array|mixed $val * @return bool */ public static function isList($val) @@ -463,14 +466,19 @@ public static function isList($val) if (!\is_array($val) || !isset($val[0])) { return false; } + $prevKey = 0; + + /** @var array $val */ foreach ($val as $k => $v) { if (!\is_int($k)) { return false; } + if ($k !== $prevKey) { return false; } + $prevKey++; } @@ -479,15 +487,21 @@ public static function isList($val) /** * 验证字段值是否是一个 int list - * @param array $val + * @param array|mixed $val * @return bool */ public static function intList($val) { - if (!\is_array($val)) { + if (!$val || !\is_array($val)) { return false; } - foreach ($val as $v) { + + /** @var array $val */ + foreach ($val as $k => $v) { + if (!\is_int($k)) { + return false; + } + if (!is_numeric($v)) { return false; } @@ -496,17 +510,48 @@ public static function intList($val) return true; } + /** + * 验证字段值是否是一个 number list + * @param array|mixed $val + * @return bool + */ + public static function numList($val) + { + if (!$val || !\is_array($val)) { + return false; + } + + /** @var array $val */ + foreach ($val as $k => $v) { + if (!\is_int($k)) { + return false; + } + + if (!is_numeric($v) || $v <= 0) { + return false; + } + } + + return true; + } + /** * 验证字段值是否是一个 string list - * @param array $val + * @param array|mixed $val * @return bool */ public static function strList($val) { - if (!\is_array($val)) { + if (!$val || !\is_array($val)) { return false; } - foreach ($val as $v) { + + /** @var array $val */ + foreach ($val as $k => $v) { + if (!\is_int($k)) { + return false; + } + if (\is_string($v)) { return true; } @@ -930,4 +975,4 @@ public static function inputMulti($type, $definition, $addEmpty = true) public static function inputHasVar($type, $varName) { } -} \ No newline at end of file +} diff --git a/tests/FieldValidationTest.php b/tests/FieldValidationTest.php index 302297a..27b10b6 100644 --- a/tests/FieldValidationTest.php +++ b/tests/FieldValidationTest.php @@ -47,7 +47,7 @@ public function testValidate() $errors = $v->getErrors(); $this->assertNotEmpty($errors); - $this->assertCount(4, $errors); + $this->assertCount(3, $errors); // var_dump($errors); } diff --git a/tests/RuleValidationTest.php b/tests/RuleValidationTest.php index 701a356..971b1ee 100644 --- a/tests/RuleValidationTest.php +++ b/tests/RuleValidationTest.php @@ -1,5 +1,6 @@ '1456767657', // filed not exists 'note' => '', 'status' => 2, - 'name' => 'john', + 'name' => '1234a2', 'existsField' => 'test', 'passwd' => 'password', 'repasswd' => 'repassword', @@ -75,6 +76,25 @@ public function testValidateFailed() $this->assertEquals($v->getSafe('tagId'), null); } + public function testValidateString() + { + $val = '123482'; + $v = Validation::make([ + 'user_name' => $val + ], [ + ['user_name', 'string', 'min' => 6], + // ['user_name', 'string', 'max' => 16], + ])->validate(); + + $this->assertTrue($v->passed()); + $this->assertFalse($v->failed()); + + $errors = $v->getErrors(); + $this->assertEmpty($errors); + $this->assertCount(0, $errors); + $this->assertEquals($v->getSafe('user_name'), $val); + } + protected function someRules() { return [ diff --git a/tests/ValidatorListTest.php b/tests/ValidatorListTest.php index f06cb5c..7681ab0 100644 --- a/tests/ValidatorListTest.php +++ b/tests/ValidatorListTest.php @@ -118,4 +118,103 @@ public function testLength() $this->assertTrue(ValidatorList::length('test', 3, 5)); $this->assertTrue(ValidatorList::length([3, 'test', 'hi'], 2, 5)); } + + public function testRegexp() + { + $this->assertFalse(ValidatorList::regexp('test', '/^\d+$/')); + $this->assertFalse(ValidatorList::regexp('test-dd', '/^\w+$/')); + + $this->assertTrue(ValidatorList::regexp('test56', '/^\w+$/')); + } + + public function testUrl() + { + $this->assertFalse(ValidatorList::url('test')); + $this->assertFalse(ValidatorList::url('/test56')); + + $this->assertTrue(ValidatorList::url('http://a.com/test56')); + } + + public function testEmail() + { + $this->assertFalse(ValidatorList::email('test')); + $this->assertFalse(ValidatorList::email('/test56')); + + $this->assertTrue(ValidatorList::email('abc@gmail.com')); + } + + public function testIp() + { + $this->assertFalse(ValidatorList::ip('test')); + $this->assertFalse(ValidatorList::ip('/test56')); + + $this->assertTrue(ValidatorList::ip('0.0.0.0')); + $this->assertTrue(ValidatorList::ip('127.0.0.1')); + } + + public function testIsArray() + { + $this->assertFalse(ValidatorList::isArray('test')); + $this->assertFalse(ValidatorList::isArray(345)); + + $this->assertTrue(ValidatorList::isArray([])); + $this->assertTrue(ValidatorList::isArray(['a'])); + } + + public function testIsMap() + { + $this->assertFalse(ValidatorList::isMap('test')); + $this->assertFalse(ValidatorList::isMap([])); + $this->assertFalse(ValidatorList::isMap(['abc'])); + + $this->assertTrue(ValidatorList::isMap(['a' => 'v'])); + $this->assertTrue(ValidatorList::isMap(['value', 'a' => 'v'])); + } + + public function testIsList() + { + $this->assertFalse(ValidatorList::isList('test')); + $this->assertFalse(ValidatorList::isList([])); + $this->assertFalse(ValidatorList::isList(['a' => 'v'])); + $this->assertFalse(ValidatorList::isList(['value', 'a' => 'v'])); + + $this->assertTrue(ValidatorList::isList(['abc'])); + $this->assertTrue(ValidatorList::isList(['abc', 565, null])); + } + + public function testIntList() + { + $this->assertFalse(ValidatorList::intList('test')); + $this->assertFalse(ValidatorList::intList([])); + $this->assertFalse(ValidatorList::intList(['a', 'v'])); + $this->assertFalse(ValidatorList::intList(['a' => 'v'])); + $this->assertFalse(ValidatorList::intList(['value', 'a' => 'v'])); + + $this->assertTrue(ValidatorList::intList(['343', 45])); + $this->assertTrue(ValidatorList::intList([565, 3234, -56])); + } + + public function testNumList() + { + $this->assertFalse(ValidatorList::numList('test')); + $this->assertFalse(ValidatorList::numList([])); + $this->assertFalse(ValidatorList::numList(['a', 'v'])); + $this->assertFalse(ValidatorList::numList(['a' => 'v'])); + $this->assertFalse(ValidatorList::numList(['value', 'a' => 'v'])); + $this->assertFalse(ValidatorList::numList([565, 3234, -56])); + + $this->assertTrue(ValidatorList::numList(['343', 45])); + $this->assertTrue(ValidatorList::numList([56, 45])); + } + + public function testStrList() + { + $this->assertFalse(ValidatorList::strList('test')); + $this->assertFalse(ValidatorList::strList([])); + $this->assertFalse(ValidatorList::strList(['a' => 'v'])); + + $this->assertTrue(ValidatorList::strList(['value', 'a' => 'v'])); + $this->assertTrue(ValidatorList::strList(['abc'])); + $this->assertTrue(ValidatorList::strList(['abc', 565, null])); + } }