1: <?php
2: namespace PHPixie\HTTP\Messages\Message;
3:
4: use Psr\Http\Message\ResponseInterface;
5: use Psr\Http\Message\StreamInterface;
6:
7: 8: 9:
10: class Response extends Implementation
11: implements ResponseInterface
12: {
13: 14: 15:
16: protected static $codePhrases = array(
17:
18: 100 => 'Continue',
19: 101 => 'Switching Protocols',
20: 102 => 'Processing',
21:
22: 200 => 'OK',
23: 201 => 'Created',
24: 202 => 'Accepted',
25: 203 => 'Non-Authoritative Information',
26: 204 => 'No Content',
27: 205 => 'Reset Content',
28: 206 => 'Partial Content',
29: 207 => 'Multi-status',
30: 208 => 'Already Reported',
31:
32: 300 => 'Multiple Choices',
33: 301 => 'Moved Permanently',
34: 302 => 'Found',
35: 303 => 'See Other',
36: 304 => 'Not Modified',
37: 305 => 'Use Proxy',
38: 306 => 'Switch Proxy',
39: 307 => 'Temporary Redirect',
40:
41: 400 => 'Bad Request',
42: 401 => 'Unauthorized',
43: 402 => 'Payment Required',
44: 403 => 'Forbidden',
45: 404 => 'Not Found',
46: 405 => 'Method Not Allowed',
47: 406 => 'Not Acceptable',
48: 407 => 'Proxy Authentication Required',
49: 408 => 'Request Time-out',
50: 409 => 'Conflict',
51: 410 => 'Gone',
52: 411 => 'Length Required',
53: 412 => 'Precondition Failed',
54: 413 => 'Request Entity Too Large',
55: 414 => 'Request-URI Too Large',
56: 415 => 'Unsupported Media Type',
57: 416 => 'Requested range not satisfiable',
58: 417 => 'Expectation Failed',
59: 418 => 'I\'m a teapot',
60: 422 => 'Unprocessable Entity',
61: 423 => 'Locked',
62: 424 => 'Failed Dependency',
63: 425 => 'Unordered Collection',
64: 426 => 'Upgrade Required',
65: 428 => 'Precondition Required',
66: 429 => 'Too Many Requests',
67: 431 => 'Request Header Fields Too Large',
68:
69: 500 => 'Internal Server Error',
70: 501 => 'Not Implemented',
71: 502 => 'Bad Gateway',
72: 503 => 'Service Unavailable',
73: 504 => 'Gateway Time-out',
74: 505 => 'HTTP Version not supported',
75: 506 => 'Variant Also Negotiates',
76: 507 => 'Insufficient Storage',
77: 508 => 'Loop Detected',
78: 511 => 'Network Authentication Required',
79: );
80:
81: 82: 83:
84: private $reasonPhrase;
85:
86: 87: 88:
89: private $statusCode = 200;
90:
91: 92: 93: 94: 95: 96: 97: 98:
99: public function __construct($protocolVersion, $headers, $body, $statusCode = 200, $reasonPhrase = null)
100: {
101: parent::__construct($protocolVersion, $headers, $body);
102:
103: $this->validateStatusCode($statusCode);
104: if($reasonPhrase === null) {
105: $reasonPhrase = $this->statusCodePhrase($statusCode);
106: }
107:
108: $this->statusCode = $statusCode;
109: $this->reasonPhrase = $reasonPhrase;
110: }
111:
112: 113: 114: 115:
116: protected function statusCodePhrase($statusCode)
117: {
118: if(array_key_exists($statusCode, static::$codePhrases)) {
119: return static::$codePhrases[$statusCode];
120: }
121:
122: return null;
123: }
124:
125: 126: 127:
128: public function getStatusCode()
129: {
130: return $this->statusCode;
131: }
132:
133: 134: 135:
136: public function getReasonPhrase()
137: {
138: return $this->reasonPhrase;
139: }
140:
141: 142: 143:
144: public function withStatus($code, $reasonPhrase = null)
145: {
146: $this->validateStatusCode($code);
147: if($reasonPhrase === null) {
148: $reasonPhrase = $this->statusCodePhrase($code);
149: }
150:
151: $new = clone $this;
152: $new->statusCode = $code;
153: $new->reasonPhrase = $reasonPhrase;
154: return $new;
155: }
156:
157: 158: 159: 160:
161: protected function validateStatusCode($code)
162: {
163: if ($code < 100 || $code > 599) {
164: throw new \InvalidArgumentException("Invalid status '$code', must be between 100 and 599");
165: }
166: }
167: }