Posted By

evinweissenberg on 08/18/12


Tagged

php json api responder


Versions (?)

JSON Responder


 / Published in: PHP
 

URL: http://www.evinw.com

This response in JSON format with an ability to indicate type,code, message and if there is data, with data.

  1. <?php
  2. /*
  3. |---------------------------
  4. | Author: Evin Weissenberg
  5. |---------------------------
  6. */
  7. class JSON_Responder {
  8.  
  9. private $code;
  10. private $type = 1; //1 Error, 2 Success, 3 Informative.
  11. private $message;
  12. private $data;
  13.  
  14. const MESSAGE_TYPE_ERROR = 'Error';
  15. const MESSAGE_TYPE_SUCCESS = 'Success';
  16. const MESSAGE_TYPE_INFO = 'Informative';
  17.  
  18. function setCode($code) {
  19.  
  20. $this->code = (string)$code;
  21. return $this;
  22.  
  23. }
  24.  
  25. function setMessage($message) {
  26.  
  27. $this->message = (string)$message;
  28. return $this;
  29.  
  30. }
  31.  
  32. function setData($date) {
  33.  
  34. $this->data = (array)$date;
  35. return $this;
  36. }
  37.  
  38. function setType($type) {
  39.  
  40. $this->type = (int)$type;
  41.  
  42. try {
  43.  
  44. if ($this->type == 1) {
  45.  
  46. $this->type = self::MESSAGE_TYPE_ERROR;
  47.  
  48. } elseif ($this->type == 2) {
  49.  
  50. $this->type = self::MESSAGE_TYPE_SUCCESS;
  51.  
  52. } elseif ($this->type == 3) {
  53.  
  54. $this->type = self::MESSAGE_TYPE_INFO;
  55.  
  56. } else {
  57.  
  58. throw new Exception('Type must be options 1,2 or 3');
  59.  
  60. }
  61.  
  62. } catch (Exception $e) {
  63.  
  64. echo $e->getMessage();
  65.  
  66. }
  67.  
  68. return $this;
  69. }
  70.  
  71. function __get($property) {
  72.  
  73. return $this->$property;
  74.  
  75. }
  76.  
  77. function respond() {
  78.  
  79. try {
  80.  
  81. if ($this->type == null) {
  82.  
  83. throw new Exception('Property type is null.');
  84.  
  85. } elseif ($this->code == null) {
  86.  
  87. throw new Exception('Property code is null.');
  88. }
  89. elseif ($this->message) {
  90.  
  91. throw new Exception('Property message is null.');
  92.  
  93. } else {
  94.  
  95. if ($this->data != NULL) {
  96.  
  97. // If there is data in the response this pattern will be used.
  98. $response = array(
  99. "Type" => $this->type,
  100. "Code" => $this->code,
  101. "Message" => $this->message,
  102. "Data" => $this->data);
  103.  
  104. $response_json = json_encode($response);
  105.  
  106. return $response_json;
  107.  
  108. } else {
  109.  
  110. // If there is no data in the response this pattern will be used.
  111. $response = array(
  112. "Type" => $this->type,
  113. "Code" => $this->code,
  114. "Message" => $this->message);
  115.  
  116. $response_json = json_encode($response);
  117.  
  118. return $response_json;
  119. }
  120. }
  121.  
  122. } catch (Exception $e) {
  123.  
  124. echo $e->getMessage();
  125. echo $e->getTrace();
  126.  
  127. }
  128. }
  129. }
  130. //Usage
  131. $j = new JSON_Responder();
  132. $j->setType(1)->setCode('304')->setMessage('Failed')->respond();

Report this snippet  

You need to login to post a comment.