Skip to content

Commit 18a416f

Browse files
committed
up: update readme, action script. format codes
1 parent 0d1a8fe commit 18a416f

12 files changed

+85
-49
lines changed

.github/workflows/php.yml

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
name: Unit-tests
22

3-
on: [push]
3+
on:
4+
push:
5+
paths:
6+
- '**.php'
7+
- 'composer.json'
8+
- '**.yml'
49

510
jobs:
611
test:
@@ -10,8 +15,15 @@ jobs:
1015
strategy:
1116
fail-fast: true
1217
matrix:
13-
php: [7.1, 7.2, 7.3, 7.4] #
18+
php: [7.3, 7.4, 8.0] # 7.1, 7.2,
1419
os: [ubuntu-latest] # windows-latest, macOS-latest
20+
include:
21+
- os: 'ubuntu-latest'
22+
php: '7.2'
23+
phpunit: '8.5.13'
24+
- os: 'ubuntu-latest'
25+
php: '7.1'
26+
phpunit: '7.5.20'
1527

1628
steps:
1729
- name: Checkout
@@ -23,7 +35,7 @@ jobs:
2335
uses: shivammathur/setup-php@v2
2436
with:
2537
php-version: ${{ matrix.php}}
26-
tools: pecl, php-cs-fixer, phpunit
38+
tools: pecl, php-cs-fixer, phpunit:${{ matrix.phpunit }}
2739
extensions: mbstring, dom, fileinfo, mysql, openssl, igbinary, redis # , swoole-4.4.19 #optional, setup extensions
2840
ini-values: post_max_size=56M, short_open_tag=On #optional, setup php.ini configuration
2941
coverage: none #optional, setup coverage driver: xdebug, none
@@ -32,10 +44,11 @@ jobs:
3244
run: env
3345

3446
- name: Install dependencies
35-
run: composer install --no-progress --no-suggest
47+
run: composer install --no-progress
3648

3749
# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
3850
# Docs: https://getcomposer.org/doc/articles/scripts.md
3951

40-
- name: Run test suite
41-
run: composer run test
52+
- name: Run unit tests
53+
# run: composer run test
54+
run: phpunit -vv

README.md

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
- 支持自定义每个验证的错误消息,字段翻译,消息翻译,支持默认值
1818
- 支持基本的数组检查,数组的子级(`'goods.apple'`)值检查, 通配符的子级检查 (`'users.*.id' 'goods.*'`)
1919
- 方便的获取错误信息,验证后的安全数据获取(只会收集有规则检查过的数据)
20-
- 已经内置了60多个常用的验证器[内置验证器](#built-in-validators)
20+
- 已经内置了大量的常用的验证器[内置验证器](#built-in-validators)
2121
- 规则设置参考 `yii`, `laravel`, `Respect/Validation`
2222
- 独立的过滤器 `Inhere\Validate\Filter\Filtration`,可单独用于数据过滤
2323

@@ -62,7 +62,7 @@ validate 同时支持两种规则配置方式,对应了两种规则的收集
6262
6363
## 安装
6464

65-
```php
65+
```bash
6666
composer require inhere/php-validate
6767
// composer require inhere/php-validate ^2.2
6868
```
@@ -135,19 +135,19 @@ class PageRequest extends Validation
135135
// 4<= tagId <=567
136136
['tagId', 'size', 'min'=>4, 'max'=>567, 'filter' => 'int'],
137137

138-
           // title length >= 40. 注意只需一个参数的验证,无需加 key, 如这里的 40
139-
           ['title', 'min', 40, 'filter' => 'trim'],
138+
// title length >= 40. 注意只需一个参数的验证,无需加 key, 如这里的 40
139+
['title', 'min', 40, 'filter' => 'trim'],
140140

141-
           // 大于 0
142-
           ['freeTime', 'number'],
141+
// 大于 0
142+
['freeTime', 'number'],
143143

144144
// 含有前置条件
145145
['tagId', 'number', 'when' => function($data) {
146146
return isset($data['status']) && $data['status'] > 2;
147147
}],
148148

149149
// 在验证前会先过滤转换为 int。并且仅会在指明场景名为 'scene1' 时规则有效
150-
           ['userId', 'number', 'on' => 'scene1', 'filter' => 'int'],
150+
['userId', 'number', 'on' => 'scene1', 'filter' => 'int'],
151151
['username', 'string', 'on' => 'scene2', 'filter' => 'trim'],
152152

153153
// 使用自定义正则表达式
@@ -164,8 +164,8 @@ class PageRequest extends Validation
164164
return false;
165165
}],
166166

167-
           // 标记字段是安全可靠的 无需验证
168-
           ['createdAt, updatedAt', 'safe'],
167+
// 标记字段是安全可靠的 无需验证
168+
['createdAt, updatedAt', 'safe'],
169169
];
170170
}
171171

@@ -308,11 +308,20 @@ class UserController
308308

309309
## 添加自定义验证器
310310

311-
- **方式1**在继承了 `Inhere\Validate\Validation` 的子类添加验证方法. 请看上面的 [使用方式1](#how-to-use2)
311+
底层调用验证器是支持:
312+
313+
- 一个闭包
314+
- 一个函数名称
315+
- 一个当前验证类的方法名
316+
- 在继承了 `Inhere\Validate\Validation` 的子类添加验证方法. 请看上面的 [使用方式1](#how-to-use2)
317+
- 一个通过 `Validation->addValidator()` 添加的临时验证器
318+
- 一个通过 `Validator\UserValidators::set` 注册的全局验证器
319+
- 一个实现了 `Validator\ValidatorInterface` 的对象
320+
- 一个可调用的对象(有 `__invoke` 方法)
312321

313322
> 注意: 写在当前类里的验证器方法必须带有后缀 `Validator`, 以防止对内部的其他的方法造成干扰
314323
315-
- **方式2**通过 `Validation::addValidator()` 添加自定义验证器. e.g:
324+
### 示例
316325

317326
```php
318327
$v = Validation::make($_POST,[
@@ -329,7 +338,7 @@ $v = Validation::make($_POST,[
329338
->validate();
330339
```
331340

332-
- **方式3**直接写闭包进行验证 e.g:
341+
直接写闭包进行验证 e.g:
333342

334343
```php
335344
['status', function($status) { // 第一个参数是字段值。最后一个参数总是 $data
@@ -341,16 +350,14 @@ $v = Validation::make($_POST,[
341350
}]
342351
```
343352

344-
- **方式4**定义一个闭包验证类进行验证,这种方法能提高验证方法的复用性
353+
定义一个闭包验证类进行验证,这种方法能提高验证方法的复用性
345354

346355
> 别忘了继承 `\Inhere\Validate\Validator\AbstractValidator`,和实现必须方法`validate`
347356
348357
```php
349-
350358
class AdemoValidator extends \Inhere\Validate\Validator\AbstractValidator
351359
{
352360

353-
354361
public function validate($value, $data): bool
355362
{
356363
if ($value == 1) {
@@ -361,7 +368,8 @@ class AdemoValidator extends \Inhere\Validate\Validator\AbstractValidator
361368

362369
}
363370

364-
['status', new AdemoValidator()]
371+
// 使用
372+
['status', new AdemoValidator()],
365373
```
366374

367375
<a name="on-in-Validation"></a>

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
{
1616
"name": "inhere",
1717
"email": "in.798@qq.com",
18-
"homepage": "http://www.yzone.net/"
18+
"homepage": "https://github.com/inhere"
1919
}
2020
],
2121
"require": {
22-
"php": ">7.1.0"
22+
"php": ">7.1.0",
23+
"toolkit/stidlib": "~1.0"
2324
},
2425
"require-dev": {
25-
"phpunit/phpunit": "^7.5"
26+
"phpunit/phpunit": "^7.5 || ^8.5 || ^9.5"
2627
},
2728
"autoload": {
2829
"psr-4": {

src/Traits/ErrorMessageTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public function lastError(bool $onlyMsg = true)
239239
}
240240

241241
/**
242-
* @param bool|null $_stopOnError
242+
* @param bool|mixed|null $_stopOnError
243243
*
244244
* @return $this
245245
*/

src/Validation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public function rules()
3939
*/
4040

4141
/**
42-
* @param string $key
43-
* @param null $value
42+
* @param string $key
43+
* @param mixed|null $value
4444
*
4545
* @return mixed
4646
*/

src/ValidationInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ public function lastError(bool $onlyMsg = true);
8787
public function getMessages(): array;
8888

8989
/**
90+
* @param bool $asObject
91+
*
9092
* @return array|stdClass
9193
*/
92-
public function getSafeData();
94+
public function getSafeData(bool $asObject = false);
9395
}

src/ValidationTrait.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
use Inhere\Validate\Traits\ErrorMessageTrait;
1717
use Inhere\Validate\Traits\ScopedValidatorsTrait;
1818
use Inhere\Validate\Validator\UserValidators;
19+
use Inhere\Validate\Validator\ValidatorInterface;
1920
use InvalidArgumentException;
2021
use stdClass;
2122
use function array_keys;
2223
use function array_merge;
2324
use function array_shift;
2425
use function array_values;
26+
use function count;
2527
use function explode;
2628
use function function_exists;
2729
use function in_array;
@@ -397,7 +399,7 @@ protected function fieldValidate(string $field, $value, string $validator, array
397399
* @param mixed $value Field value
398400
* @param Closure|string|mixed $validator Validator
399401
* @param array $args Arguments for validate
400-
* @param string $defMsg
402+
* @param string|array $defMsg
401403
*
402404
* @return bool
403405
* @throws InvalidArgumentException
@@ -411,15 +413,21 @@ protected function valueValidate(string $field, $value, $validator, array $args,
411413
}
412414

413415
$rawArgs = $args;
414-
$args = array_values($args);
416+
$passed = false;
415417

416-
// if $validator is a closure OR a object has method '__invoke'
417-
if (is_object($validator) && method_exists($validator, '__invoke')) {
418-
$args[] = $this->data;
419-
$passed = $validator($value, ...$args);
418+
$args = array_values($args);
419+
if (is_object($validator)) {
420+
// if $validator is a object has method '__invoke'
421+
if (method_exists($validator, '__invoke')) {
422+
$args[] = $this->data;
423+
$passed = $validator($value, ...$args);
424+
} elseif ($validator instanceof ValidatorInterface) {
425+
$args[] = $this->data;
426+
$passed = $validator->validate($value, ...$args);
427+
}
420428
} elseif (is_string($validator)) {
421429
$realName = Validators::realName($validator);
422-
// is global user validator
430+
// is user validator
423431
if ($callback = $this->getValidator($validator)) {
424432
$args[] = $this->data;
425433
$passed = $callback($value, ...$args);
@@ -684,7 +692,7 @@ public function isValidated(): bool
684692
*/
685693
public function hasRule(): bool
686694
{
687-
return $this->getRules() ? true : false;
695+
return count($this->getRules()) > 0;
688696
}
689697

690698
/**
@@ -698,7 +706,7 @@ public function getRules(): array
698706
/**
699707
* @param array $rules
700708
*
701-
* @return $this|mixed
709+
* @return $this|static
702710
*/
703711
public function setRules(array $rules)
704712
{
@@ -894,7 +902,7 @@ public function setSafe(string $key, $value): void
894902
*
895903
* @return array|stdClass
896904
*/
897-
public function safeData($asObject = false)
905+
public function safeData(bool $asObject = false)
898906
{
899907
return $this->getSafeData($asObject);
900908
}
@@ -904,7 +912,7 @@ public function safeData($asObject = false)
904912
*
905913
* @return array|stdClass
906914
*/
907-
public function getSafeData($asObject = false)
915+
public function getSafeData(bool $asObject = false)
908916
{
909917
return $asObject ? (object)$this->_safeData : $this->_safeData;
910918
}
@@ -913,7 +921,7 @@ public function getSafeData($asObject = false)
913921
* @param array $safeData
914922
* @param bool $clearOld
915923
*/
916-
public function setSafeData(array $safeData, $clearOld = false): void
924+
public function setSafeData(array $safeData, bool $clearOld = false): void
917925
{
918926
if ($clearOld) {
919927
$this->_safeData = [];

src/Validator/AbstractValidator.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@
22

33
namespace Inhere\Validate\Validator;
44

5+
/**
6+
* Class AbstractValidator
7+
* @package Inhere\Validate\Validator
8+
*/
59
abstract class AbstractValidator implements ValidatorInterface
610
{
711
/**
812
* 魔术方法,在试图函数式使用对象是调用
913
*
10-
* @param type $value
11-
* @param type $data
14+
* @param mixed $value
15+
* @param array $data
1216
*
1317
* @return bool
1418
*/
15-
public function __invoke($value, $data): bool
19+
public function __invoke($value, array $data): bool
1620
{
17-
return (bool)$this->validate($value, $data);
21+
return $this->validate($value, $data);
1822
}
1923
}

src/Validator/UserValidators.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
final class UserValidators
1919
{
2020
/**
21-
* @var array user custom add's validators (global)
21+
* @var array user custom added validators (global)
2222
*/
2323
private static $validators = [];
2424

src/Validator/ValidatorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ interface ValidatorInterface
1515
*
1616
* @return bool
1717
*/
18-
public function validate($value, $data): bool;
18+
public function validate($value, array $data): bool;
1919
}

test/RuleValidationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ function () {
439439
/**
440440
* 测试自定义验证器
441441
*/
442-
public function testValidator()
442+
public function testValidator(): void
443443
{
444444
$rule = [
445445
[

test/Validator/AdemoValidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class AdemoValidatorTest extends AbstractValidator
1717
*
1818
* @return bool
1919
*/
20-
public function validate($value, $data): bool
20+
public function validate($value, array $data): bool
2121
{
2222
return $value === 1;
2323
}

0 commit comments

Comments
 (0)