1 | <?php |
||
13 | class RequestHandler |
||
14 | { |
||
15 | /** |
||
16 | * @param ParamMeta[] $paramMates |
||
17 | */ |
||
18 | 4 | public function __construct(array $paramMates=[]){ |
|
21 | |||
22 | /** |
||
23 | * @param Application $app |
||
24 | * @param Request $request |
||
25 | * @param array $params |
||
26 | * @param array $reference |
||
27 | * @return void |
||
28 | */ |
||
29 | 1 | public function handle(Application $app, Request $request, array &$params, array &$reference){ |
|
|
|||
30 | |||
31 | 1 | $vld = new Validator(); |
|
32 | 1 | $req = ['request'=>$request]; |
|
33 | 1 | $requestArray = new ArrayAdaptor($req); |
|
34 | 1 | $inputs = []; |
|
35 | 1 | foreach ($this->paramMetas as $k=>$meta){ |
|
36 | if($meta->isPassedByReference){ |
||
37 | // param PassedByReference is used to output |
||
38 | continue; |
||
39 | } |
||
40 | $source = \JmesPath\search($meta->source, $requestArray); |
||
41 | if ($source !== null){ |
||
42 | $source = ArrayAdaptor::strip($source); |
||
43 | if($source instanceof ParameterBag){ |
||
44 | $source = $source->all(); |
||
45 | } |
||
46 | if($meta->container){ |
||
47 | $inputs[$meta->name] = $meta->container->make($source); |
||
48 | }else{ |
||
49 | $inputs[$meta->name] = $source; |
||
50 | } |
||
51 | if($meta->validation){ |
||
52 | $vld->rule($meta->validation, $meta->name); |
||
53 | } |
||
54 | }else{ |
||
55 | $meta->isOptional or \PhpBoot\abort(new BadRequestHttpException("param $source is required")); |
||
56 | $inputs[$meta->name] = $meta->default; |
||
57 | } |
||
58 | 1 | } |
|
59 | 1 | $vld = $vld->withData($inputs); |
|
60 | 1 | $vld->validate() or \PhpBoot\abort( |
|
61 | new \InvalidArgumentException( |
||
62 | json_encode( |
||
63 | $vld->errors(), |
||
64 | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE |
||
65 | ) |
||
66 | ) |
||
67 | ); |
||
68 | |||
69 | 1 | $pos = 0; |
|
70 | 1 | foreach ($this->paramMetas as $meta){ |
|
71 | if($meta->isPassedByReference){ |
||
72 | $params[$pos] = &$reference[$meta->name]; |
||
73 | }else{ |
||
74 | $params[$pos] = $inputs[$meta->name]; |
||
75 | } |
||
76 | $pos++; |
||
77 | |||
78 | 1 | } |
|
79 | 1 | } |
|
80 | |||
81 | public function getParamNames(){ |
||
84 | |||
85 | /** |
||
86 | * 获取参数列表 |
||
87 | * @return ParamMeta[] |
||
88 | */ |
||
89 | 5 | public function getParamMetas(){ |
|
92 | |||
93 | /** |
||
94 | * 获取指定参数信息 |
||
95 | * @param $name |
||
96 | * @return ParamMeta|null |
||
97 | */ |
||
98 | 3 | public function getParamMeta($name){ |
|
106 | |||
107 | /** |
||
108 | * @param \PhpBoot\Metas\ParamMeta[] $paramMetas |
||
109 | */ |
||
110 | 4 | public function setParamMetas($paramMetas) |
|
114 | /** |
||
115 | * @var ParamMeta[] |
||
116 | */ |
||
117 | private $paramMetas = []; |
||
118 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.