Skip to content

Commit d53f502

Browse files
committed
Remove and define json() method
1 parent 7139c40 commit d53f502

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

src/http/Response.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,13 @@ public static function formatJson($data)
216216
*
217217
* @param array|mixed Callback data body, false will remove body key
218218
* @param int Callback status code
219-
* @param string Callback status text
220219
* @return string Response body data
221220
*/
222-
public function json($data, $statusCode=null, $statusText=null)
221+
public function json($data, $statusCode=null)
223222
{
224223
// Set Status Code
225224
if ($statusCode) {
226-
$this->setStatusCode($statusCode, $statusText);
225+
$this->setStatusCode($statusCode);
227226
}
228227

229228
return $this->setFormat(Response::FORMAT_JSON)

src/rest/Controller.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Controller extends \CI_Controller
3131
*/
3232
protected $responseFormat = [
3333
'status_code' => 'code',
34-
'status_text' => 'message',
34+
'message' => 'message',
3535
'body' => 'data',
3636
];
3737

@@ -146,18 +146,21 @@ public function ajax($resourceID=NULL)
146146
*
147147
* @param array|mixed Callback data body, false will remove body key
148148
* @param bool Enable body format
149-
* @param int Callback status code
150-
* @param string Callback status text
149+
* @param int HTTP Status Code
150+
* @param string Callback message
151151
* @return string Response body data
152+
*
153+
* @example
154+
* json(false, true, 401, 'Login Required', 'Unauthorized');
152155
*/
153-
protected function json($data=[], $bodyFormat=null, $statusCode=null, $statusText=null)
156+
protected function json($data=[], $bodyFormat=null, $statusCode=null, $message=null)
154157
{
155158
// Check default Body Format setting if not assigning
156159
$bodyFormat = ($bodyFormat!==null) ? $bodyFormat : $this->bodyFormat;
157160

158161
if ($bodyFormat) {
159162
// Pack data
160-
$data = $this->_format($statusCode, $statusText, $data);
163+
$data = $this->_format($statusCode, $message, $data);
161164
} else {
162165
// JSON standard of RFC4627
163166
$data = is_array($data) ? $data : [$data];
@@ -174,19 +177,15 @@ protected function json($data=[], $bodyFormat=null, $statusCode=null, $statusTex
174177
* @param array|mixed|bool Callback data body, false will remove body key
175178
* @return array Formated array data
176179
*/
177-
protected function _format($statusCode=null, $statusText=null, $body=false)
180+
protected function _format($statusCode=null, $message=null, $body=false)
178181
{
179182
$format = [];
180-
// Status Code setting
181-
if ($statusCode) {
182-
$this->response->setStatusCode($statusCode);
183-
}
184183
// Status Code field is necessary
185184
$format[$this->responseFormat['status_code']] = ($statusCode)
186185
?: $this->response->getStatusCode();
187-
// Status Text field
188-
if ($statusText) {
189-
$format[$this->responseFormat['status_text']] = $statusText;
186+
// Message field
187+
if ($message) {
188+
$format[$this->responseFormat['message']] = $message;
190189
}
191190
// Body field
192191
if ($body !== false) {

0 commit comments

Comments
 (0)