Skip to content

Commit 0ef6f2c

Browse files
committed
some bug fixed. add filter keywords for validate. add filter tool class.
1 parent f809ccc commit 0ef6f2c

14 files changed

+595
-147
lines changed

README.md

Lines changed: 93 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
# php-validate
1+
# php validate
22

33
一个简洁小巧且功能完善的php验证库。仅有几个文件,无依赖。
44

5-
功能:
6-
75
- 简单方便,支持添加自定义验证器
8-
- 规则设置参考自 yii 的。部分规则参考自 laravel
96
- 支持前置验证检查, 自定义如何判断非空
10-
- 支持将规则按场景进行分组设置
7+
- 支持将规则按场景进行分组设置。或者部分验证
8+
- 支持在进行验证前对值使用过滤器进行净化过滤[内置过滤器](#built-in-filters)
119
- 支持自定义每个验证的错误消息,字段翻译,消息翻译,支持默认值
1210
- 支持基本的数组检查,数组的子级值检查
1311
- 方便的获取错误信息,验证后的安全数据获取
14-
- 已经内置了30多个常用的验证器
12+
- 已经内置了30多个常用的验证器[内置验证器](#built-in-validators)
13+
- 规则设置参考自 yii 的。部分规则参考自 laravel
14+
- `RuleValidation` 规则配置类似于Yii: 每条规则中,允许多个字段,但只能有一个验证器。
15+
- e.g `['tagId,userId,name,email,freeTime', 'required', ...]`(下面的示例都是这种)
16+
- `FieldValidation` 规则配置类似于Laravel: 每条规则中,只能有一个字段,但允许多个验证器。
17+
- e.g `['field', 'required|string:5,10|...', ...]`
18+
19+
> 新增了独立的过滤器 `Inhere\Validate\Filter\Filtration`. 只用于数据过滤。
1520
1621
## 项目地址
1722

@@ -21,7 +26,7 @@
2126
**注意:**
2227

2328
- master 分支是要求 `php >= 7` 的(推荐使用)。
24-
- php5 分支是支持 php 5 的代码分支
29+
- php5 分支是支持 php 5 的代码分支(不再维护)
2530

2631
## 安装
2732

@@ -104,7 +109,7 @@ class PageRequest extends Validation
104109
return [
105110
'required' => '{attr} 是必填项。',
106111
// 可以直接针对字段的某个规则进行消息定义
107-
'title.required' => 'O, 标题是必填项。',
112+
'title.required' => 'O, 标题是必填项。are you known?',
108113
];
109114
}
110115
}
@@ -268,14 +273,34 @@ $v = Validation::make($_POST,[
268273
}]
269274
```
270275

271-
## 规则关键词说明
276+
## 规则关键词
277+
278+
除了可以添加字段的验证之外,还有一些特殊关键词可以设置使用,以适应各种需求。
272279

273280
### `default` -- 设置字段的默认值
274281

275282
给一个或多个字段设置一个默认值。
276283

284+
```php
285+
['page', 'number', 'default' => 1],
286+
['pageSize', 'number', 'default' => 15],
287+
```
288+
277289
> NOTICE: 默认值也会被验证器验证
278290
291+
### `msg` -- 设置错误提示消息
292+
293+
设置当前规则的错误提示消息, 设置了后就不会在使用默认的提示消息。
294+
295+
```php
296+
['title', 'customValidator', 'msg' => '{attr} error msg!' ], // 指定当前规则的消息
297+
// o, 可以是数组哦 :)
298+
['tagId,title,userId,freeTime', 'required', 'msg' => [
299+
'tagId' => 'message ...',
300+
'userId' => 'message 1 ...',
301+
]],
302+
```
303+
279304
### `on` -- 设置规则使用场景
280305

281306
> 如果需要让定义的规则在多个类似情形下重复使用,可以设置规则的使用场景。在验证时也表明要验证的场景
@@ -338,8 +363,7 @@ $v = Validation::make($_POST,[
338363
```
339364

340365
提交的数据中 没有 `name` 字段或者 `$data['name']` 等于空都不会进行 `string` 验证;
341-
只有当 `$data['name']` 有值且不为空时才会验证是否是string
342-
366+
只有当 `$data['name']` **有值且不为空** 时才会验证是否是 `string`
343367

344368
如果要想为空时也检查, 请将此字段同时加入 `required` 规则中.
345369

@@ -366,14 +390,38 @@ $v = Validation::make($_POST,[
366390
}]
367391
```
368392

369-
### `safe` -- 标记属性/字段是安全的
393+
### `filter` -- 使用过滤器
370394

371-
标记属性/字段是安全的,无需验证,直接加入到安全数据中。
395+
支持在进行验证前对值使用过滤器进行净化过滤[内置过滤器](#built-in-filters)
372396

373-
比如我们在写入数据库之前手动追加的字段: 创建时间,更新时间
397+
> 允许同时使用多个过滤器。字符串使用 `|` 分隔,或者配置为数组
374398
375399
```php
376-
['createdAt, updatedAt', 'safe']
400+
['tagId,userId,freeTime', 'number', 'filter' => 'int'],
401+
['field', 'validator', 'filter' => 'filter0|filter1...'],
402+
```
403+
404+
> 过滤器请参看 http://php.net/manual/zh/filter.filters.sanitize.php
405+
406+
### 一个完整的规则示例
407+
408+
一个完整的规则示例, 包含了所有可添加的项。
409+
410+
```php
411+
// a full rule:
412+
[
413+
// validate setting
414+
'field0,field1,...', 'validator', 'arg0', 'arg1', ...,
415+
416+
// some extended option settings
417+
'skipOnEmpty' => 'bool',
418+
'msg' => 'string|array',
419+
'default' => 'mixed',
420+
'on' => 'string|array'
421+
'isEmpty' => 'callback(string|closure)',
422+
'when' => 'callback(string|closure)',
423+
'filter' => 'callback(string|array|closure)'
424+
]
377425
```
378426

379427
## 一些关键方法使用说明
@@ -497,6 +545,23 @@ public function get(string $key, $default = null)
497545

498546
从验证时传入的数据中取出对应 key 的值
499547

548+
<a name="built-in-filters"></a>
549+
## 内置的过滤器
550+
551+
过滤器 | 说明 | 示例
552+
-------|-------------|------------
553+
`int/integer` | 过滤非法字符并转换为`int`类型 | `['userId', 'number', 'filter' => 'int'],`
554+
`float` | 过滤非法字符,保留`float`格式的数据 | `['price', 'float', 'filter' => 'float'],`
555+
`string` | 过滤非法字符并转换为`string`类型 | `['userId', 'number', 'filter' => 'string'],`
556+
`trim` | 去除首尾空白字符,支持数组。 | `['username', 'min', 4, 'filter' => 'string|trim'],`
557+
`abs` | 返回绝对值 | `['field', 'int', 'filter' => 'abs'],`
558+
`url` | URL 过滤,移除所有不符合 URL 的字符 | `['field', 'url', 'filter' => 'url'],`
559+
`email` | email 过滤,移除所有不符合 email 的字符 | `['field', 'email', 'filter' => 'email'],`
560+
`encoded` | 去除 URL 编码不需要的字符,与 `urlencode()` 函数很类似 | `['imgUrl', 'url', 'filter' => 'encoded'],`
561+
`specialChars` | 相当于使用 `htmlspecialchars()` 转义数据 | `['content', 'string', 'filter' => 'specialChars'],`
562+
`quotes` | 应用 `addslashes()` 转义数据 | `['content', 'string', 'filter' => 'quotes'],`
563+
564+
<a name="built-in-validators"></a>
500565
## 内置的验证器
501566

502567
> `/` 分隔的验证器,表明功能是一样的,只是有不同的别名
@@ -540,6 +605,17 @@ public function get(string $key, $default = null)
540605
`ipv4` | 验证是否是 IPv4 | `['ipAddr', 'ipv4']`
541606
`ipv6` | 验证是否是 IPv6 | `['ipAddr', 'ipv6']`
542607
`regexp` | 使用正则进行验证 | `['name', 'regexp', '/^\w+$/']`
608+
`safe` | 用于标记字段是安全的,无需验证 | `['createdAt, updatedAt', 'safe']`
609+
610+
### `safe` 验证器,标记属性/字段是安全的
611+
612+
特殊验证器 用于标记字段是安全的,无需验证,直接加入到安全数据中。
613+
614+
比如我们在写入数据库之前手动追加的字段: 创建时间,更新时间。
615+
616+
```php
617+
['createdAt, updatedAt', 'safe']
618+
```
543619

544620
### 一些补充说明
545621

@@ -568,11 +644,11 @@ public function get(string $key, $default = null)
568644
- `required*` 系列规则参考自 laravel
569645
- `size/range` `length` 可以只定义 min 最小值。 但是当定义了max 值时,必须同时定义最小值
570646

571-
## 其他
647+
## 代码示例
572648

573649
可运行示例请看 `examples`
574650

575-
## 测试
651+
## 单元测试
576652

577653
```sh
578654
./tests/test.sh

examples/filter.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-11-24
6+
* Time: 18:13
7+
*/

examples/index.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272

7373
echo "\n----------------------------\n use Validation\n----------------------------\n\n";
7474

75-
$valid = \Inhere\Validate\Validation::make($data, $rules)
75+
$v = \Inhere\Validate\Validation::make($data, $rules)
7676
->setTranslates([
7777
'goods.pear' => '梨子'
7878
])
@@ -81,5 +81,5 @@
8181
])
8282
->validate([], false);
8383

84-
print_r($valid->getErrors());
84+
print_r($v->getErrors());
8585

src/AbstractValidation.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ abstract class AbstractValidation implements ValidationInterface
4242
public function __construct(array $data = [], array $rules = [], array $translates = [], $scene = '', $startValidate = false)
4343
{
4444
$this->data = $data;
45-
4645
$this
4746
->setRules($rules)
4847
->setScene($scene)

src/Filter/FilterList.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,45 +29,28 @@ public static function trim($var)
2929
/**
3030
* 过滤器删除数字中所有非法的字符。
3131
* @note 该过滤器允许所有数字以及 . + -
32-
* @param mixed $int 要过滤的变量
32+
* @param mixed $var 要过滤的变量
3333
* @return mixed $string
3434
*/
35-
public static function integer($int)
35+
public static function integer($var)
3636
{
37-
return filter_var($int, FILTER_SANITIZE_NUMBER_INT);
37+
return (int)filter_var($var, FILTER_SANITIZE_NUMBER_INT);
3838
}
3939

40-
public static function int($int)
40+
public static function int($var)
4141
{
42-
return self::integer($int);
42+
return self::integer($var);
4343
}
4444

4545
/**
46-
* @param $var
46+
* @param mixed $var
4747
* @return number
4848
*/
4949
public static function abs($var)
5050
{
5151
return abs((int)$var);
5252
}
5353

54-
/**
55-
* 字符串长度过滤截取
56-
* @param string $string 字符串
57-
* @param integer $start 起始长度
58-
* @param int $end 结束位置
59-
* @return mixed
60-
*/
61-
public static function stringCute($string, $start = 0, $end = null)
62-
{
63-
if (!is_string($string)) {
64-
return '';
65-
}
66-
67-
// $length = Helper::strlen($string);
68-
return Helper::substr($string, $start, $end);
69-
}
70-
7154
/**
7255
* 过滤器删除浮点数中所有非法的字符。
7356
* @note 该过滤器默认允许所有数字以及 + -
@@ -187,6 +170,23 @@ public static function stripped($var, $flags = 0)
187170
return self::string($var, $flags);
188171
}
189172

173+
/**
174+
* 字符串长度过滤截取
175+
* @param string $string 字符串
176+
* @param integer $start 起始长度
177+
* @param int $end 结束位置
178+
* @return mixed
179+
*/
180+
public static function stringCute($string, $start = 0, $end = null)
181+
{
182+
if (!is_string($string)) {
183+
return '';
184+
}
185+
186+
// $length = Helper::strlen($string);
187+
return Helper::substr($string, $start, $end);
188+
}
189+
190190
/**
191191
* url地址过滤 移除所有不符合 url 的字符
192192
* @note 该过滤器允许所有的字母、数字以及 $-_.+!*'(),{}|\^~[]`"><#%;/?:@&=

0 commit comments

Comments
 (0)