1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the "Kata 1" package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Daniel González |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @author Daniel González <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Component\Http; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Response. |
18
|
|
|
*/ |
19
|
|
|
class Response |
20
|
|
|
{ |
21
|
|
|
const HTTP_VERSION = 1.1; |
22
|
|
|
const HTTP_OK = 200; |
23
|
|
|
const HTTP_CREATED = 201; |
24
|
|
|
const HTTP_MOVED_PERMANENTLY = 301; |
25
|
|
|
const HTTP_FOUND = 302; |
26
|
|
|
const HTTP_BAD_REQUEST = 400; |
27
|
|
|
const HTTP_UNAUTHORIZED = 401; |
28
|
|
|
const HTTP_NOT_FOUND = 404; |
29
|
|
|
const HTTP_METHOD_NOT_ALLOWED = 405; |
30
|
|
|
const HTTP_INTERNAL_SERVER_ERROR = 500; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
public static $statusTexts = [ |
36
|
|
|
200 => 'OK', |
37
|
|
|
201 => 'Created', |
38
|
|
|
301 => 'Moved Permanently', |
39
|
|
|
302 => 'Found', |
40
|
|
|
401 => 'Unauthorized', |
41
|
|
|
402 => 'Payment Required', |
42
|
|
|
403 => 'Forbidden', |
43
|
|
|
404 => 'Not Found', |
44
|
|
|
405 => 'Method Not Allowed', |
45
|
|
|
500 => 'Internal Server Error', |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array; |
50
|
|
|
*/ |
51
|
|
|
protected $headers; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var int |
55
|
|
|
*/ |
56
|
|
|
protected $statusCode; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $content; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $content |
65
|
|
|
* @param int $status |
66
|
|
|
* @param array $headers |
67
|
|
|
*/ |
68
|
17 |
|
public function __construct($content = '', $status = self::HTTP_OK, array $headers = []) |
69
|
|
|
{ |
70
|
17 |
|
$this->addHeader('Content-Type', 'text/html; charset=UTF-8'); |
71
|
17 |
|
foreach ($headers as $key => $value) { |
72
|
13 |
|
$this->addHeader($key, $value); |
73
|
17 |
|
} |
74
|
17 |
|
$this->statusCode = $status; |
75
|
17 |
|
$this->content = $content; |
76
|
17 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
public function getHeaders() |
82
|
|
|
{ |
83
|
|
|
return $this->headers; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $key |
88
|
|
|
* @param string $value |
89
|
|
|
*/ |
90
|
17 |
|
public function addHeader($key, $value) |
91
|
|
|
{ |
92
|
17 |
|
$this->headers[$this->normalizeHeaderName($key)] = $value; |
93
|
17 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $key |
97
|
|
|
* |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
16 |
View Code Duplication |
public function getHeader($key) |
|
|
|
|
101
|
|
|
{ |
102
|
16 |
|
$key = $this->normalizeHeaderName($key); |
103
|
16 |
|
if (isset($this->headers[$key])) { |
104
|
16 |
|
return $this->headers[$key]; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return int |
110
|
|
|
*/ |
111
|
17 |
|
public function getStatusCode() |
112
|
|
|
{ |
113
|
17 |
|
return $this->statusCode; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
17 |
|
public function getContent() |
120
|
|
|
{ |
121
|
17 |
|
return $this->content; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function send() |
125
|
|
|
{ |
126
|
|
|
$statusText = isset(self::$statusTexts[$this->statusCode]) ? self::$statusTexts[$this->statusCode] : ''; |
127
|
|
|
header(sprintf('HTTP/%s %s %s', self::HTTP_VERSION, $this->statusCode, $statusText), true, $this->statusCode); |
128
|
|
|
foreach ($this->headers as $name => $value) { |
129
|
|
|
header($name.': '.$value); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
echo $this->content; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $key |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
17 |
|
protected function normalizeHeaderName($key) |
141
|
|
|
{ |
142
|
17 |
|
return ucwords(str_replace('_', '-', strtolower($key)), '-'); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.