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) |
|
110 | |||
111 | /** |
||
112 | * Formats a response to JSend specification. |
||
113 | * |
||
114 | * @param string $status The status of the response. |
||
115 | * @param array $response The response properties. |
||
116 | * @return string |
||
117 | */ |
||
118 | 58 | protected function _format($status, $response = []) |
|
129 | |||
130 | /** |
||
131 | * Creates a successful response. |
||
132 | * |
||
133 | * @param array $data The response data object. |
||
134 | * @return string |
||
135 | */ |
||
136 | protected function _success($data = null) |
||
140 | |||
141 | /** |
||
142 | * Creates a failure response. |
||
143 | * |
||
144 | * @param array $data The response data object. |
||
145 | * @return string |
||
146 | */ |
||
147 | protected function _fail($data = null) |
||
151 | |||
152 | /** |
||
153 | * Creates an error response. |
||
154 | * |
||
155 | * @param string $message The error message. |
||
156 | * @param int $code The error code. |
||
157 | * @param array $data The response data object. |
||
158 | * @param array $trace The exception trace |
||
159 | * @return string |
||
160 | */ |
||
161 | 9 | protected function _error($message = 'Unknown error', $code = 0, $data = null, $trace = null) |
|
174 | |||
175 | /** |
||
176 | * Update status based on result code |
||
177 | * |
||
178 | * @param Result $result A result object instance. |
||
179 | * @return void |
||
180 | */ |
||
181 | 53 | protected function _mapStatus(Result $result) |
|
190 | } |
||
191 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: