99 */
1010
1111namespace nguyenanhung \CodeIgniter \BaseREST ;
12+
1213use Exception ;
14+
1315/**
1416 * Response Component based on CI_Output
1517 *
@@ -40,13 +42,13 @@ class Response
4042 /**
4143 * @var array the formatters that are supported by default
4244 */
43- public $ contentTypes = [
44- self ::FORMAT_RAW => 'text/plain; ' ,
45- self ::FORMAT_HTML => 'text/html; ' ,
46- self ::FORMAT_JSON => 'application/json; ' , // RFC 4627
45+ public $ contentTypes = array (
46+ self ::FORMAT_RAW => 'text/plain; ' ,
47+ self ::FORMAT_HTML => 'text/html; ' ,
48+ self ::FORMAT_JSON => 'application/json; ' , // RFC 4627
4749 self ::FORMAT_JSONP => 'application/javascript; ' , // RFC 4329
48- self ::FORMAT_XML => 'application/xml; ' , // RFC 2376
49- ] ;
50+ self ::FORMAT_XML => 'application/xml; ' , // RFC 2376
51+ ) ;
5052 /**
5153 * @var string the response format. This determines how to convert [[data]] into [[content]]
5254 * when the latter is not set. The value of this property must be one of the keys declared in the [[formatters]] array.
@@ -77,7 +79,7 @@ class Response
7779 function __construct ()
7880 {
7981 // CI_Controller initialization
80- $ this ->ci = & get_instance ();
82+ $ this ->ci = &get_instance ();
8183 }
8284
8385 /**
@@ -90,8 +92,7 @@ public function setFormat($format)
9092 $ this ->_format = $ format ;
9193 // Use formatter content type if exists
9294 if (isset ($ this ->contentTypes [$ this ->_format ])) {
93- $ this ->ci ->output
94- ->set_content_type ($ this ->contentTypes [$ this ->_format ]);
95+ $ this ->ci ->output ->set_content_type ($ this ->contentTypes [$ this ->_format ]);
9596 }
9697
9798 return $ this ;
@@ -101,7 +102,9 @@ public function setFormat($format)
101102 * Set Response Data into CI_Output
102103 *
103104 * @todo Format data before send
105+ *
104106 * @param mixed Response data
107+ *
105108 * @return object self
106109 */
107110 public function setData ($ data )
@@ -136,12 +139,14 @@ public function getStatusCode()
136139 /**
137140 * Sets the response status code.
138141 * This method will set the corresponding status text if `$text` is null.
139- * @param int $code the status code
142+ *
143+ * @param int $code the status code
140144 * @param string $text HTTP status text base on PHP http_response_code().
145+ *
141146 * @throws Exception if the status code is invalid.
142147 * @return $this the response object itself
143148 */
144- public function setStatusCode ($ code , $ text= null )
149+ public function setStatusCode ($ code , $ text = null )
145150 {
146151 if ($ code === null ) {
147152 $ code = 200 ;
@@ -150,7 +155,7 @@ public function setStatusCode($code, $text=null)
150155 $ this ->_statusCode = (int ) $ code ;
151156 // Check status code
152157 if ($ this ->getIsInvalid ()) {
153- throw new Exception ("The HTTP status code is invalid: " . $ this ->_statusCode );
158+ throw new Exception ("The HTTP status code is invalid: " . $ this ->_statusCode );
154159 }
155160 // Set HTTP status code with options
156161 if ($ text ) {
@@ -186,6 +191,7 @@ public function send()
186191 *
187192 * @param array Pre-handle array data
188193 * @param string Format
194+ *
189195 * @return string Formatted data by specified formatter
190196 */
191197 public function format ($ data , $ format )
@@ -197,8 +203,7 @@ public function format($data, $format)
197203 if (method_exists ($ this , $ formatFunc )) {
198204
199205 $ data = $ this ->{$ formatFunc }($ data );
200- }
201- elseif (is_array ($ data )) {
206+ } elseif (is_array ($ data )) {
202207 // Use JSON while the Formatter not found and the data is array
203208 $ data = $ this ->formatJson ($ data );
204209 }
@@ -210,6 +215,7 @@ public function format($data, $format)
210215 * Common format funciton by format types. {FORMAT}Format()
211216 *
212217 * @param array Pre-handle array data
218+ *
213219 * @return string Formatted data
214220 */
215221 public static function formatJson ($ data )
@@ -226,7 +232,7 @@ public static function formatJson($data)
226232 * @return string Response body data
227233 * @throws \Exception
228234 */
229- public function json ($ data , $ statusCode= null )
235+ public function json ($ data , $ statusCode = null )
230236 {
231237 // Set Status Code
232238 if ($ statusCode ) {
@@ -251,8 +257,9 @@ public function json($data, $statusCode=null)
251257 *
252258 * PSR-7 standard
253259 *
254- * @param string $name Case-insensitive header field name to add.
260+ * @param string $name Case-insensitive header field name to add.
255261 * @param string|string[] $value Header value(s).
262+ *
256263 * @return self
257264 */
258265 public function withAddedHeader ($ name , $ value )
0 commit comments