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
|
|
|
|
16
|
|
|
class Response |
17
|
|
|
{ |
18
|
|
|
public |
19
|
|
|
$code, |
|
|
|
|
20
|
|
|
$errorCode, |
21
|
|
|
$error, |
22
|
|
|
$header, |
23
|
|
|
$body, |
24
|
|
|
/** |
25
|
|
|
* @var Request |
26
|
|
|
*/ |
27
|
|
|
$request, |
28
|
|
|
$contentType, |
29
|
|
|
$charset, |
30
|
|
|
$duration, |
31
|
|
|
$info; |
32
|
|
|
protected function __construct() |
33
|
|
|
{ |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function create(Request $request, $body, $info, $errorCode, $error){ |
37
|
|
|
$self = new self; |
38
|
|
|
$self->request = $request; |
39
|
|
|
$self->body = $body; |
40
|
|
|
$self->info = $info; |
41
|
|
|
$self->errorCode = $errorCode; |
42
|
2 |
|
$self->error = $error; |
43
|
|
|
$self->parse(); |
44
|
2 |
|
$self->check(); |
45
|
|
|
return $self; |
46
|
|
|
} |
47
|
|
|
public function check(){ |
48
|
|
|
|
49
|
|
|
} |
50
|
|
|
public function parse(){ |
51
|
|
|
//has header |
52
|
|
|
if($this->request->getIni('header')){ |
53
|
|
|
$headers = rtrim(substr($this->body, 0, $this->info['header_size'])); |
54
|
2 |
|
$this->body = substr($this->body, $this->info['header_size']); |
55
|
|
|
$headers = explode(PHP_EOL, $headers); |
56
|
2 |
|
array_shift($headers); // HTTP HEADER |
57
|
2 |
|
foreach($headers as $h) { |
58
|
2 |
|
if(false !== strpos($h, ':')) |
59
|
2 |
|
list($k, $v) = explode(':', $h, 2); |
60
|
2 |
|
else |
61
|
2 |
|
list($k, $v) = array($h,''); |
62
|
2 |
|
|
63
|
|
|
$this->header[trim($k)] = trim($v); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
$this->code = $this->info['http_code']; |
67
|
2 |
|
$this->duration = $this->info['total_time']; |
68
|
|
|
$this->contentType = $this->info['content_type']; |
69
|
|
|
$content_type = isset($this->info['content_type']) ? $this->info['content_type'] : ''; |
70
|
2 |
|
$content_type = explode(';', $content_type); |
71
|
2 |
|
$this->contentType = $content_type[0]; |
72
|
2 |
|
if (count($content_type) == 2 && strpos($content_type[1], '=') !== false) { |
73
|
2 |
|
list( , $this->charset) = explode('=', $content_type[1]); |
74
|
2 |
|
} |
75
|
2 |
|
} |
76
|
2 |
|
|
77
|
|
|
/** |
78
|
1 |
|
* Status Code Definitions |
79
|
|
|
* |
80
|
2 |
|
* Informational 1xx |
81
|
2 |
|
* Successful 2xx |
82
|
|
|
* Redirection 3xx |
83
|
2 |
|
* Client Error 4xx |
84
|
2 |
|
* Server Error 5xx |
85
|
2 |
|
* |
86
|
2 |
|
* http://pretty-rfc.herokuapp.com/RFC2616#status.codes |
87
|
2 |
|
* |
88
|
2 |
|
* @return bool Did we receive a 4xx or 5xx? |
89
|
2 |
|
*/ |
90
|
2 |
|
public function hasErrors() |
91
|
2 |
|
{ |
92
|
|
|
return $this->code == 0 || $this->code >= 400; |
93
|
2 |
|
} |
94
|
|
|
|
95
|
|
|
} |
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.