allow filtering parameters
This commit is contained in:
@@ -10,6 +10,7 @@ use Bsr\Utils\Logger\Logger;
|
|||||||
abstract class WebService
|
abstract class WebService
|
||||||
{
|
{
|
||||||
private $func = null;
|
private $func = null;
|
||||||
|
|
||||||
private $status = 200;
|
private $status = 200;
|
||||||
|
|
||||||
private $version = null;
|
private $version = null;
|
||||||
@@ -33,11 +34,12 @@ abstract class WebService
|
|||||||
$data = array();
|
$data = array();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
$result = $this->call($sendSession);
|
$result = $this->call($sendSession);
|
||||||
$data["result"][$this->func] = $result;
|
$data["result"][$this->func] = $result;
|
||||||
|
|
||||||
// Logger::log(print_r($result, true));
|
|
||||||
} catch (WebException $e) {
|
} catch (WebException $e) {
|
||||||
|
|
||||||
$data["error"]["code"] = $e->getCode();
|
$data["error"]["code"] = $e->getCode();
|
||||||
$data["error"]["reason"] = $e->getMessage();
|
$data["error"]["reason"] = $e->getMessage();
|
||||||
$data["error"]["name"] = $e->getName();
|
$data["error"]["name"] = $e->getName();
|
||||||
@@ -47,12 +49,15 @@ abstract class WebService
|
|||||||
$this->status = 400;
|
$this->status = 400;
|
||||||
|
|
||||||
Logger::info($e->getName(), 'error');
|
Logger::info($e->getName(), 'error');
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
|
|
||||||
$data["failure"]["code"] = $e->getCode();
|
$data["failure"]["code"] = $e->getCode();
|
||||||
$data["failure"]["reason"] = $e->getMessage();
|
$data["failure"]["reason"] = $e->getMessage();
|
||||||
$this->status = 500;
|
$this->status = 500;
|
||||||
|
|
||||||
Logger::info($e->getMessage(), 'error');
|
Logger::info($e->getMessage(), 'error');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger::stop(array('status' => $this->status));
|
Logger::stop(array('status' => $this->status));
|
||||||
@@ -75,14 +80,17 @@ abstract class WebService
|
|||||||
}
|
}
|
||||||
|
|
||||||
$params = empty($_GET) ? $_POST : $_GET;
|
$params = empty($_GET) ? $_POST : $_GET;
|
||||||
|
|
||||||
if (empty($params)) {
|
if (empty($params)) {
|
||||||
throw new UsageException("NoArguments", "No arguments specified.", UsageException::NO_ARGS);
|
throw new UsageException("NoArguments", "No arguments specified.", UsageException::NO_ARGS);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!array_key_exists("func", $params)) {
|
if (!isset($params["func"])) {
|
||||||
throw new UsageException("MissingMethod", "No method specified.", UsageException::MISSING_METHOD);
|
throw new UsageException("MissingMethod", "No method specified.", UsageException::MISSING_METHOD);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$params = $this->filterParams($params);
|
||||||
|
|
||||||
$this->func = $params["func"];
|
$this->func = $params["func"];
|
||||||
unset($params['func']);
|
unset($params['func']);
|
||||||
|
|
||||||
@@ -109,4 +117,13 @@ abstract class WebService
|
|||||||
|
|
||||||
return call_user_func_array(array($this, $this->func), $params);
|
return call_user_func_array(array($this, $this->func), $params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allow subclasses to filter params by overriding this
|
||||||
|
* @param $params
|
||||||
|
*/
|
||||||
|
protected function filterParams($params)
|
||||||
|
{
|
||||||
|
return $params;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user