request.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. // This object is the abstract class for a request.
  3. abstract class arkiwiRequest
  4. {
  5. const BODY_TYPE_JSON = 'json';
  6. const BODY_TYPE_DATA = 'data';
  7. protected $request = null;
  8. protected $code = 500;
  9. protected $mime = 'application/json';
  10. protected $bodyType = arkiwiRequest::BODY_TYPE_JSON;
  11. protected $body = null;
  12. public function __construct($request) {
  13. $this->request = $request;
  14. }
  15. //run is an abstract method:
  16. abstract public function run();
  17. // GETTER/SETTER -----------------------------------------------------------
  18. // getter/setter HTTP Code:
  19. protected function getCode() {
  20. return $this->code;
  21. }
  22. protected function setCode($code) {
  23. $this->code = (int)$code;
  24. }
  25. protected function getCodeString() {
  26. $strings = array(
  27. '200' => 'OK',
  28. '201' => 'Created',
  29. '301' => 'Moved Permanently',
  30. '400' => 'Bad Request',
  31. '403' => 'Forbidden',
  32. '404' => 'Not Found',
  33. '409' => 'Conflict');
  34. if(isset($strings[$this->code]))
  35. return $strings[$this->code];
  36. return 'Internal Server Error';
  37. }
  38. // getter/setter MimeType:
  39. protected function getMime() {
  40. return $this->mime;
  41. }
  42. protected function setMime($mime) {
  43. $this->mime = $mime;
  44. }
  45. // setter JSON body:
  46. protected function setBodyJson($obj) {
  47. $this->setMime('application/json');
  48. $this->bodyType = arkiwiRequest::BODY_TYPE_JSON;
  49. $this->body = $obj;
  50. }
  51. // setter DATA body:
  52. protected function setBodyData($data) {
  53. $this->bodyType = arkiwiRequest::BODY_TYPE_DATA;
  54. $this->body = $data;
  55. }
  56. // setter ERROR body:
  57. protected function setBodyError($code, $message) {
  58. $this->setCode($code);
  59. $this->bodyType = arkiwiRequest::BODY_TYPE_JSON;
  60. $this->setMime('application/json');
  61. $this->body = array('errorCode' => $code,
  62. 'errorTitle' => $this->getCodeString(),
  63. 'errorMessage' => $message);
  64. }
  65. // Some getter methods:
  66. public function getRequestMethod() {
  67. return $this->request['method'];
  68. }
  69. public function getRequestPath() {
  70. return $this->request['path'];
  71. }
  72. public function getRequestExtra() {
  73. return $this->request['extra'];
  74. }
  75. public function getParam($param) {
  76. if(isset($this->queryString[$param]))
  77. return $this->queryString[$param];
  78. return null;
  79. }
  80. // WRITE METHODS -----------------------------------------------------------
  81. // This method write the common headers:
  82. public function writeHeader() {
  83. header('HTTP/1.0 ' . $this->getCode() . ' ' . $this->getCodeString());
  84. header('Content-Type: ' . $this->getMime());
  85. }
  86. // This method write the body using types:
  87. public function writeBody() {
  88. switch($this->bodyType) {
  89. case arkiwiRequest::BODY_TYPE_JSON:
  90. if($this->body)
  91. echo json_encode($this->body);
  92. break;
  93. case arkiwiRequest::BODY_TYPE_DATA:
  94. echo $this->body;
  95. break;
  96. }
  97. }
  98. }