Skip to content

Commit 2c26045

Browse files
committed
update, add some new validators
1 parent b70c0f2 commit 2c26045

File tree

8 files changed

+384
-92
lines changed

8 files changed

+384
-92
lines changed

README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
- 支持基本的数组检查,数组的子级值检查
1616
- 方便的获取错误信息,验证后的安全数据获取
1717
- 已经内置了40多个常用的验证器[内置验证器](#built-in-validators)
18-
- 规则设置参考自 yii 的。部分规则参考自 laravel
18+
- 规则设置参考 yii. 部分规则参考自 laravel, Respect/Validation
1919
- 新增了独立的过滤器 `Inhere\Validate\Filter\Filtration` 用于数据过滤
2020

21-
2221
支持两种规则配置方式:
2322

2423
- `Validation/RuleValidation` 规则配置类似于Yii: 每条规则中,允许多个字段,但只能有一个验证器。
@@ -598,17 +597,19 @@ public function get(string $key, $default = null)
598597

599598
过滤器 | 说明 | 示例
600599
-------|-------------|------------
601-
`int/integer` | 过滤非法字符并转换为`int`类型 | `['userId', 'number', 'filter' => 'int'],`
600+
`abs` | 返回绝对值 | `['field', 'int', 'filter' => 'abs'],`
601+
`int/integer` | 过滤非法字符并转换为`int`类型 **支持数组** | `['userId', 'number', 'filter' => 'int'],`
602602
`float` | 过滤非法字符,保留`float`格式的数据 | `['price', 'float', 'filter' => 'float'],`
603603
`string` | 过滤非法字符并转换为`string`类型 | `['userId', 'number', 'filter' => 'string'],`
604604
`trim` | 去除首尾空白字符,支持数组。 | `['username', 'min', 4, 'filter' => 'trim'],`
605+
`nl2br` | 转换 `\n` `\r\n` `\r``<br/>` | `['content', 'string', 'filter' => 'nl2br'],`
605606
`lower/lowercase` | 字符串转换为小写 | `['description', 'string', 'filter' => 'lowercase'],`
606607
`upper/uppercase` | 字符串转换为大写 | `['title', 'string', 'filter' => 'uppercase'],`
607608
`snake/snakeCase` | 字符串转换为蛇形风格 | `['title', 'string', 'filter' => 'snakeCase'],`
608609
`camel/camelCase` | 字符串转换为驼峰风格 | `['title', 'string', 'filter' => 'camelCase'],`
609610
`timestamp/strToTime` | 字符串日期转换时间戳 | `['pulishedAt', 'number', 'filter' => 'strToTime'],`
610-
`abs` | 返回绝对值 | `['field', 'int', 'filter' => 'abs'],`
611611
`url` | URL 过滤,移除所有不符合 URL 的字符 | `['field', 'url', 'filter' => 'url'],`
612+
`str2list/str2array` | 字符串转数组 `'tag0,tag1' -> ['tag0', 'tag1']` | `['tags', 'strList', 'filter' => 'str2array'],`
612613
`email` | email 过滤,移除所有不符合 email 的字符 | `['field', 'email', 'filter' => 'email'],`
613614
`encoded` | 去除 URL 编码不需要的字符,与 `urlencode()` 函数很类似 | `['imgUrl', 'url', 'filter' => 'encoded'],`
614615
`clearTags/stripTags` | 相当于使用 `strip_tags()` | `['content', 'string', 'filter' => 'clearTags'],`
@@ -624,11 +625,11 @@ public function get(string $key, $default = null)
624625
625626
验证器 | 说明 | 规则示例
626627
----------|-------------|------------
627-
`int/integer` | 验证是否是 int 支持范围检查 | `['userId', 'int']` `['userId', 'int', 'min'=>4, 'max'=>16]`
628-
`num/number` | 验证是否是 number | `['userId', 'number']` `['userId', 'number', 'min'=>4, 'max'=>16]`
628+
`int/integer` | 验证是否是 int **支持范围检查** | `['userId', 'int']` `['userId', 'int', 'min'=>4, 'max'=>16]`
629+
`num/number` | 验证是否是 number **支持范围检查** | `['userId', 'number']` `['userId', 'number', 'min'=>4, 'max'=>16]`
629630
`bool/boolean` | 验证是否是 bool | `['open', 'bool']`
630631
`float` | 验证是否是 float | `['price', 'float']`
631-
`string` | 验证是否是 string. 支持长度检查 | `['name', 'string']`, `['name', 'string', 'min'=>4, 'max'=>16]`
632+
`string` | 验证是否是 string. **支持长度检查** | `['name', 'string']`, `['name', 'string', 'min'=>4, 'max'=>16]`
632633
`url` | 验证是否是 url | `['myUrl', 'url']`
633634
`email` | 验证是否是 email | `['userEmail', 'email']`
634635
`alpha` | 验证值是否仅包含字母字符 | `['name', 'alpha']`
@@ -637,6 +638,7 @@ public function get(string $key, $default = null)
637638
`isMap` | 验证值是否是一个非自然数组 map (key - value 形式的) | `['goods', 'isMap']`
638639
`isList` | 验证值是否是一个自然数组 list (key是从0自然增长的) | `['tags', 'isList']`
639640
`isArray` | 验证是否是数组 | `['goods', 'isArray']`
641+
`hasKey` | 验证数组存在给定的key(s) | `['goods', 'hasKey', 'pear']` `['goods', 'hasKey', ['pear', 'banana']]`
640642
`intList` | 验证字段值是否是一个 int list | `['tagIds', 'intList']`
641643
`numList` | 验证字段值是否是一个 number list | `['tagIds', 'numList']`
642644
`strList` | 验证字段值是否是一个 string list | `['tags', 'strList']`
@@ -645,6 +647,8 @@ public function get(string $key, $default = null)
645647
`size/range/between` | 验证大小范围, 可以支持验证 `int`, `string`, `array` 数据类型 | `['tagId', 'size', 'min'=>4, 'max'=>567]`
646648
`length` | 长度验证( 跟 `size`差不多, 但只能验证 `string`, `array` 的长度 | `['username', 'length', 'min' => 5, 'max' => 20]`
647649
`fixedSize/fixedLength` | 固定的长度/大小 | `['field', 'fixedSize', 12]`
650+
`startWith` | 值(`string/array`)是以给定的字符串开始 | `['field', 'startWith', 'hell']`
651+
`endWith` | 值(`string/array`)是以给定的字符串结尾 | `['field', 'endWith', 'world']`
648652
`in/enum` | 枚举验证 | `['status', 'in', [1,2,3]]`
649653
`notIn` | 枚举验证 | `['status', 'notIn', [4,5,6]]`
650654
`mustBe` | 必须是等于给定值 | `['status', 'mustBe', 1]`
@@ -671,6 +675,7 @@ public function get(string $key, $default = null)
671675
`ip` | 验证是否是 IP | `['ipAddr', 'ip']`
672676
`ipv4` | 验证是否是 IPv4 | `['ipAddr', 'ipv4']`
673677
`ipv6` | 验证是否是 IPv6 | `['ipAddr', 'ipv6']`
678+
`macAddress` | 验证是否是 mac Address | `['field', 'macAddress']`
674679
`md5` | 验证是否是 md5 格式的字符串 | `['passwd', 'md5']`
675680
`sha1` | 验证是否是 sha1 格式的字符串 | `['passwd', 'sha1']`
676681
`color` | 验证是否是html color | `['backgroundColor', 'color']`

examples/help.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# help
2+
3+
4+
```php
5+
6+
/**
7+
* @link http://php.net/manual/zh/function.filter-input.php
8+
* @param int $type INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV
9+
* @param $varName
10+
* @param array $filter 过滤/验证器 {@link http://php.net/manual/zh/filter.filters.php}
11+
* @param array $options 一个选项的关联数组,或者按位区分的标示。
12+
* 如果过滤器接受选项,可以通过数组的 "flags" 位去提供这些标示。
13+
* 如果成功的话返回所请求的变量。
14+
* 如果成功的话返回所请求的变量。
15+
* 如果过滤失败则返回 FALSE ,
16+
* 如果 varName 不存在的话则返回 NULL 。
17+
* 如果标示 FILTER_NULL_ON_FAILURE 被使用了,那么当变量不存在时返回 FALSE ,当过滤失败时返回 NULL 。
18+
*/
19+
public static function input($type, $varName, $filter, array $options = [])
20+
{
21+
}
22+
23+
public static function multi(array $data, array $filters = [])
24+
{
25+
}
26+
27+
/**
28+
* @link http://php.net/manual/zh/function.filter-input-array.php
29+
* 检查(验证/过滤)输入数据中的多个变量名 like filter_input_array()
30+
* 当需要获取很多变量却不想重复调用 filter_input()时很有用。
31+
* @param int $type One of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV. 要检查的输入数据
32+
* @param mixed $definition 一个定义参数的数组。
33+
* 一个有效的键必须是一个包含变量名的string,
34+
* 一个有效的值要么是一个filter type,或者是一个array 指明了过滤器、标示和选项。
35+
* 如果值是一个数组,那么它的有效的键可以是 :
36+
* filter, 用于指明 filter type,
37+
* flags 用于指明任何想要用于过滤器的标示,
38+
* options 用于指明任何想要用于过滤器的选项。
39+
* 参考下面的例子来更好的理解这段说明。
40+
* @param bool $addEmpty 在返回值中添加 NULL 作为不存在的键。
41+
* 如果成功的话返回一个所请求的变量的数组,
42+
* 如果失败的话返回 FALSE 。
43+
* 对于数组的值,
44+
* 如果过滤失败则返回 FALSE ,
45+
* 如果 variable_name 不存在的话则返回 NULL 。
46+
* 如果标示 FILTER_NULL_ON_FAILURE 被使用了,那么当变量不存在时返回 FALSE ,当过滤失败时返回 NULL 。
47+
*/
48+
public static function inputMulti($type, $definition, $addEmpty = true)
49+
{
50+
}
51+
52+
/**
53+
* 检查变量名是否存在
54+
* @param int $type One of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, or INPUT_ENV. 要检查的输入数据
55+
* @param string $varName Name of a variable to check. 要检查的变量名
56+
*/
57+
public static function inputHasVar($type, $varName)
58+
{
59+
}
60+
```

src/Filter/FilterList.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ final class FilterList
2222
*/
2323
public static function integer($val)
2424
{
25+
if (\is_array($val)) {
26+
return array_map(self::class . '::integer', $val);
27+
}
28+
2529
return (int)filter_var($val, FILTER_SANITIZE_NUMBER_INT);
2630
}
2731

@@ -95,6 +99,43 @@ public static function string($val, $flags = 0)
9599
return filter_var($val, FILTER_SANITIZE_FULL_SPECIAL_CHARS, $settings);
96100
}
97101

102+
/**
103+
* Convert \n and \r\n and \r to <br/>
104+
* @param string $str String to transform
105+
* @return string New string
106+
*/
107+
public static function nl2br($str)
108+
{
109+
return str_replace(["\r\n", "\r", "\n"], '<br/>', $str);
110+
}
111+
112+
/**
113+
* @param string $str
114+
* @param string $sep
115+
* @return array
116+
*/
117+
public static function str2list($str, $sep = ',')
118+
{
119+
return self::str2array($str, $sep);
120+
}
121+
122+
/**
123+
* var_dump(str2array('34,56,678, 678, 89, '));
124+
* @param string $str
125+
* @param string $sep
126+
* @return array
127+
*/
128+
public static function str2array($str, $sep = ',')
129+
{
130+
$str = trim($str, "$sep ");
131+
132+
if (!$str) {
133+
return [];
134+
}
135+
136+
return preg_split("/\s*$sep\s*/", $str, -1, PREG_SPLIT_NO_EMPTY);
137+
}
138+
98139
/**
99140
* @see FilterList::string()
100141
* {@inheritdoc}

src/ValidationTrait.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ public function getScene(): string
520520
*/
521521
public function setScene(string $scene)
522522
{
523-
$this->scene = $scene;
523+
$this->scene = trim($scene);
524524

525525
return $this;
526526
}
@@ -535,6 +535,16 @@ public function atScene(string $scene)
535535
return $this->setScene($scene);
536536
}
537537

538+
/**
539+
* alias of the `setScene()`
540+
* @param string $scene
541+
* @return static
542+
*/
543+
public function onScene(string $scene)
544+
{
545+
return $this->setScene($scene);
546+
}
547+
538548
/**
539549
* Get all items in collection
540550
* @return array The collection's source data

0 commit comments

Comments
 (0)