Add a factory function

This commit is contained in:
Gnieark 2017-05-01 22:05:03 +02:00
parent a8d9667d54
commit 954195b57f
4 changed files with 53 additions and 60 deletions

View File

@ -4,6 +4,56 @@ if (!defined('DC_RC_PATH')) { return; }
$core->url->register('rest','rest','^rest(?:/(.*))?$',array('rest','getResponse')); $core->url->register('rest','rest','^rest(?:/(.*))?$',array('rest','getResponse'));
class rest extends dcUrlHandlers class rest extends dcUrlHandlers
{ {
/**
* Create the "good" RestQuery instance
* Input: $httpMethod: String (POST/GET/PATCH etc...)
* $args Url arguments
* $user dcAuth object
* $body Body of the input query. String
* Output: object RestQuery
*/
private function restFactoryQuery($httpMethod,$args,$user,$body){
//définir la methode API (pas HTML) appelée
switch($httpMethod){
case "GET":
if($args == 'blogs'){
$queryObj = new RestQueryGetBlogs($user);
break;
}elseif($args == 'specs'){
$queryObj = new RestQueryGetSpecs($user);
break;
}
break;
case "POST":
break;
case "PUT":
break;
case "PATCH":
break;
case "DELETE":
break;
default:
$this->response_code = RestQuery::get_full_code_header(400);
$this->response_message = array(
"error" => "Unrecoknized method",
"code" => 400
);
return;
break;
}
return $queryObj;
}
public static function getResponse($args) public static function getResponse($args)
{ {
global $core; global $core;
@ -44,7 +94,7 @@ class rest extends dcUrlHandlers
} }
} }
$r = new RestQuery($_SERVER['REQUEST_METHOD'],$args,$user); $r = rest::restFactoryQuery($_SERVER['REQUEST_METHOD'],$args,$user,file_get_contents('php://input'));
header($r->response_code); header($r->response_code);
echo json_encode($r->response_message); echo json_encode($r->response_message);

View File

@ -1,10 +1,7 @@
<?php <?php
class RestQueryGetBlogs class RestQueryGetBlogs extends RestQuery
{ {
public $response_code;
public $response_message; //array
public function __construct($user){ public function __construct($user){
if($user === false){ if($user === false){
//need To be authentified //need To be authentified

View File

@ -1,10 +1,6 @@
<?php <?php
class RestQueryGetSpecs class RestQueryGetSpecs extends RestQuery
{ {
public $response_code;
public $response_message; //array
public function __construct($user){ public function __construct($user){
global $core; global $core;
$this->response_code = 200; $this->response_code = 200;

View File

@ -3,49 +3,6 @@
class RestQuery{ class RestQuery{
public $response_code; public $response_code;
public $response_message; //array public $response_message; //array
private $queryObj;
public function __construct($httpMethod,$args,$user){
error_log($httpMethod." ".$args);
//définir la methode API (pas HTML) appelée
switch($httpMethod){
case "GET":
if($args == 'blogs'){
$queryObj = new RestQueryGetBlogs($user);
break;
}elseif($args == 'specs'){
$queryObj = new RestQueryGetSpecs($user);
break;
}
break;
case "POST":
break;
case "PUT":
break;
case "PATCH":
break;
case "DELETE":
break;
default:
$this->response_code = RestQuery::get_full_code_header(400);
$this->response_message = array(
"error" => "Unrecoknized method",
"code" => 400
);
return;
break;
}
$this->response_code = $queryObj->response_code;
$this->response_message = $queryObj->response_message;
}
public function get_full_code_header($code){ public function get_full_code_header($code){
static $codes = array( static $codes = array(
@ -111,12 +68,5 @@ class RestQuery{
}else{ }else{
return "HTTP/1.0 ".$code." Something wrong happened"; return "HTTP/1.0 ".$code." Something wrong happened";
} }
} }
} }
//etc...