| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace Selami; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Selami\Router\Router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Zend\Diactoros\Stream; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use finfo; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  | final class ControllerResponse | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     private $returnType; | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     private $headers; | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     private $data; | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     private $statusCode; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     private $metaData; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     public function __construct(int $returnType, int $statusCode, array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $this->returnType = $returnType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $this->statusCode = $statusCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $this->headers = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this->data = $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $this->metaData = []; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function withHeaders(array $headers) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $new = clone $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $new->headers = $headers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         return $new; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function withMetaData(array $metaData) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $new = clone $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $new->metaData = $metaData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         return $new; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public static function CUSTOM(int $statusCode, array $data, ?array $metaData=[], ?array $headers =[]) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         return (new self(Router::CUSTOM, $statusCode, $data)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             ->withHeaders($headers) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             ->withMetaData($metaData); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public static function EMPTY(int $statusCode, array $data, ?array $metaData=[], ?array $headers=[]) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return (new self(Router::EMPTY, $statusCode, $data)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             ->withHeaders($headers) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             ->withMetaData($metaData); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public static function HTML(int $statusCode, array $data, ?array $metaData = [], ?array $headers=[]) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         return (new self(Router::HTML, $statusCode, $data)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             ->withHeaders($headers) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             ->withMetaData($metaData); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     public static function JSON(int $statusCode, array $data, ?array $headers=[]) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         return (new self(Router::JSON, $statusCode, $data)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             ->withHeaders($headers); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public static function TEXT(int $statusCode, array $data) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return new self(Router::TEXT, $statusCode, $data); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     public static function XML(int $statusCode, array $xmlData, ?array $headers=[]) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return (new self(Router::XML, $statusCode,  $xmlData )) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             ->withHeaders($headers); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     public static function REDIRECT(int $statusCode, string $redirectUrl) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return (new self(Router::REDIRECT, $statusCode, [])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             ->withHeaders(['uri' => $redirectUrl]); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public static function DOWNLOAD(int $statusCode, string $filePath, ?string $fileName = null) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $stream = new Stream(realpath($filePath), 'r'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $headers = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             'Content-Type' => (new finfo(FILEINFO_MIME))->file($filePath), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             'Content-Disposition' => 'attachment; filename=' . $fileName ?? basename($filePath), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             'Content-Transfer-Encoding' => 'Binary', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             'Content-Description' => 'File Transfer', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             'Pragma' =>  'public', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             'Expires' => '0', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             'Cache-Control' =>  'must-revalidate', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             'Content-Length' =>  (string) $stream->getSize() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         return (new self(Router::DOWNLOAD, $statusCode, [])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             ->withHeaders($headers) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             ->withMetaData(['stream' => $stream]); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     public function getReturnType() : int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         return $this->returnType; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     public function getStatusCode() : int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         return $this->statusCode; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     public function getHeaders() : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         return $this->headers; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     public function getData() : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         return $this->data; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     public function getMetaData() : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         return $this->metaData; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 127 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |  |