1 | <?php |
||
27 | class JSendRenderer extends BaseRenderer |
||
28 | { |
||
29 | |||
30 | /** |
||
31 | * Success status. |
||
32 | */ |
||
33 | const STATUS_SUCCESS = 'success'; |
||
34 | |||
35 | /** |
||
36 | * Fail status. |
||
37 | */ |
||
38 | const STATUS_FAIL = 'fail'; |
||
39 | |||
40 | /** |
||
41 | * Error status. |
||
42 | */ |
||
43 | const STATUS_ERROR = 'error'; |
||
44 | |||
45 | /** |
||
46 | * Response status. |
||
47 | */ |
||
48 | public $status = self::STATUS_SUCCESS; |
||
49 | |||
50 | /** |
||
51 | * HTTP error code. |
||
52 | */ |
||
53 | public $errorCode = 200; |
||
54 | |||
55 | /** |
||
56 | * Confirms if the specified content type is acceptable for the response. |
||
57 | * |
||
58 | * @return bool |
||
59 | */ |
||
60 | public function accept() |
||
66 | |||
67 | /** |
||
68 | * Builds the HTTP response. |
||
69 | * |
||
70 | * @param Result $result The result object returned by the Service. |
||
71 | * @return bool |
||
72 | */ |
||
73 | 53 | public function response(Result $result = null) |
|
91 | |||
92 | /** |
||
93 | * Processes an exception thrown while processing the request. |
||
94 | * |
||
95 | * @param Exception $exception The exception object. |
||
96 | * @return void |
||
97 | */ |
||
98 | 9 | public function error(Exception $exception) |
|
112 | |||
113 | /** |
||
114 | * Formats a response to JSend specification. |
||
115 | * |
||
116 | * @param string $status The status of the response. |
||
117 | * @param array $response The response properties. |
||
118 | * @return string |
||
119 | */ |
||
120 | 58 | protected function _format($status, $response = []) |
|
131 | |||
132 | /** |
||
133 | * Creates a successful response. |
||
134 | * |
||
135 | * @param array $data The response data object. |
||
136 | * @return string |
||
137 | */ |
||
138 | protected function _success($data = null) |
||
142 | |||
143 | /** |
||
144 | * Creates a failure response. |
||
145 | * |
||
146 | * @param array $data The response data object. |
||
147 | * @return string |
||
148 | */ |
||
149 | protected function _fail($data = null) |
||
153 | |||
154 | /** |
||
155 | * Creates an error response. |
||
156 | * |
||
157 | * @param string $message The error message. |
||
158 | * @param int $code The error code. |
||
159 | * @param array $data The response data object. |
||
160 | * @param array $trace The exception trace |
||
161 | * @return string |
||
162 | */ |
||
163 | 9 | protected function _error($message = 'Unknown error', $code = 0, $data = null, $trace = null) |
|
176 | |||
177 | /** |
||
178 | * Update status based on result code |
||
179 | * |
||
180 | * @param Result $result |
||
181 | * @return void |
||
182 | */ |
||
183 | 53 | protected function _mapStatus(Result $result) |
|
192 | } |
||
193 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: