File indexing completed on 2024-12-22 05:36:48
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Json 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 * @version $Id$ 0020 */ 0021 0022 /** 0023 * @category Zend 0024 * @package Zend_Json 0025 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0026 * @license http://framework.zend.com/license/new-bsd New BSD License 0027 */ 0028 class Zend_Json_Server_Error 0029 { 0030 const ERROR_PARSE = -32768; 0031 const ERROR_INVALID_REQUEST = -32600; 0032 const ERROR_INVALID_METHOD = -32601; 0033 const ERROR_INVALID_PARAMS = -32602; 0034 const ERROR_INTERNAL = -32603; 0035 const ERROR_OTHER = -32000; 0036 0037 /** 0038 * Allowed error codes 0039 * @var array 0040 */ 0041 protected $_allowedCodes = array( 0042 self::ERROR_PARSE, 0043 self::ERROR_INVALID_REQUEST, 0044 self::ERROR_INVALID_METHOD, 0045 self::ERROR_INVALID_PARAMS, 0046 self::ERROR_INTERNAL, 0047 self::ERROR_OTHER, 0048 ); 0049 0050 /** 0051 * Current code 0052 * @var int 0053 */ 0054 protected $_code = -32000; 0055 0056 /** 0057 * Error data 0058 * @var mixed 0059 */ 0060 protected $_data; 0061 0062 /** 0063 * Error message 0064 * @var string 0065 */ 0066 protected $_message; 0067 0068 /** 0069 * Constructor 0070 * 0071 * @param string $message 0072 * @param int $code 0073 * @param mixed $data 0074 * @return void 0075 */ 0076 public function __construct($message = null, $code = -32000, $data = null) 0077 { 0078 $this->setMessage($message) 0079 ->setCode($code) 0080 ->setData($data); 0081 } 0082 0083 /** 0084 * Set error code 0085 * 0086 * @param int $code 0087 * @return Zend_Json_Server_Error 0088 */ 0089 public function setCode($code) 0090 { 0091 if (!is_scalar($code)) { 0092 return $this; 0093 } 0094 0095 $code = (int) $code; 0096 if (in_array($code, $this->_allowedCodes)) { 0097 $this->_code = $code; 0098 } elseif (in_array($code, range(-32099, -32000))) { 0099 $this->_code = $code; 0100 } 0101 0102 return $this; 0103 } 0104 0105 /** 0106 * Get error code 0107 * 0108 * @return int|null 0109 */ 0110 public function getCode() 0111 { 0112 return $this->_code; 0113 } 0114 0115 /** 0116 * Set error message 0117 * 0118 * @param string $message 0119 * @return Zend_Json_Server_Error 0120 */ 0121 public function setMessage($message) 0122 { 0123 if (!is_scalar($message)) { 0124 return $this; 0125 } 0126 0127 $this->_message = (string) $message; 0128 return $this; 0129 } 0130 0131 /** 0132 * Get error message 0133 * 0134 * @return string 0135 */ 0136 public function getMessage() 0137 { 0138 return $this->_message; 0139 } 0140 0141 /** 0142 * Set error data 0143 * 0144 * @param mixed $data 0145 * @return Zend_Json_Server_Error 0146 */ 0147 public function setData($data) 0148 { 0149 $this->_data = $data; 0150 return $this; 0151 } 0152 0153 /** 0154 * Get error data 0155 * 0156 * @return mixed 0157 */ 0158 public function getData() 0159 { 0160 return $this->_data; 0161 } 0162 0163 /** 0164 * Cast error to array 0165 * 0166 * @return array 0167 */ 0168 public function toArray() 0169 { 0170 return array( 0171 'code' => $this->getCode(), 0172 'message' => $this->getMessage(), 0173 'data' => $this->getData(), 0174 ); 0175 } 0176 0177 /** 0178 * Cast error to JSON 0179 * 0180 * @return string 0181 */ 0182 public function toJson() 0183 { 0184 // require_once 'Zend/Json.php'; 0185 return Zend_Json::encode($this->toArray()); 0186 } 0187 0188 /** 0189 * Cast to string (JSON) 0190 * 0191 * @return string 0192 */ 0193 public function __toString() 0194 { 0195 return $this->toJson(); 0196 } 0197 } 0198