11
11
* @since 0.2.0
12
12
* @example
13
13
* $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' );
17
17
* $response->send();
18
18
* @todo Formatters
19
19
*/
@@ -35,7 +35,11 @@ class Response
35
35
* @var array the formatters that are supported by default
36
36
*/
37
37
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
39
43
];
40
44
/**
41
45
* @var string the response format. This determines how to convert [[data]] into [[content]]
@@ -90,21 +94,14 @@ public function setFormat($format)
90
94
/**
91
95
* Set Response Data into CI_Output
92
96
*
97
+ * @todo Format data before send
93
98
* @param mixed Response data
94
99
* @return object self
95
100
*/
96
101
public function setData ($ data )
97
102
{
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 );
108
105
// CI Output
109
106
$ this ->ci ->output ->set_output ($ data );
110
107
@@ -178,12 +175,38 @@ public function send()
178
175
exit ;
179
176
}
180
177
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
+
181
203
/**
182
204
* Common format funciton by format types. {FORMAT}Format()
183
205
*
184
206
* @param array Pre-handle array data
207
+ * @return string Formatted data
185
208
*/
186
- public static function jsonFormat ($ data )
209
+ public static function formatJson ($ data )
187
210
{
188
211
return json_encode ($ data );
189
212
}
0 commit comments