Skip to content

Commit

Permalink
update, add some new validators
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 26, 2017
1 parent b70c0f2 commit 2c26045
Show file tree
Hide file tree
Showing 8 changed files with 384 additions and 92 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
- 支持基本的数组检查,数组的子级值检查
- 方便的获取错误信息,验证后的安全数据获取
- 已经内置了40多个常用的验证器[内置验证器](#built-in-validators)
- 规则设置参考自 yii 的。部分规则参考自 laravel
- 规则设置参考 yii. 部分规则参考自 laravel, Respect/Validation
- 新增了独立的过滤器 `Inhere\Validate\Filter\Filtration` 用于数据过滤


支持两种规则配置方式:

- `Validation/RuleValidation` 规则配置类似于Yii: 每条规则中,允许多个字段,但只能有一个验证器。
Expand Down Expand Up @@ -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``<br/>` | `['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'],`
Expand All @@ -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']`
Expand All @@ -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']`
Expand All @@ -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]`
Expand All @@ -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']`
Expand Down
60 changes: 60 additions & 0 deletions examples/help.md
Original file line number Diff line number Diff line change
@@ -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)
{
}
```
41 changes: 41 additions & 0 deletions src/Filter/FilterList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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 <br/>
* @param string $str String to transform
* @return string New string
*/
public static function nl2br($str)
{
return str_replace(["\r\n", "\r", "\n"], '<br/>', $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}
Expand Down
12 changes: 11 additions & 1 deletion src/ValidationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ public function getScene(): string
*/
public function setScene(string $scene)
{
$this->scene = $scene;
$this->scene = trim($scene);

return $this;
}
Expand All @@ -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
Expand Down
Loading

0 comments on commit 2c26045

Please sign in to comment.