Skip to content

Commit

Permalink
Merge pull request #1 from anythink-wx/master
Browse files Browse the repository at this point in the history
add ParamException
  • Loading branch information
tmtbe authored May 29, 2019
2 parents 2287bd6 + 8e6362f commit 33d98db
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/Core/ParamException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* Created by PhpStorm.
* User: anythink-wx
* Date: 2019-05-29
* Time: 15:35
*/

namespace ESD\BaseServer;
use ESD\Core\Exception;

class ParamException extends Exception
{
}
39 changes: 35 additions & 4 deletions src/Core/Server/Beans/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace ESD\Core\Server\Beans;

use ESD\BaseServer\ParamException;
use ESD\Core\Exception;

/**
Expand Down Expand Up @@ -139,7 +140,7 @@ public function getGetRequire(string $key)
{
$result = $this->get[$key] ?? null;
if ($result == null) {
throw new Exception("缺少参数$key");
throw new ParamException("require params $key");
}
return $result;
}
Expand All @@ -161,6 +162,21 @@ public function post()
}


/**
* @param null $key
* @param null $default
* @return array|null
*/
public function query($key = null, $default = null)
{
$query = array_merge($this->get, $this->post);
if($key){
return $query[$key] ?? $default;
}
return $query;
}


/**
* @param string $key
* @param null $default
Expand All @@ -180,7 +196,7 @@ public function getPostRequire(string $key)
{
$result = $this->post[$key] ?? null;
if ($result == null) {
throw new Exception("缺少参数$key");
throw new ParamException("require param $key");
}
return $result;
}
Expand Down Expand Up @@ -216,7 +232,7 @@ public function getGetPostRequire(string $key)
{
$result = $this->get[$key] ?? $this->post[$key] ?? null;
if ($result == null) {
throw new Exception("缺少参数$key");
throw new ParamException("require param $key");
}
return $result;
}
Expand All @@ -230,7 +246,7 @@ public function getPostGetRequire(string $key)
{
$result = $this->post[$key] ?? $this->get[$key] ?? null;
if ($result == null) {
throw new Exception("缺少参数$key");
throw new ParamException("require param $key");
}
return $result;
}
Expand All @@ -243,4 +259,19 @@ public function getJsonBody()
{
return json_decode($this->getRawContent(), true);
}


/**
* 获取request xml
* @return array
* @throws ParamException
*/
public function getXmlBody()
{
if (!$xml = simplexml_load_string($this->getRawContent(), 'SimpleXMLElement', LIBXML_NOCDATA)) {
throw new ParamException("wrong XmlBody");
}
$xml = json_decode(json_encode($xml), true);
return (array)$xml;
}
}

0 comments on commit 33d98db

Please sign in to comment.