126 rivejä
2,9 KiB
PHP
126 rivejä
2,9 KiB
PHP
<?php
|
|
|
|
// This object is the abstract class for a request.
|
|
abstract class arkiwiRequest
|
|
{
|
|
const BODY_TYPE_JSON = 'json';
|
|
const BODY_TYPE_DATA = 'data';
|
|
|
|
protected $request = null;
|
|
|
|
protected $code = 500;
|
|
protected $mime = 'application/json';
|
|
|
|
protected $bodyType = arkiwiRequest::BODY_TYPE_JSON;
|
|
protected $body = null;
|
|
|
|
public function __construct($request) {
|
|
$this->request = $request;
|
|
}
|
|
|
|
//run is an abstract method:
|
|
abstract public function run();
|
|
|
|
// GETTER/SETTER -----------------------------------------------------------
|
|
|
|
// getter/setter HTTP Code:
|
|
protected function getCode() {
|
|
return $this->code;
|
|
}
|
|
|
|
protected function setCode($code) {
|
|
$this->code = (int)$code;
|
|
}
|
|
|
|
protected function getCodeString() {
|
|
$strings = array(
|
|
'200' => 'OK',
|
|
'201' => 'Created',
|
|
'301' => 'Moved Permanently',
|
|
'400' => 'Bad Request',
|
|
'403' => 'Forbidden',
|
|
'404' => 'Not Found',
|
|
'409' => 'Conflict');
|
|
|
|
if(isset($strings[$this->code]))
|
|
return $strings[$this->code];
|
|
|
|
return 'Internal Server Error';
|
|
}
|
|
|
|
// getter/setter MimeType:
|
|
protected function getMime() {
|
|
return $this->mime;
|
|
}
|
|
|
|
protected function setMime($mime) {
|
|
$this->mime = $mime;
|
|
}
|
|
|
|
// setter JSON body:
|
|
protected function setBodyJson($obj) {
|
|
$this->setMime('application/json');
|
|
$this->bodyType = arkiwiRequest::BODY_TYPE_JSON;
|
|
$this->body = $obj;
|
|
}
|
|
|
|
// setter DATA body:
|
|
protected function setBodyData($data) {
|
|
$this->bodyType = arkiwiRequest::BODY_TYPE_DATA;
|
|
$this->body = $data;
|
|
}
|
|
|
|
// setter ERROR body:
|
|
protected function setBodyError($code, $message) {
|
|
$this->setCode($code);
|
|
|
|
$this->bodyType = arkiwiRequest::BODY_TYPE_JSON;
|
|
$this->setMime('application/json');
|
|
|
|
$this->body = array('errorCode' => $code,
|
|
'errorTitle' => $this->getCodeString(),
|
|
'errorMessage' => $message);
|
|
}
|
|
|
|
// Some getter methods:
|
|
public function getRequestMethod() {
|
|
return $this->request['method'];
|
|
}
|
|
|
|
public function getRequestPath() {
|
|
return $this->request['path'];
|
|
}
|
|
|
|
public function getRequestExtra() {
|
|
return $this->request['extra'];
|
|
}
|
|
|
|
public function getParam($param) {
|
|
if(isset($this->queryString[$param]))
|
|
return $this->queryString[$param];
|
|
|
|
return null;
|
|
}
|
|
|
|
// WRITE METHODS -----------------------------------------------------------
|
|
|
|
// This method write the common headers:
|
|
public function writeHeader() {
|
|
header('HTTP/1.0 ' . $this->getCode() . ' ' . $this->getCodeString());
|
|
header('Content-Type: ' . $this->getMime());
|
|
}
|
|
|
|
// This method write the body using types:
|
|
public function writeBody() {
|
|
switch($this->bodyType) {
|
|
case arkiwiRequest::BODY_TYPE_JSON:
|
|
if($this->body)
|
|
echo json_encode($this->body);
|
|
break;
|
|
|
|
case arkiwiRequest::BODY_TYPE_DATA:
|
|
echo $this->body;
|
|
break;
|
|
}
|
|
}
|
|
}
|