Skip to content

Commit 7139c40

Browse files
committed
[Http][Response] Optimize formatter
1 parent 1d5be30 commit 7139c40

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

src/http/Response.php

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
* @since 0.2.0
1212
* @example
1313
* $response = new yidas\http\Response;
14-
* $response->format = yidas\http\Response::FORMAT_JSON;
15-
* $response->data = ['foo'=>'bar'];
16-
* // $response->setStatusCode(200);
14+
* $response->setFormat(yidas\http\Response::FORMAT_JSON);
15+
* $response->setData(['foo'=>'bar']);
16+
* $response->setStatusCode(201, 'Created');
1717
* $response->send();
1818
* @todo Formatters
1919
*/
@@ -35,7 +35,11 @@ class Response
3535
* @var array the formatters that are supported by default
3636
*/
3737
public $contentTypes = [
38-
self::FORMAT_JSON => 'application/json;',
38+
self::FORMAT_RAW => 'text/plain;',
39+
self::FORMAT_HTML => 'text/html;',
40+
self::FORMAT_JSON => 'application/json;', // RFC 4627
41+
self::FORMAT_JSONP => 'application/javascript;', // RFC 4329
42+
self::FORMAT_XML => 'application/xml;', // RFC 2376
3943
];
4044
/**
4145
* @var string the response format. This determines how to convert [[data]] into [[content]]
@@ -90,21 +94,14 @@ public function setFormat($format)
9094
/**
9195
* Set Response Data into CI_Output
9296
*
97+
* @todo Format data before send
9398
* @param mixed Response data
9499
* @return object self
95100
*/
96101
public function setData($data)
97102
{
98-
$formatFunc = $this->_format. "Format";
99-
// Use formatter if exists
100-
if (method_exists($this, $formatFunc)) {
101-
102-
$data = $this->{$formatFunc}($data);
103-
}
104-
elseif (is_array($data)) {
105-
// Prevent error if is array data for deafult
106-
$data = json_encode($data);
107-
}
103+
// Format data
104+
$data = $this->format($data, $this->_format);
108105
// CI Output
109106
$this->ci->output->set_output($data);
110107

@@ -178,12 +175,38 @@ public function send()
178175
exit;
179176
}
180177

178+
/**
179+
* Common format funciton by format types. {FORMAT}Format()
180+
*
181+
* @param array Pre-handle array data
182+
* @param string Format
183+
* @return string Formatted data by specified formatter
184+
*/
185+
public function format($data, $format)
186+
{
187+
// Case handing. ex. json => Json
188+
$format = ucfirst(strtolower($format));
189+
$formatFunc = "format" . $format;
190+
// Use formatter if exists
191+
if (method_exists($this, $formatFunc)) {
192+
193+
$data = $this->{$formatFunc}($data);
194+
}
195+
elseif (is_array($data)) {
196+
// Use JSON while the Formatter not found and the data is array
197+
$data = $this->formatJson($data);
198+
}
199+
200+
return $data;
201+
}
202+
181203
/**
182204
* Common format funciton by format types. {FORMAT}Format()
183205
*
184206
* @param array Pre-handle array data
207+
* @return string Formatted data
185208
*/
186-
public static function jsonFormat($data)
209+
public static function formatJson($data)
187210
{
188211
return json_encode($data);
189212
}

0 commit comments

Comments
 (0)