From 4c15c77a9c5312e29d454f5f6bfbf14036d03b2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=82=E6=9E=9C=E7=9A=84=E5=A6=82=E6=9E=9C?= Date: Wed, 1 Feb 2023 15:44:11 +0800 Subject: [PATCH] =?UTF-8?q?ParamFrom=E6=94=AF=E6=8C=81=E6=9D=A5=E8=87=AA?= =?UTF-8?q?=E5=A4=9A=E6=9D=A5=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AnnotationController.php | 2 +- src/Attributes/Param.php | 207 ++++++++++++++++++----------------- 2 files changed, 110 insertions(+), 99 deletions(-) diff --git a/src/AnnotationController.php b/src/AnnotationController.php index 8eae9d4..7c6c1c7 100644 --- a/src/AnnotationController.php +++ b/src/AnnotationController.php @@ -94,7 +94,7 @@ private function runParamsValidate(string $method, Request $request):array $actionParams = $apiTag->requestParam; if($apiTag->allowMethod instanceof HttpMethod){ - $allowRequestMethod = [$apiTag->allowMethod->toString()]; + $allowRequestMethod = [$apiTag->allowMethod]; }else{ $allowRequestMethod = $apiTag->allowMethod; } diff --git a/src/Attributes/Param.php b/src/Attributes/Param.php index 077e0bd..709f2d4 100644 --- a/src/Attributes/Param.php +++ b/src/Attributes/Param.php @@ -26,7 +26,7 @@ class Param */ public function __construct( public string $name, - public ParamFrom $from = ParamFrom::GET, + public ParamFrom|array $from = [ParamFrom::GET,ParamFrom::POST], public ?array $validate = [], public $value = null, public bool $deprecated = false, @@ -67,130 +67,141 @@ public function parsedValue(?ServerRequestInterface $request = null) return $this->value; } if($request){ - switch ($this->from){ - case ParamFrom::GET:{ - $data = $request->getQueryParams(); - if(isset($data[$this->name])){ - $this->hasSet = true; - $this->value = $data[$this->name]; - } - break; - } - case ParamFrom::POST:{ - $data = $request->getParsedBody(); - if(isset($data[$this->name])){ - $this->hasSet = true; - $this->value = $data[$this->name]; - } - break; - } - case ParamFrom::JSON:{ - $data = json_decode($request->getBody()->__toString(),true); - if(!is_array($data)){ - $data = []; - } - if(empty($this->parentStack)){ + if(is_array($this->from)){ + $fromList = $this->from; + }else{ + $fromList = [$this->from]; + } + foreach ($fromList as $from){ + switch ($from){ + case ParamFrom::GET:{ + $data = $request->getQueryParams(); if(isset($data[$this->name])){ $this->hasSet = true; $this->value = $data[$this->name]; } - }else{ - foreach ($this->parentStack as $stack){ - if(isset($data[$stack])){ - $data = $data[$stack]; - } - } - if(is_array($data) && isset($data[$this->name])){ + break; + } + case ParamFrom::POST:{ + $data = $request->getParsedBody(); + if(isset($data[$this->name])){ $this->hasSet = true; $this->value = $data[$this->name]; } + break; } + case ParamFrom::JSON:{ + $data = json_decode($request->getBody()->__toString(),true); + if(!is_array($data)){ + $data = []; + } + if(empty($this->parentStack)){ + if(isset($data[$this->name])){ + $this->hasSet = true; + $this->value = $data[$this->name]; + } + }else{ + foreach ($this->parentStack as $stack){ + if(isset($data[$stack])){ + $data = $data[$stack]; + } + } + if(is_array($data) && isset($data[$this->name])){ + $this->hasSet = true; + $this->value = $data[$this->name]; + } + } - break; - } - case ParamFrom::XML:{ - $xml = $request->getBody()->__toString(); - // xml 转数组 - $data = json_decode(json_encode(simplexml_load_string($xml)), true); - if(!is_array($data)){ - $data = []; + break; } - if(empty($this->parentStack)){ - if(isset($data[$this->name])){ - $this->hasSet = true; - $this->value = $data[$this->name]; + case ParamFrom::XML:{ + $xml = $request->getBody()->__toString(); + // xml 转数组 + $data = json_decode(json_encode(simplexml_load_string($xml)), true); + if(!is_array($data)){ + $data = []; } - }else{ - foreach ($this->parentStack as $stack){ - if(isset($data[$stack])){ - $data = $data[$stack]; + if(empty($this->parentStack)){ + if(isset($data[$this->name])){ + $this->hasSet = true; + $this->value = $data[$this->name]; + } + }else{ + foreach ($this->parentStack as $stack){ + if(isset($data[$stack])){ + $data = $data[$stack]; + } + } + if(is_array($data) && isset($data[$this->name])){ + $this->hasSet = true; + $this->value = $data[$this->name]; } } - if(is_array($data) && isset($data[$this->name])){ + + break; + } + case ParamFrom::RAW_POST:{ + $this->hasSet = true; + $this->value = $request->getBody()->__toString(); + break; + } + case ParamFrom::FILE:{ + $data = $request->getUploadedFile($this->name); + if(!empty($data)){ $this->hasSet = true; - $this->value = $data[$this->name]; + $this->value = $data; } + break; } - - break; - } - case ParamFrom::RAW_POST:{ - $this->hasSet = true; - $this->value = $request->getBody()->__toString(); - break; - } - case ParamFrom::FILE:{ - $data = $request->getUploadedFile($this->name); - if(!empty($data)){ - $this->hasSet = true; - $this->value = $data; + case ParamFrom::DI:{ + $data = IOC::getInstance()->get($this->name); + if(!empty($data)){ + $this->hasSet = true; + $this->value = $data; + } + break; } - break; - } - case ParamFrom::DI:{ - $data = IOC::getInstance()->get($this->name); - if(!empty($data)){ - $this->hasSet = true; - $this->value = $data; + case ParamFrom::CONTEXT:{ + $data = ContextManager::getInstance()->get($this->name); + if(!empty($data)){ + $this->hasSet = true; + $this->value = $data; + } + break; } - break; - } - case ParamFrom::CONTEXT:{ - $data = ContextManager::getInstance()->get($this->name); - if(!empty($data)){ - $this->hasSet = true; - $this->value = $data; + case ParamFrom::COOKIE:{ + $data = $request->getCookieParams($this->name); + if(!empty($data)){ + $this->hasSet = true; + $this->value = $data; + } + break; } - break; - } - case ParamFrom::COOKIE:{ - $data = $request->getCookieParams($this->name); - if(!empty($data)){ + case ParamFrom::HEADER:{ + $data = $request->getHeader($this->name); $this->hasSet = true; - $this->value = $data; + if(!empty($data)){ + $this->value = $data[0]; + }else{ + $this->value = null; + } + break; } - break; - } - case ParamFrom::HEADER:{ - $data = $request->getHeader($this->name); - $this->hasSet = true; - if(!empty($data)){ - $this->value = $data[0]; - }else{ - $this->value = null; + case ParamFrom::ROUTER_PARAMS:{ + $data = ContextManager::getInstance()->get(AbstractRouter::PARSE_PARAMS_CONTEXT_KEY); + if(isset($data[$this->name])){ + $this->hasSet = true; + $this->value = $data; + } + break; } - break; } - case ParamFrom::ROUTER_PARAMS:{ - $data = ContextManager::getInstance()->get(AbstractRouter::PARSE_PARAMS_CONTEXT_KEY); - if(isset($data[$this->name])){ - $this->hasSet = true; - $this->value = $data; - } + if($this->hasSet){ break; } } + if($this->type != null){ switch ($this->type){ case ParamType::STRING:{