11<?php
2- declare (strict_types=1 );
32
3+ declare (strict_types=1 );
44namespace HPlus \Validate \Aspect ;
55
66use HPlus \Validate \Annotations \RequestValidation ;
1111use Hyperf \Di \Aop \AbstractAspect ;
1212use Hyperf \Di \Aop \ProceedingJoinPoint ;
1313use Hyperf \Di \Exception \Exception ;
14- use Hyperf \Utils \Context ;
14+ use Hyperf \Context \Context ;
1515use Psr \Container \ContainerInterface ;
1616use Psr \Http \Message \ServerRequestInterface ;
1717
18- /**
19- * @Aspect
20- */
18+ #[Aspect]
2119class 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