/framework/vendor/zend/Zend/Json/Server/Error.php

http://zoop.googlecode.com/ · PHP · 198 lines · 82 code · 19 blank · 97 comment · 3 complexity · 15ae6f90c4977e7d3c3cebe84f862c5d MD5 · raw file

  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Error.php 20096 2010-01-06 02:05:09Z bkarwin $
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_Json
  24. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license http://framework.zend.com/license/new-bsd New BSD License
  26. */
  27. class Zend_Json_Server_Error
  28. {
  29. const ERROR_PARSE = -32768;
  30. const ERROR_INVALID_REQUEST = -32600;
  31. const ERROR_INVALID_METHOD = -32601;
  32. const ERROR_INVALID_PARAMS = -32602;
  33. const ERROR_INTERNAL = -32603;
  34. const ERROR_OTHER = -32000;
  35. /**
  36. * Allowed error codes
  37. * @var array
  38. */
  39. protected $_allowedCodes = array(
  40. self::ERROR_PARSE,
  41. self::ERROR_INVALID_REQUEST,
  42. self::ERROR_INVALID_METHOD,
  43. self::ERROR_INVALID_PARAMS,
  44. self::ERROR_INTERNAL,
  45. self::ERROR_OTHER,
  46. );
  47. /**
  48. * Current code
  49. * @var int
  50. */
  51. protected $_code = -32000;
  52. /**
  53. * Error data
  54. * @var mixed
  55. */
  56. protected $_data;
  57. /**
  58. * Error message
  59. * @var string
  60. */
  61. protected $_message;
  62. /**
  63. * Constructor
  64. *
  65. * @param string $message
  66. * @param int $code
  67. * @param mixed $data
  68. * @return void
  69. */
  70. public function __construct($message = null, $code = -32000, $data = null)
  71. {
  72. $this->setMessage($message)
  73. ->setCode($code)
  74. ->setData($data);
  75. }
  76. /**
  77. * Set error code
  78. *
  79. * @param int $code
  80. * @return Zend_Json_Server_Error
  81. */
  82. public function setCode($code)
  83. {
  84. if (!is_scalar($code)) {
  85. return $this;
  86. }
  87. $code = (int) $code;
  88. if (in_array($code, $this->_allowedCodes)) {
  89. $this->_code = $code;
  90. } elseif (in_array($code, range(-32099, -32000))) {
  91. $this->_code = $code;
  92. }
  93. return $this;
  94. }
  95. /**
  96. * Get error code
  97. *
  98. * @return int|null
  99. */
  100. public function getCode()
  101. {
  102. return $this->_code;
  103. }
  104. /**
  105. * Set error message
  106. *
  107. * @param string $message
  108. * @return Zend_Json_Server_Error
  109. */
  110. public function setMessage($message)
  111. {
  112. if (!is_scalar($message)) {
  113. return $this;
  114. }
  115. $this->_message = (string) $message;
  116. return $this;
  117. }
  118. /**
  119. * Get error message
  120. *
  121. * @return string
  122. */
  123. public function getMessage()
  124. {
  125. return $this->_message;
  126. }
  127. /**
  128. * Set error data
  129. *
  130. * @param mixed $data
  131. * @return Zend_Json_Server_Error
  132. */
  133. public function setData($data)
  134. {
  135. $this->_data = $data;
  136. return $this;
  137. }
  138. /**
  139. * Get error data
  140. *
  141. * @return mixed
  142. */
  143. public function getData()
  144. {
  145. return $this->_data;
  146. }
  147. /**
  148. * Cast error to array
  149. *
  150. * @return array
  151. */
  152. public function toArray()
  153. {
  154. return array(
  155. 'code' => $this->getCode(),
  156. 'message' => $this->getMessage(),
  157. 'data' => $this->getData(),
  158. );
  159. }
  160. /**
  161. * Cast error to JSON
  162. *
  163. * @return string
  164. */
  165. public function toJson()
  166. {
  167. require_once 'Zend/Json.php';
  168. return Zend_Json::encode($this->toArray());
  169. }
  170. /**
  171. * Cast to string (JSON)
  172. *
  173. * @return string
  174. */
  175. public function __toString()
  176. {
  177. return $this->toJson();
  178. }
  179. }