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