Error.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Json
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * @category Zend
  22. * @package Zend_Json
  23. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  24. * @license http://framework.zend.com/license/new-bsd New BSD License
  25. */
  26. class Zend_Json_Server_Error
  27. {
  28. const ERROR_PARSE = -32768;
  29. const ERROR_INVALID_REQUEST = -32600;
  30. const ERROR_INVALID_METHOD = -32601;
  31. const ERROR_INVALID_PARAMS = -32602;
  32. const ERROR_INTERNAL = -32603;
  33. const ERROR_OTHER = -32000;
  34. /**
  35. * Allowed error codes
  36. * @var array
  37. */
  38. protected $_allowedCodes = array(
  39. self::ERROR_PARSE,
  40. self::ERROR_INVALID_REQUEST,
  41. self::ERROR_INVALID_METHOD,
  42. self::ERROR_INVALID_PARAMS,
  43. self::ERROR_INTERNAL,
  44. self::ERROR_OTHER,
  45. );
  46. /**
  47. * Current code
  48. * @var int
  49. */
  50. protected $_code = -32000;
  51. /**
  52. * Error data
  53. * @var mixed
  54. */
  55. protected $_data;
  56. /**
  57. * Error message
  58. * @var string
  59. */
  60. protected $_message;
  61. /**
  62. * Constructor
  63. *
  64. * @param string $message
  65. * @param int $code
  66. * @param mixed $data
  67. * @return void
  68. */
  69. public function __construct($message = null, $code = -32000, $data = null)
  70. {
  71. $this->setMessage($message)
  72. ->setCode($code)
  73. ->setData($data);
  74. }
  75. /**
  76. * Set error code
  77. *
  78. * @param int $code
  79. * @return Zend_Json_Server_Error
  80. */
  81. public function setCode($code)
  82. {
  83. if (!is_scalar($code)) {
  84. return $this;
  85. }
  86. $code = (int) $code;
  87. if (in_array($code, $this->_allowedCodes)) {
  88. $this->_code = $code;
  89. } elseif (in_array($code, range(-32099, -32000))) {
  90. $this->_code = $code;
  91. }
  92. return $this;
  93. }
  94. /**
  95. * Get error code
  96. *
  97. * @return int|null
  98. */
  99. public function getCode()
  100. {
  101. return $this->_code;
  102. }
  103. /**
  104. * Set error message
  105. *
  106. * @param string $message
  107. * @return Zend_Json_Server_Error
  108. */
  109. public function setMessage($message)
  110. {
  111. if (!is_scalar($message)) {
  112. return $this;
  113. }
  114. $this->_message = (string) $message;
  115. return $this;
  116. }
  117. /**
  118. * Get error message
  119. *
  120. * @return string
  121. */
  122. public function getMessage()
  123. {
  124. return $this->_message;
  125. }
  126. /**
  127. * Set error data
  128. *
  129. * @param mixed $data
  130. * @return Zend_Json_Server_Error
  131. */
  132. public function setData($data)
  133. {
  134. $this->_data = $data;
  135. return $this;
  136. }
  137. /**
  138. * Get error data
  139. *
  140. * @return mixed
  141. */
  142. public function getData()
  143. {
  144. return $this->_data;
  145. }
  146. /**
  147. * Cast error to array
  148. *
  149. * @return array
  150. */
  151. public function toArray()
  152. {
  153. return array(
  154. 'code' => $this->getCode(),
  155. 'message' => $this->getMessage(),
  156. 'data' => $this->getData(),
  157. );
  158. }
  159. /**
  160. * Cast error to JSON
  161. *
  162. * @return string
  163. */
  164. public function toJson()
  165. {
  166. require_once 'Zend/Json.php';
  167. return Zend_Json::encode($this->toArray());
  168. }
  169. /**
  170. * Cast to string (JSON)
  171. *
  172. * @return string
  173. */
  174. public function __toString()
  175. {
  176. return $this->toJson();
  177. }
  178. }