Skip to content

Commit 9ab44a9

Browse files
committed
merge master update to current branch
1 parent eaa2f15 commit 9ab44a9

10 files changed

+284
-58
lines changed

README.md

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
- 方便的获取错误信息,验证后的安全数据获取
1717
- 已经内置了40多个常用的验证器[内置验证器](#built-in-validators)
1818
- 规则设置参考自 yii 的。部分规则参考自 laravel
19-
- `RuleValidation` 规则配置类似于Yii: 每条规则中,允许多个字段,但只能有一个验证器。
19+
- `Validation/RuleValidation` 规则配置类似于Yii: 每条规则中,允许多个字段,但只能有一个验证器。
2020
- e.g `['tagId,userId,name,email,freeTime', 'required', ...]`(下面的示例都是这种)
2121
- `FieldValidation` 规则配置类似于Laravel: 每条规则中,只能有一个字段,但允许多个验证器。
2222
- e.g `['field', 'required|string:5,10|...', ...]`
@@ -26,7 +26,7 @@
2626
## 项目地址
2727

2828
- **github** https://github.com/inhere/php-console.git
29-
- **git@osc** https://git.oschina.net/inhere/php-console.git
29+
- **git@osc** https://gitee.com/inhere/php-console.git
3030

3131
**注意:**
3232

@@ -39,6 +39,7 @@
3939

4040
```bash
4141
composer require inhere/php-validate
42+
// composer require inhere/php-validate ^1.2
4243
```
4344

4445
- 使用 composer.json
@@ -56,7 +57,7 @@ composer require inhere/php-validate
5657

5758
```
5859
git clone https://github.com/inhere/php-validate.git // github
59-
git clone https://git.oschina.net/inhere/php-validate.git // git@osc
60+
git clone https://gitee.com/inhere/php-validate.git // git@osc
6061
```
6162

6263
## 使用
@@ -69,7 +70,6 @@ git clone https://git.oschina.net/inhere/php-validate.git // git@osc
6970
> 此方式是最为完整的使用方式
7071
7172
```php
72-
7373
use Inhere\Validate\Validation;
7474

7575
class PageRequest extends Validation
@@ -88,7 +88,7 @@ class PageRequest extends Validation
8888
['username', 'string', 'on' => 'scene2' ],
8989
['username', 'regexp' ,'/^[a-z]\w{2,12}$/'],
9090
['title', 'customValidator', 'msg' => '{attr} error msg!' ], // 指定当前规则的消息
91-
['status', function($status) {
91+
['status', function($status) { // 直接使用闭包验证
9292
if (is_int($status) && $status > 3) {
9393
return true;
9494
}
@@ -114,7 +114,7 @@ class PageRequest extends Validation
114114
];
115115
}
116116

117-
// 自定义验证器的提示消息, 更多请看 {@see ValidationTrait::$messages}
117+
// 自定义验证器的提示消息, 默认消息请看 {@see ErrorMessageTrait::$messages}
118118
public function messages()
119119
{
120120
return [
@@ -175,7 +175,7 @@ class SomeController
175175
}
176176
```
177177

178-
### 方式 1: 创建一个新的class,使用 ValidationTrait
178+
### 方式 3: 创建一个新的class,使用 ValidationTrait
179179

180180
创建一个新的class,并使用 Trait `Inhere\Validate\ValidationTrait`。 此方式是高级自定义的使用方式, 可以方便的嵌入到其他类中
181181

@@ -565,8 +565,10 @@ public function get(string $key, $default = null)
565565
`float` | 过滤非法字符,保留`float`格式的数据 | `['price', 'float', 'filter' => 'float'],`
566566
`string` | 过滤非法字符并转换为`string`类型 | `['userId', 'number', 'filter' => 'string'],`
567567
`trim` | 去除首尾空白字符,支持数组。 | `['username', 'min', 4, 'filter' => 'trim'],`
568-
`lowercase` | 字符串转换为小写 | `['description', 'min', 4, 'filter' => 'lowercase'],`
569-
`uppercase` | 字符串转换为大写 | `['title', 'min', 4, 'filter' => 'uppercase'],`
568+
`lowercase` | 字符串转换为小写 | `['description', 'string', 'filter' => 'lowercase'],`
569+
`uppercase` | 字符串转换为大写 | `['title', 'string', 'filter' => 'uppercase'],`
570+
`snakeCase` | 字符串转换为蛇形风格 | `['title', 'string', 'filter' => 'snakeCase'],`
571+
`camelCase` | 字符串转换为驼峰风格 | `['title', 'string', 'filter' => 'camelCase'],`
570572
`timestamp/strToTime` | 字符串日期转换时间戳 | `['pulishedAt', 'number', 'filter' => 'strToTime'],`
571573
`abs` | 返回绝对值 | `['field', 'int', 'filter' => 'abs'],`
572574
`url` | URL 过滤,移除所有不符合 URL 的字符 | `['field', 'url', 'filter' => 'url'],`
@@ -596,6 +598,7 @@ public function get(string $key, $default = null)
596598
`isList` | 验证值是否是一个自然数组 list (key是从0自然增长的) | `['tags', 'isList']`
597599
`isArray` | 验证是否是数组 | `['goods', 'isArray']`
598600
`intList` | 验证字段值是否是一个 int list | `['tagIds', 'intList']`
601+
`numList` | 验证字段值是否是一个 number list | `['tagIds', 'numList']`
599602
`strList` | 验证字段值是否是一个 string list | `['tags', 'strList']`
600603
`min` | 最小边界值验证 | `['title', 'min', 40]`
601604
`max` | 最大边界值验证 | `['title', 'max', 40]`
@@ -630,7 +633,7 @@ public function get(string $key, $default = null)
630633
`md5` | 验证是否是 md5 格式的字符串 | `['passwd', 'md5']`
631634
`sha1` | 验证是否是 sha1 格式的字符串 | `['passwd', 'sha1']`
632635
`color` | 验证是否是html color | `['backgroundColor', 'color']`
633-
`regexp` | 使用正则进行验证 | `['name', 'regexp', '/^\w+$/']`
636+
`regex/regexp` | 使用正则进行验证 | `['name', 'regexp', '/^\w+$/']`
634637
`safe` | 用于标记字段是安全的,无需验证 | `['createdAt, updatedAt', 'safe']`
635638

636639
### `safe` 验证器,标记属性/字段是安全的
@@ -664,6 +667,7 @@ $v = Validation::make($_POST, [
664667
- 关于布尔值验证
665668
* 如果是 "1"、"true"、"on" 和 "yes",则返回 TRUE
666669
* 如果是 "0"、"false"、"off"、"no" 和 "",则返回 FALSE
670+
- `size/range` `length` 可以只定义 min 最小值。 但是 **当定义了max 值时,必须同时定义最小值**
667671
- 支持对数组的子级值验证
668672

669673
```php
@@ -681,9 +685,8 @@ $v = Validation::make($_POST, [
681685
['goods.pear', 'max', 30], //goods 下的 pear 值最大不能超过 30
682686
```
683687

684-
- 验证大小范围 `int` 是比较大小。 `string``array` 是检查长度
688+
- 验证大小范围 `int` 是比较大小。 `string``array` 是检查长度。大小范围 是包含边界值的
685689
- `required*` 系列规则参考自 laravel
686-
- `size/range` `length` 可以只定义 min 最小值。 但是当定义了max 值时,必须同时定义最小值
687690

688691
## 代码示例
689692

@@ -692,9 +695,23 @@ $v = Validation::make($_POST, [
692695
## 单元测试
693696

694697
```sh
695-
./tests/test.sh
698+
phpunit
696699
```
697700

698701
## License
699702

700703
MIT
704+
705+
## 我的其他项目
706+
707+
### `inhere/console` [github](https://github.com/inhere/php-console) [git@osc](https://git.oschina.net/inhere/php-console)
708+
709+
轻量且功能丰富的命令行应用,工具库, 控制台交互.
710+
711+
### `inhere/sroute` [github](https://github.com/inhere/php-srouter) [git@osc](https://git.oschina.net/inhere/php-srouter)
712+
713+
轻量且快速的路由库
714+
715+
### `inhere/http` [github](https://github.com/inhere/php-http) [git@osc](https://git.oschina.net/inhere/php-http)
716+
717+
http message 工具库(PSR 7 实现)

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "inhere/php-validate",
33
"type": "library",
4-
"description": "a simple validate library of the php",
4+
"description": "a simple validate, filter library of the php",
55
"keywords": ["library", "validate"],
66
"homepage": "http://github.com/inhere/php-validate",
77
"license": "MIT",

src/Filter/FilterList.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,36 @@ public static function uppercase($var)
136136
return Helper::strToUpper($var);
137137
}
138138

139+
/**
140+
* string to snakeCase
141+
* @param string $var
142+
* @param string $sep
143+
* @return string
144+
*/
145+
public static function snakeCase($var, $sep = '_')
146+
{
147+
if (!$var || !\is_string($var)) {
148+
return '';
149+
}
150+
151+
return Helper::toSnakeCase($var, $sep);
152+
}
153+
154+
/**
155+
* string to camelcase
156+
* @param string $var
157+
* @param bool $ucFirst
158+
* @return string
159+
*/
160+
public static function camelCase($var, $ucFirst = false)
161+
{
162+
if (!$var || !\is_string($var)) {
163+
return '';
164+
}
165+
166+
return Helper::toCamelCase($var, $ucFirst);
167+
}
168+
139169
/**
140170
* string to time
141171
* @param string $var
@@ -307,4 +337,4 @@ public static function callback($val, $callback)
307337
{
308338
return filter_var($val, FILTER_CALLBACK, ['options' => $callback]);
309339
}
310-
}
340+
}

src/Utils/ErrorMessageTrait.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ trait ErrorMessageTrait
4242
'notIn' => '{attr} cannot in ({value0})',
4343
'string' => ['{attr} must be a string',
4444
'{attr} must be a string and minimum length be {min}', '{attr} must be a string and length range must be {min} ~ {max}'],
45+
'regex' => '{attr} does not match the {value0} conditions',
4546
'regexp' => '{attr} does not match the {value0} conditions',
4647

4748
'mustBe' => '{attr} must be equals to {value0}',
@@ -50,7 +51,10 @@ trait ErrorMessageTrait
5051
'compare' => '{attr} must be equals to {value0}', 'same' => '{attr} must be equals to {value0}', 'equal' => '{attr} must be equals to {value0}',
5152
'notEqual' => '{attr} can not be equals to {value0}',
5253

53-
'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!'];
54+
'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',
55+
'intList' => '{attr} must be an array and value is all integers',
56+
'numList' => '{attr} must be an array and value is all numbers',
57+
'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!'];
5458
/**
5559
* attribute field translate list
5660
* @var array
@@ -320,7 +324,7 @@ public function getTranslate($attr)
320324
{
321325
$trans = $this->getTranslates();
322326

323-
return isset($trans[$attr]) ? $trans[$attr] : Helper::toSnakeCase($attr, ' ');
327+
return isset($trans[$attr]) ? $trans[$attr] : Helper::beautifyFieldName($attr, ' ');
324328
}
325329

326330
/**
@@ -335,4 +339,4 @@ public function setAttrTrans(array $attrTrans)
335339

336340
return $this;
337341
}
338-
}
342+
}

src/Utils/Helper.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,13 @@ public static function ucwords($str)
192192
* Translates a string with underscores into camel case (e.g. first_name -> firstName)
193193
* @prototype string public static function toCamelCase(string $str[, bool $capitalise_first_char = false])
194194
* @param $str
195-
* @param bool $upper_case_first_char
195+
* @param bool $upperCaseFirstChar
196196
* @return mixed
197197
*/
198-
public static function toCamelCase($str, $upper_case_first_char = false)
198+
public static function toCamelCase($str, $upperCaseFirstChar = false)
199199
{
200200
$str = self::strToLower($str);
201-
if ($upper_case_first_char) {
201+
if ($upperCaseFirstChar) {
202202
$str = self::ucfirst($str);
203203
}
204204

@@ -220,6 +220,17 @@ public static function toSnakeCase($string, $sep = '_')
220220
return self::strToLower(trim(preg_replace('/([A-Z][a-z])/', $sep . '$1', $string), $sep));
221221
}
222222

223+
/**
224+
* @param string $field
225+
* @return mixed|string
226+
*/
227+
public static function beautifyFieldName($field)
228+
{
229+
$str = self::toSnakeCase($field, ' ');
230+
231+
return strpos($str, '_') ? str_replace('_', ' ', $str) : $str;
232+
}
233+
223234
/**
224235
* getValueOfArray 支持以 '.' 分割进行子级值获取 eg: 'goods.apple'
225236
* @param array $array
@@ -247,7 +258,7 @@ public static function getValueOfArray(array $array, $key, $default = null)
247258
}
248259

249260
/**
250-
* @param $cb
261+
* @param callable $cb
251262
* @param array $args
252263
* @return mixed
253264
* @throws \InvalidArgumentException
@@ -271,4 +282,4 @@ public static function call($cb, ...$args)
271282
}
272283
throw new \InvalidArgumentException('The parameter is not a callable');
273284
}
274-
}
285+
}

src/ValidationTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* Trait ValidationTrait
1919
* @package Inhere\Validate
20-
* @property array $data To verify the data list. please define it on main class. 待验证的数据列表
20+
* property array $data To verify the data list. please define it on main class. 待验证的数据列表
2121
*/
2222
trait ValidationTrait
2323
{
@@ -167,7 +167,7 @@ public function validate(array $onlyChecked = null, $stopOnError = null)
167167
if ($when && $when instanceof \Closure && $when($data, $this) !== true) {
168168
continue;
169169
}
170-
// clear all options
170+
// clear all keywords options
171171
unset($rule['msg'], $rule['default'], $rule['skipOnEmpty'], $rule['isEmpty'], $rule['when'], $rule['filter']);
172172
// 验证设置, 有一些验证器需要参数。 e.g. size()
173173
$args = $rule;
@@ -547,4 +547,4 @@ public function getSafeFields()
547547
{
548548
return array_keys($this->_safeData);
549549
}
550-
}
550+
}

0 commit comments

Comments
 (0)