1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fracture\Http; |
4
|
|
|
|
5
|
|
|
class Response |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
private $headers = []; |
10
|
|
|
private $cookies = []; |
11
|
|
|
|
12
|
|
|
private $code = 200; |
13
|
|
|
private $body = ''; |
14
|
|
|
|
15
|
|
|
private $hostname = ''; |
16
|
|
|
|
17
|
|
|
private $locationHeader = null; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
|
21
|
3 |
|
public function setHostname($hostname) |
22
|
|
|
{ |
23
|
3 |
|
$this->hostname = $hostname; |
24
|
3 |
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
1 |
|
public function setBody($content) |
28
|
|
|
{ |
29
|
1 |
|
$this->body = $content; |
30
|
1 |
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
1 |
|
public function appendBody($content) |
34
|
|
|
{ |
35
|
1 |
|
$this->body = $this->body . $content; |
36
|
1 |
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
1 |
|
public function prependBody($content) |
40
|
|
|
{ |
41
|
1 |
|
$this->body = $content . $this->body; |
42
|
1 |
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
2 |
|
public function getBody() |
46
|
|
|
{ |
47
|
2 |
|
return $this->body; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
1 |
|
public function addCookie($name, $value, $options = []) |
52
|
|
|
{ |
53
|
1 |
|
$header = new Headers\SetCookie($name, $value, $options); |
54
|
1 |
|
$header->prepare(); |
55
|
1 |
|
$this->cookies[$name] = $header; |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
2 |
|
public function removeCookie($name, $options = []) |
60
|
|
|
{ |
61
|
2 |
|
$this->addCookie($name, 'deleted', ['expires' => 0] + $options); |
62
|
2 |
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
6 |
|
public function addHeader(Headers\Abstracted $header) |
66
|
|
|
{ |
67
|
6 |
|
$name = $header->getName(); |
68
|
6 |
|
$name = strtolower($name); |
69
|
|
|
|
70
|
6 |
|
if ($name === 'location') { |
71
|
4 |
|
$this->locationHeader = $header; |
72
|
4 |
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
$this->headers[$name] = $header; |
76
|
2 |
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
10 |
|
public function getHeaders() |
80
|
|
|
{ |
81
|
10 |
|
$list = []; |
82
|
|
|
|
83
|
10 |
|
$list = $this->populateHeaderList($list, $this->cookies); |
84
|
10 |
|
$list = $this->populateHeaderList($list, $this->headers); |
85
|
|
|
|
86
|
10 |
|
if ($this->locationHeader !== null) { |
87
|
4 |
|
$this->code = 302; |
88
|
4 |
|
$location = $this->locationHeader; |
89
|
|
|
|
90
|
4 |
|
$value = $this->adjustForHostname($location->getValue()); |
91
|
|
|
|
92
|
4 |
|
$list[] = [ |
93
|
4 |
|
'value' => $location->getName(). ': ' . $value, |
94
|
|
|
'replace' => true, |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
10 |
|
return $list; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
6 |
|
private function populateHeaderList($list, $headers) |
103
|
|
|
{ |
104
|
6 |
|
foreach ($headers as $header) { |
105
|
|
|
|
106
|
2 |
|
$name = $header->getName(); |
107
|
2 |
|
$value = $header->getValue(); |
108
|
|
|
|
109
|
2 |
|
$list[] = [ |
110
|
2 |
|
'value' => $name . ': ' . $value, |
111
|
2 |
|
'replace' => $header->isFinal() === false, |
112
|
|
|
]; |
113
|
|
|
} |
114
|
|
|
|
115
|
6 |
|
return $list; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
4 |
|
private function adjustForHostname($value) |
120
|
|
|
{ |
121
|
4 |
|
if (preg_match('#^https?://*#', $value) === 0) { |
122
|
3 |
|
$value = $this->hostname . $value; |
123
|
|
|
} |
124
|
|
|
|
125
|
4 |
|
return $value; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
2 |
|
public function setStatusCode($code) |
130
|
|
|
{ |
131
|
2 |
|
$code = (int)$code; |
132
|
|
|
|
133
|
2 |
|
if ($code < 100 || $code > 599) { |
134
|
1 |
|
throw new \InvalidArgumentException('Invalid response status code'); |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
$this->code = $code; |
138
|
1 |
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
2 |
|
public function getStatusCode() |
142
|
|
|
{ |
143
|
2 |
|
return $this->code; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|