1 | <?php |
||
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) |
|
25 | |||
26 | |||
27 | 1 | public function setBody($content) |
|
31 | |||
32 | |||
33 | 1 | public function appendBody($content) |
|
37 | |||
38 | |||
39 | 1 | public function prependBody($content) |
|
43 | |||
44 | |||
45 | 2 | public function getBody() |
|
49 | |||
50 | |||
51 | 1 | public function addCookie($name, $value, $options = []) |
|
57 | |||
58 | |||
59 | 2 | public function removeCookie($name, $options = []) |
|
63 | |||
64 | |||
65 | 6 | public function addHeader(Headers\Abstracted $header) |
|
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) |
|
139 | |||
140 | |||
141 | 2 | public function getStatusCode() |
|
145 | } |
||
146 |