Skip to content

Commit

Permalink
ParamFrom支持来自多来源
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Feb 1, 2023
1 parent 3c1b704 commit 4c15c77
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 99 deletions.
2 changes: 1 addition & 1 deletion src/AnnotationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
207 changes: 109 additions & 98 deletions src/Attributes/Param.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:{
Expand Down

0 comments on commit 4c15c77

Please sign in to comment.