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
|
|
|
* Request. |
18
|
|
|
*/ |
19
|
|
|
class Request |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $method; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $get; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $post; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $server; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $cookie; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $headers; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $uri = '/'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $method |
58
|
|
|
* @param array $get |
59
|
|
|
* @param array $post |
60
|
|
|
* @param array $server |
61
|
|
|
* @param array $cookie |
62
|
|
|
* @param array $headers |
63
|
|
|
*/ |
64
|
28 |
|
public function __construct( |
65
|
|
|
$method, |
66
|
|
|
array $get = [], |
67
|
|
|
array $post = [], |
68
|
|
|
array $server = [], |
69
|
|
|
array $cookie = [], |
70
|
|
|
array $headers = [] |
71
|
|
|
) { |
72
|
28 |
|
$this->method = $method; |
73
|
28 |
|
$this->get = $get; |
74
|
28 |
|
$this->post = $post; |
75
|
28 |
|
$this->server = $server; |
76
|
28 |
|
$this->cookie = $cookie; |
77
|
28 |
|
foreach ($headers as $key => $value) { |
78
|
5 |
|
$this->addHeader($key, $value); |
79
|
28 |
|
} |
80
|
|
|
|
81
|
28 |
|
$path = $this->get('server', 'REQUEST_URI'); |
82
|
28 |
|
$parsed = parse_url($path); |
83
|
28 |
|
if (isset($parsed['path'])) { |
84
|
28 |
|
$this->uri = $parsed['path']; |
85
|
28 |
|
} |
86
|
28 |
|
} |
87
|
|
|
|
88
|
1 |
|
public static function createFromGlobals() |
|
|
|
|
89
|
|
|
{ |
90
|
1 |
|
$method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'; |
91
|
1 |
|
$headers = []; |
92
|
1 |
|
foreach ($_SERVER as $name => $value) { |
93
|
1 |
|
if (substr($name, 0, 5) == 'HTTP_') { |
94
|
|
|
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; |
95
|
|
|
} |
96
|
1 |
|
} |
97
|
|
|
|
98
|
1 |
|
return new static($method, $_GET, $_POST, $_SERVER, $_COOKIE, $headers); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
19 |
|
public function getMethod() |
105
|
|
|
{ |
106
|
19 |
|
return $this->method; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
17 |
|
public function getUri() |
113
|
|
|
{ |
114
|
17 |
|
return $this->uri; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $source |
119
|
|
|
* @param string $key |
120
|
|
|
* @param mixed $default |
121
|
|
|
*/ |
122
|
28 |
|
public function get($source, $key, $default = false) |
123
|
|
|
{ |
124
|
28 |
|
if (!in_array($source, ['get', 'post', 'server', 'cookie'])) { |
125
|
|
|
return; |
126
|
|
|
} |
127
|
28 |
|
$data = $this->$source; |
128
|
28 |
|
if (isset($data[$key])) { |
129
|
23 |
|
return $data[$key]; |
130
|
|
|
} |
131
|
|
|
|
132
|
10 |
|
return $default; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $key |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
9 |
View Code Duplication |
public function getHeader($key) |
|
|
|
|
141
|
|
|
{ |
142
|
9 |
|
$key = $this->normalizeHeaderName($key); |
143
|
9 |
|
if (isset($this->headers[$key])) { |
144
|
5 |
|
return $this->headers[$key]; |
145
|
|
|
} |
146
|
7 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $key |
150
|
|
|
* @param string $value |
151
|
|
|
*/ |
152
|
5 |
|
public function addHeader($key, $value) |
153
|
|
|
{ |
154
|
5 |
|
$this->headers[$this->normalizeHeaderName($key)] = $value; |
155
|
5 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $key |
159
|
|
|
* |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
9 |
|
protected function normalizeHeaderName($key) |
163
|
|
|
{ |
164
|
9 |
|
return ucwords(str_replace('_', '-', strtolower($key)), '-'); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: