| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace Selami\Foundation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Selami; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Psr\Container\ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Selami\View\ViewInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Selami\Stdlib\CaseConverter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\HttpFoundation\Session\Session; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     private $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     private $view; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private $session; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $statusCode = 200; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $headers = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $cookies = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $body = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     private $data = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     private $contentType = Selami\Router::HTML; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     private $redirect; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     private $downloadFilePath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     private $downloadFileName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     private $customContentType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public function __construct(ContainerInterface $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $this->container = $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $this->config = $container->get('config'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     private function checkTemplateFile($template, $type, $controller) : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         if (!file_exists($this->config['app']['templates_path'] .'/'. $template)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $message  = sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 '%s  template file not found! %s  needs a main template file at: %s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 $type, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 $controller, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 $this->config['app_dir'] .'/'. $template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             throw new \DomainException($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     public function setResponse(int $returnType, array $actionOutput, string $controller) : void | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         switch ($returnType) { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             case Selami\Router::HTML: | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |                 $this->setRenderedResponse(Selami\Router::HTML, $actionOutput, $controller); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             case Selami\Router::JSON: | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |                 $this->setJsonResponse($actionOutput); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |             case Selami\Router::TEXT: | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |                 $this->setRenderedResponse(Selami\Router::TEXT, $actionOutput, $controller); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |             case Selami\Router::XML: | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |                 $this->setRenderedResponse(Selami\Router::XML, $actionOutput, $controller); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |             case Selami\Router::DOWNLOAD: | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |                 $this->setDownloadResponse($actionOutput); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |             case Selami\Router::CUSTOM: | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |                 $this->setRenderedResponse(Selami\Router::CUSTOM, $actionOutput, $controller); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |             case Selami\Router::REDIRECT: | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |                 $this->setRedirectResponse($actionOutput); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     public function setRedirectResponse(array $actionOutput) : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $this->contentType = Selami\Router::REDIRECT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         if (isset($actionOutput['meta']['type']) && $actionOutput['meta']['type'] === Dispatcher::REDIRECT) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $this->contentType = Selami\Router::REDIRECT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $this->statusCode = $actionOutput['status'] ?? 302; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $this->redirect = $actionOutput['meta']['redirect_url']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     public function setDownloadResponse(array $actionOutput) : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $this->contentType = Selami\Router::DOWNLOAD; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         if (isset($actionOutput['meta']['type']) ?? $actionOutput['meta']['type'] === Dispatcher::DOWNLOAD) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             $statusCode = $actionOutput['status'] ?? 200; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             $this->statusCode = (int) $statusCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             $this->downloadFilePath = $actionOutput['meta']['download_file_path']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             $this->downloadFileName = $actionOutput['meta']['download_file_name'] ?? date('Ymdhis'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     public function setJsonResponse(array $actionOutput) : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $this->contentType = Selami\Router::JSON; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         if (!is_array($actionOutput)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             $actionOutput = ['status' => 500, 'error' => 'Internal Server Error']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         if (!isset($actionOutput['status'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             $actionOutput['status'] = 200; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $status = (int) $actionOutput['status']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $this->statusCode = $status; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $this->data = $actionOutput; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     private function setRenderedResponse(int $returnType, array $actionOutput, string $controllerClass) : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $this->useSession(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $this->useView($this->container->get(ViewInterface::class)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $this->view->addGlobal('defined', get_defined_constants(true)['user'] ?? []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $this->view->addGlobal('session', $this->session->all()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $this->renderResponse($returnType, $actionOutput, $controllerClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     private function renderResponse(int $returnType, array $actionOutput, string $controllerClass) : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $paths = explode("\\", $controllerClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $templateFile = array_pop($paths); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $templateFolder = array_pop($paths); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $template = CaseConverter::toSnakeCase($templateFolder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             . '/' . CaseConverter::toSnakeCase($templateFile) . '.twig'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $this->checkTemplateFile($template, 'Method\'s', $controllerClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $actionOutput['data'] = $actionOutput['data'] ?? []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $output = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             'status' => $actionOutput['status'] ?? 200, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             'meta' => $actionOutput['meta'] ?? [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             'app' => null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $output['app']['_content'] = $this->view->render($template, $actionOutput['data']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         $mainTemplateName = $actionOutput['meta']['layout'] ?? 'default'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         $mainTemplate = '_' . CaseConverter::toSnakeCase($mainTemplateName) . '.twig'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         $this->checkTemplateFile($mainTemplate, 'Layout', $controllerClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         $this->contentType = $returnType; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         if ($returnType === Selami\Router::CUSTOM) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |             $this->customContentType = $actionOutput['meta']['content_type'] ?? 'plain/text'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         $this->body = $this->view->render($mainTemplate, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     public function notFound($status = 404, $returnType = Selami\Router::HTML, $message = 'Not Found') : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         if ($returnType === Selami\Router::JSON) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             $this->body = ['status' => $status, 'message' => $message]; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             $this->useView($this->container->get(ViewInterface::class)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |             $notFoundTemplate = '_404.twig'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |             $this->contentType = $returnType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |             $this->body = $this->view->render( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |                 $notFoundTemplate, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |                 ['message' => $message, 'status' => $status] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         $this->statusCode = $status; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     private function useView(ViewInterface $view) : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         $this->view = $view; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     private function useSession() : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         $this->session = $this->container->get(Session::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     private function setHeaders() : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         $this->headers['X-Powered-By']      = 'r/selami'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |         $this->headers['X-Frame-Options']   = 'SAMEORIGIN'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |         $this->headers['X-XSS-Protection']  = '1; mode=block'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |         $this->headers['Strict-Transport-Security'] = 'max-age=31536000'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         if (array_key_exists('headers', $this->config) && is_array($this->config['headers'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |             foreach ($this->config['headers'] as $header => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |                 $this->headers[$header] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |     public function getResponse() : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |         $headers = $this->config['headers'] ?? null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |         $this->setHeaders($headers); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |             'statusCode'    => $this->statusCode, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |             'headers'       => $this->headers, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |             'cookies'       => $this->cookies, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |             'body'          => (string) $this->body, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |             'data'          => $this->data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |             'contentType'   => $this->contentType, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |             'redirect'      => $this->redirect | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |     public function sendResponse() : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |         $response = new Selami\Http\Response(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |         $response->setHeaders($this->headers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |         $response->setStatusCode($this->statusCode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |         switch ($this->contentType) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |             case Selami\Router::REDIRECT: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |                 $response->setOutputType(Selami\Router::REDIRECT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |                 $response->setRedirect($this->redirect); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |             case Selami\Router::CUSTOM: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |                 $response->setOutputType(Selami\Router::CUSTOM); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |                 $response->setCustomContentType($this->customContentType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |                 $response->setBody($this->body); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |             case Selami\Router::DOWNLOAD: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |                 $response->setOutputType(Selami\Router::DOWNLOAD); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |                 $response->setDownloadFileName($this->downloadFileName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |                 $response->setDownloadFilePath($this->downloadFilePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |             case Selami\Router::JSON: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |                 $response->setOutputType(Selami\Router::JSON); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |                 $response->setData($this->data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |             case Selami\Router::TEXT: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |                 $response->setOutputType(Selami\Router::TEXT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |                 $response->setBody($this->body); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |             case Selami\Router::XML: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |                 $response->setOutputType(Selami\Router::XML); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |                 $response->setBody($this->body); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |             case Selami\Router::HTML: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |             default: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |                 $response->setOutputType(Selami\Router::HTML); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |                 $response->setBody($this->body); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |         $response->send(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 262 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 263 |  |  |  | 
            
                        
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.