This repository was archived by the owner on May 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponse.php
More file actions
83 lines (72 loc) · 1.5 KB
/
Response.php
File metadata and controls
83 lines (72 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace http;
class Response {
protected $statusCode = 200;
protected $statusText = 'OK';
protected $body = '';
protected $headers = array();
public function __construct(array $headers, $body) {
list(, $statusCode, $statusText) = explode(' ', array_shift($headers), 3);
$this->setStatus($statusCode, $statusText);
$this->setHeaders($headers);
$this->setBody($body);
}
/**
* @param int $code
* @param string $text
* @return Response
*/
protected function setStatus($code, $text) {
$this->statusCode = (int) $code;
$this->statusText = (string) $text;
return $this;
}
/**
* @param array $headers
* @return Response
*/
protected function setHeaders(array $headers) {
foreach($headers as $header) {
list($name, $value) = explode(':', $header, 2);
$this->headers[$name] = trim($value);
}
return $this;
}
/**
* @param string|null $name
* @return string[]|string|null
*/
public function getHeader($name = null) {
if ($name === null) {
return $this->headers;
} else if (array_key_exists($name, $this->headers)) {
return $this->headers[$name];
}
return null;
}
/**
* @return string
*/
public function getBody() {
return $this->body;
}
/**
* @param string $body
* @return string
*/
protected function setBody($body) {
$this->body = (string) $body;
}
/**
* @return int
*/
public function getStatus() {
return $this->statusCode;
}
/**
* @return string
*/
public function getStatusText() {
return $this->statusText;
}
}