| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace LunixREST\Request\HeaderParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | class BasicHeaderParser implements HeaderParser { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |     private $apiKeyHeaderKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |      * BasicHeaderParser constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * @param string $apiKeyHeaderKey | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     public function __construct($apiKeyHeaderKey = 'x-api-key') { | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |         $this->apiKeyHeaderKey = $apiKeyHeaderKey; | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     public function findAcceptableMIMETypes(array $headers): array{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         //TODO: follow RFC2616 order | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $acceptedMIMETypes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         foreach($headers as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |             if(strtolower($key) == 'http-accept'){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |                 $values = explode(',', $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |                 foreach($values as $acceptedType) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |                     $typeParts = explode(';', $acceptedType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |                     if(count($typeParts) > 0 ){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |                         $acceptedMIMETypes[] = trim($typeParts); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         return $acceptedMIMETypes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function findAPIKey(array $headers){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         foreach($headers as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             if(strtolower($key) == $this->apiKeyHeaderKey){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function getContentType(array $headers){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         foreach($headers as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             if(strtolower($key) == 'content-type'){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public function parse(array $headers): ParsedHeaders { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $contentType = $this->getContentType($headers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $acceptableMIMETypes = $this->findAcceptableMIMETypes($headers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $apiKey = $this->findAPIKey($headers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         return new ParsedHeaders($contentType, $acceptableMIMETypes, $apiKey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 59 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |  |