00001 <?php 00028 class Zend_Json_Server_Error 00029 { 00030 const ERROR_PARSE = -32768; 00031 const ERROR_INVALID_REQUEST = -32600; 00032 const ERROR_INVALID_METHOD = -32601; 00033 const ERROR_INVALID_PARAMS = -32602; 00034 const ERROR_INTERNAL = -32603; 00035 const ERROR_OTHER = -32000; 00036 00041 protected $_allowedCodes = array( 00042 self::ERROR_PARSE, 00043 self::ERROR_INVALID_REQUEST, 00044 self::ERROR_INVALID_METHOD, 00045 self::ERROR_INVALID_PARAMS, 00046 self::ERROR_INTERNAL, 00047 self::ERROR_OTHER, 00048 ); 00049 00054 protected $_code = -32000; 00055 00060 protected $_data; 00061 00066 protected $_message; 00067 00076 public function __construct($message = null, $code = -32000, $data = null) 00077 { 00078 $this->setMessage($message) 00079 ->setCode($code) 00080 ->setData($data); 00081 } 00082 00089 public function setCode($code) 00090 { 00091 if (!is_scalar($code)) { 00092 return $this; 00093 } 00094 00095 $code = (int) $code; 00096 if (in_array($code, $this->_allowedCodes)) { 00097 $this->_code = $code; 00098 } elseif (in_array($code, range(-32099, -32000))) { 00099 $this->_code = $code; 00100 } 00101 00102 return $this; 00103 } 00104 00110 public function getCode() 00111 { 00112 return $this->_code; 00113 } 00114 00121 public function setMessage($message) 00122 { 00123 if (!is_scalar($message)) { 00124 return $this; 00125 } 00126 00127 $this->_message = (string) $message; 00128 return $this; 00129 } 00130 00136 public function getMessage() 00137 { 00138 return $this->_message; 00139 } 00140 00147 public function setData($data) 00148 { 00149 $this->_data = $data; 00150 return $this; 00151 } 00152 00158 public function getData() 00159 { 00160 return $this->_data; 00161 } 00162 00168 public function toArray() 00169 { 00170 return array( 00171 'code' => $this->getCode(), 00172 'message' => $this->getMessage(), 00173 'data' => $this->getData(), 00174 ); 00175 } 00176 00182 public function toJson() 00183 { 00184 require_once 'Zend/Json.php'; 00185 return Zend_Json::encode($this->toArray()); 00186 } 00187 00193 public function __toString() 00194 { 00195 return $this->toJson(); 00196 } 00197 } 00198