11<?php namespace MiladRahimi \PHPRouter ;
22
3+ use MiladRahimi \PHPRouter \Exceptions \InvalidArgumentException ;
4+ use MiladRahimi \PHPTemplate \Exceptions \FileNotFoundException ;
5+
36/**
47 * Class Response
58 * Response Class is used to help developer to response the matched route, it
69 * includes methods like redirect(), render(), etc.
710 *
811 * @package MiladRahimi\PHPRouter
9- * @author Milad Rahimi <[email protected] > 12+ * @author Milad Rahimi <[email protected] > 1013 */
11- class Response
12- {
14+ class Response {
1315
1416 /**
1517 * Singleton instance of the class
@@ -30,21 +32,21 @@ class Response
3032 *
3133 * @param Router $rooter
3234 */
33- private function __construct (Router $ rooter )
34- {
35- if (!($ rooter instanceof Router))
35+ private function __construct (Router $ rooter ) {
36+ if (!($ rooter instanceof Router)) {
3637 throw new \InvalidArgumentException ("Neatplex PHPRouter: Invalid object given instead of Router object " );
38+ }
3739 $ this ->router = $ rooter ;
3840 }
3941
4042 /**
4143 * Get singleton instance of the class
4244 *
4345 * @param Router $router
46+ *
4447 * @return Request
4548 */
46- public static function getInstance (Router $ router )
47- {
49+ public static function getInstance (Router $ router ) {
4850 return isset (self ::$ instance ) ? self ::$ instance : self ::$ instance = new Response ($ router );
4951 }
5052
@@ -53,10 +55,10 @@ public static function getInstance(Router $router)
5355 *
5456 * @param string $to
5557 */
56- public function redirect ($ to = "/ " )
57- {
58- if (!is_scalar ($ to ))
58+ public function redirect ($ to = "/ " ) {
59+ if (!is_scalar ($ to )) {
5960 throw new InvalidArgumentException ("To must be a string value " );
61+ }
6062 $ to = $ this ->router ->getBaseURI () . (is_string ($ to ) ? $ to : "" );
6163 ob_start ();
6264 ob_clean ();
@@ -67,49 +69,50 @@ public function redirect($to = "/")
6769 /**
6870 * @return string
6971 */
70- public function __toString ()
71- {
72+ public function __toString () {
7273 return ob_get_contents ();
7374 }
7475
7576 /**
7677 * Render (PHP) file
7778 *
7879 * @param string $file : PHP file to render
79- * @throws InvalidArgumentException
80- * @throws PHPRouterError
80+ *
81+ * @throws FileNotFoundException
8182 */
82- public function render ($ file )
83- {
84- if (!is_scalar ($ file ))
83+ public function render ($ file ) {
84+ if (!is_scalar ($ file )) {
8585 throw new InvalidArgumentException ("File must be a string value " );
86+ }
8687 if (file_exists ($ file )) {
88+ /** @noinspection PhpIncludeInspection */
8789 require $ file ;
8890 } else {
89- throw new PHPRouterError ( " File does not exist " );
91+ throw new FileNotFoundException ( );
9092 }
9193 }
9294
93-
9495 /**
9596 * Response cookies (write-only)
9697 *
9798 * @param string $name
9899 * @param string $value
99- * @param int $expire
100+ * @param int $expire
100101 * @param string $path
101102 * @param string $domain
102- * @param bool $secure
103- * @param bool $httponly
103+ * @param bool $secure
104+ * @param bool $httponly
104105 *
105106 * @return array
106107 */
107- public function cookie ($ name , $ value , $ expire = 0 , $ path = null , $ domain = null , $ secure = false , $ httponly = false )
108- {
109- if (!isset ($ name ) || !is_scalar ($ name ))
108+ public function cookie ($ name , $ value , $ expire = 0 , $ path = null , $ domain = null , $ secure = false ,
109+ $ httponly = false ) {
110+ if (!isset ($ name ) || !is_scalar ($ name )) {
110111 throw new InvalidArgumentException ("Name must be a string value " );
111- if (!isset ($ value ))
112+ }
113+ if (!isset ($ value )) {
112114 throw new InvalidArgumentException ("Value must be set " );
115+ }
113116 return setcookie ($ name , $ value , $ expire , $ path , $ domain , $ secure , $ httponly );
114117 }
115118
@@ -118,8 +121,7 @@ public function cookie($name, $value, $expire = 0, $path = null, $domain = null,
118121 *
119122 * @return string
120123 */
121- public function contents ()
122- {
124+ public function contents () {
123125 return ob_get_contents ();
124126 }
125127
@@ -128,21 +130,20 @@ public function contents()
128130 *
129131 * @param string|mixed $content
130132 */
131- public function publish ($ content )
132- {
133- if (!isset ($ content ))
134- throw new InvalidArgumentException ("Content must be set " );
133+ public function publish ($ content = null ) {
135134 // Open output stream
136135 $ fp = fopen ("php://output " , 'r+ ' );
137136 // Raw content
138137 if (is_string ($ content ) || is_numeric ($ content ) || is_null ($ content )) {
139138 fputs ($ fp , $ content );
140139 } // Object with __toString method
141- else if (is_object ($ content ) && method_exists ($ content , "__toString " )) {
142- fputs ($ fp , $ content ->__toString ());
143- } // Else
144140 else {
145- fputs ($ fp , print_r ($ content , true ));
141+ if (is_object ($ content ) && method_exists ($ content , "__toString " )) {
142+ fputs ($ fp , $ content ->__toString ());
143+ } // Else
144+ else {
145+ fputs ($ fp , print_r ($ content , true ));
146+ }
146147 }
147148 }
148149}
0 commit comments