| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @author  [email protected] [email protected] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Date: 2017/6/19 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Time: 10:55 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @version $Id: $ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @since 1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @copyright Sina Corp. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace MultiHttp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use MultiHttp\Exception\InvalidOperationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use MultiHttp\Exception\UnexpectedResponseException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * Class Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @package MultiHttp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $code, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $errorCode, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $error, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $header, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $body, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |          * @var Request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $request, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $contentType, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $charset, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $duration, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $info; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * Response constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 6 |  |     protected function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 6 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param Request $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param $body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param $info | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param $errorCode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param $error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return Response | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 6 |  |     public static function create(Request $request, $body, $info, $errorCode, $error) | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 6 |  |         $self = new self; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 6 |  |         $self->request = $request; | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 6 |  |         $self->body = $body; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 6 |  |         $self->info = $info; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 6 |  |         $self->errorCode = $errorCode; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 6 |  |         $self->error = $error; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 6 |  |         return $self; | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 5 |  |     public function parse() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         //has header | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 5 |  |         $headers = rtrim(substr($this->body, 0, $this->info['header_size'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 5 |  |         $this->body = substr($this->body, $this->info['header_size']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 5 |  |         $headers = explode(PHP_EOL, $headers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 5 |  |         array_shift($headers); // HTTP HEADER | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 5 |  |         foreach ($headers as $h) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 5 |  |             if (false !== strpos($h, ':')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 5 |  |                 list($k, $v) = explode(':', $h, 2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |                 list($k, $v) = array($h, ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 5 |  |             $this->header[trim($k)] = trim($v); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 5 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 5 |  |         $this->code = $this->info['http_code']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 5 |  |         $this->duration = $this->info['total_time']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 5 |  |         $this->contentType = $this->info['content_type']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 5 |  |         $content_type = isset($this->info['content_type']) ? $this->info['content_type'] : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 5 |  |         $content_type = explode(';', $content_type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 5 |  |         $this->contentType = $content_type[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 5 |  |         if (count($content_type) == 2 && strpos($content_type[1], '=') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 5 |  |             list(, $this->charset) = explode('=', $content_type[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 5 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 5 |  |         $this->unserializeBody(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 5 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 5 |  |     public function unserializeBody() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 5 |  |         if (isset($this->request->expectedMime)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 2 |  |             if (Mime::getFullMime($this->request->expectedMime) !== $this->contentType) throw new UnexpectedResponseException('expected mime can not be matched, real mime:'. $this->contentType. ', expected mime:'. Mime::getFullMime($this->request->expectedMime)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 2 |  |             $method = 'un'.ucfirst($this->request->expectedMime); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 2 |  |             if (!method_exists($this->request, $method)) throw new InvalidOperationException($method . ' is not exists in ' . __CLASS__); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 2 |  |             $this->body = $this->request->{$method}($this->body); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 5 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * Status Code Definitions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * Informational 1xx | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * Successful    2xx | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * Redirection   3xx | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * Client Error  4xx | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * Server Error  5xx | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * http://pretty-rfc.herokuapp.com/RFC2616#status.codes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @return bool Did we receive a 4xx or 5xx? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 2 |  |     public function hasErrors() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 2 |  |         return $this->code == 0 || $this->code >= 400; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 123 |  |  | } | 
            
                        
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.