Skip to content

Commit 4b0285b

Browse files
committed
Upgrade to hyperf 3.0.0
1 parent f3b8f3c commit 4b0285b

6 files changed

Lines changed: 147 additions & 164 deletions

File tree

.gitignore

100755100644
File mode changed.

composer.json

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@
1010
"description": "hyperf-plus validate 支持request 和方法场景验证,兼容tp5写法 支持控制器注解验证、方法注解验证",
1111
"license": "Apache-2.0",
1212
"require": {
13-
"php": ">=7.2",
14-
"hyperf/di": "^2.0",
15-
"hyperf/http-message": "^2.0",
16-
"hyperf/utils": "^2.0"
17-
},
18-
"require-dev": {
19-
"friendsofphp/php-cs-fixer": "^2.14"
13+
"php": ">=8.0",
14+
"hyperf/di": "^3.0.0",
15+
"hyperf/http-message": "^3.0.0",
16+
"hyperf/utils": "^3.0.0"
2017
},
2118
"suggest": {
2219
"ext-json": "Required to use JSON."

src/Annotations/RequestValidation.php

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,58 @@
22

33
namespace HPlus\Validate\Annotations;
44

5+
use Attribute;
56
use Hyperf\Di\Annotation\AbstractAnnotation;
67

7-
/**
8-
* @Annotation
9-
* @Target("METHOD")
10-
*/
8+
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
119
class RequestValidation extends AbstractAnnotation
1210
{
1311
/**
14-
* 规则类
15-
* @var string
16-
*/
17-
public $rules = [];
18-
/**
19-
* 验证器
20-
* @var string
21-
*/
22-
public $validate = '';
23-
/**
24-
* 场景
25-
* @var string
26-
*/
27-
public $scene = '';
28-
/**
29-
* 场景
30-
* @var string
31-
*/
32-
public $value = '';
33-
/**
34-
* 是否过滤多余字段
35-
* @var bool
36-
*/
37-
public $filter = false;
38-
/**
39-
* 安全模式严格按照规则字段,如果多字段会抛出异常
40-
* @var bool
41-
*/
42-
public $security = false;
43-
/**
44-
* 是否批量验证
45-
* @var bool
46-
*/
47-
public $batch = false;
48-
/**
49-
* 验证数据类型,支持json|xml|form表单
50-
* @var string
51-
*/
52-
public $dateType = 'json';
53-
54-
public function __construct($value = null)
12+
* @param array $rules
13+
* @param string|null $validate
14+
* @param string|null $scene
15+
* @param bool $filter
16+
* @param bool $security
17+
* @param bool $batch
18+
* @param string $dateType
19+
*/
20+
public function __construct(
21+
/**
22+
* 规则类
23+
* @var string
24+
*/
25+
public array $rules = [],
26+
/**
27+
* 验证器
28+
* @var string
29+
*/
30+
public ?string $validate = null,
31+
/**
32+
* 场景
33+
* @var string
34+
*/
35+
public ?string $scene = null,
36+
/**
37+
* 是否过滤多余字段
38+
* @var bool
39+
*/
40+
public bool $filter = false,
41+
/**
42+
* 安全模式严格按照规则字段,如果多字段会抛出异常
43+
* @var bool
44+
*/
45+
public bool $security = false,
46+
/**
47+
* 是否批量验证
48+
* @var bool
49+
*/
50+
public bool $batch = false,
51+
/**
52+
* 验证数据类型,支持json|xml|form表单
53+
* @var string
54+
*/
55+
public string $dateType = 'json'
56+
)
5557
{
56-
parent::__construct($value);
57-
$this->bindMainProperty('scene', $value);
5858
}
5959
}

src/Annotations/Validation.php

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,69 @@
22

33
namespace HPlus\Validate\Annotations;
44

5+
use Attribute;
56
use Hyperf\Di\Annotation\AbstractAnnotation;
67

7-
/**
8-
* @Annotation
9-
* @Target("METHOD")
10-
*/
8+
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
119
class Validation extends AbstractAnnotation
1210
{
1311
/**
14-
* 自定义规则
15-
* @var string
12+
* @param array $rules
13+
* @param string|null $validate
14+
* @param string|null $value
15+
* @param string|null $scene
16+
* @param bool $filter
17+
* @param bool $security
18+
* @param bool $batch
19+
* @param string $field
1620
*/
17-
public $rules = [];
18-
/**
19-
* 验证器
20-
* @var string
21-
*/
22-
public $validate = '';
23-
/**
24-
* 场景
25-
* @var string
26-
*/
27-
public $value = '';
28-
/**
29-
* 场景
30-
* @var string
31-
*/
32-
public $scene = '';
33-
/**
34-
* 是否过滤多余字段
35-
* @var bool
36-
*/
37-
public $filter = false;
38-
/**
39-
* 安全模式严格按照规则字段,如果多字段会抛出异常
40-
* @var bool
41-
*/
42-
public $security = false;
21+
public function __construct(
4322

44-
/**
45-
* 是否批量验证
46-
* @var bool
47-
*/
48-
public $batch = false;
49-
/**
50-
* 验证哪个参数
51-
* @var string
52-
*/
53-
public $field = "data";
23+
/**
24+
* 自定义规则
25+
* @var string
26+
*/
27+
public array $rules = [],
28+
/**
29+
* 验证器
30+
* @var string
31+
*/
32+
public ?string $validate = null,
33+
/**
34+
* 场景
35+
* @var string
36+
*/
37+
public ?string $value = null,
38+
/**
39+
* 场景
40+
* @var string
41+
*/
42+
public ?string $scene = null,
43+
/**
44+
* 是否过滤多余字段
45+
* @var bool
46+
*/
47+
public bool $filter = false,
48+
/**
49+
* 安全模式严格按照规则字段,如果多字段会抛出异常
50+
* @var bool
51+
*/
52+
public bool $security = false,
53+
54+
/**
55+
* 是否批量验证
56+
* @var bool
57+
*/
58+
public bool $batch = false,
59+
/**
60+
* 验证哪个参数
61+
* @var string
62+
*/
63+
public string $field = "data"
5464

55-
public function __construct($value = null)
65+
66+
)
5667
{
57-
parent::__construct($value);
58-
$this->bindMainProperty('scene', $value);
68+
5969
}
6070
}

src/Aspect/ValidationAspect.php

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
2-
declare(strict_types=1);
32

3+
declare (strict_types=1);
44
namespace HPlus\Validate\Aspect;
55

66
use HPlus\Validate\Annotations\RequestValidation;
@@ -11,30 +11,26 @@
1111
use Hyperf\Di\Aop\AbstractAspect;
1212
use Hyperf\Di\Aop\ProceedingJoinPoint;
1313
use Hyperf\Di\Exception\Exception;
14-
use Hyperf\Utils\Context;
14+
use Hyperf\Context\Context;
1515
use Psr\Container\ContainerInterface;
1616
use Psr\Http\Message\ServerRequestInterface;
1717

18-
/**
19-
* @Aspect
20-
*/
18+
#[Aspect]
2119
class ValidationAspect extends AbstractAspect
2220
{
21+
2322
protected $container;
23+
2424
protected $request;
25-
2625
// 要切入的类,可以多个,亦可通过 :: 标识到具体的某个方法,通过 * 可以模糊匹配
27-
public $annotations = [
28-
Validation::class,
29-
RequestValidation::class
30-
];
31-
26+
// 要切入的类,可以多个,亦可通过 :: 标识到具体的某个方法,通过 * 可以模糊匹配
27+
public array $annotations = [Validation::class, RequestValidation::class];
28+
3229
public function __construct(ContainerInterface $container, ServerRequestInterface $Request)
3330
{
3431
$this->container = $container;
3532
$this->request = $this->container->get(ServerRequestInterface::class);
3633
}
37-
3834
/**
3935
* @param ProceedingJoinPoint $proceedingJoinPoint
4036
* @return mixed
@@ -62,7 +58,6 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
6258
}
6359
return $proceedingJoinPoint->process();
6460
}
65-
6661
/**
6762
* @param $validation
6863
* @param $verData
@@ -84,7 +79,7 @@ private function validationData($validation, $verData, $class, $proceedingJoinPo
8479
$rules = $validation->rules;
8580
} else {
8681
if (class_exists($class)) {
87-
$validate = new $class;
82+
$validate = new $class();
8883
} else {
8984
throw new ValidateException('class not exists:' . $class);
9085
}
@@ -103,16 +98,15 @@ private function validationData($validation, $verData, $class, $proceedingJoinPo
10398
throw new ValidateException('params ' . $key . ' invalid');
10499
}
105100
}
106-
};
101+
}
107102
if ($validation->filter) {
108103
$fields = $this->getFields($rules);
109-
$verData = array_filter($verData, function ($value, $key) use ($fields) {
104+
$verData = array_filter($verData, function ($value, $key) use($fields) {
110105
return in_array($key, $fields);
111106
}, ARRAY_FILTER_USE_BOTH);
112-
113107
switch ($isRequest) {
114108
case true:
115-
Context::override(ServerRequestInterface::class, function (ServerRequestInterface $request) use ($verData) {
109+
Context::override(ServerRequestInterface::class, function (ServerRequestInterface $request) use($verData) {
116110
return $request->withParsedBody($verData);
117111
});
118112
break;
@@ -122,7 +116,7 @@ private function validationData($validation, $verData, $class, $proceedingJoinPo
122116
}
123117
}
124118
}
125-
119+
126120
protected function getFields(array $rules)
127121
{
128122
$fields = [];
@@ -132,7 +126,7 @@ protected function getFields(array $rules)
132126
}
133127
if (strpos($field, '|')) {
134128
// 字段|描述 用于指定属性名称
135-
list($field,) = explode('|', $field);
129+
list($field, ) = explode('|', $field);
136130
}
137131
$fields[] = $field;
138132
}

0 commit comments

Comments
 (0)