1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zapheus\Bridge\Psr; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\RequestInterface; |
6
|
|
|
use Psr\Http\Message\StreamInterface; |
7
|
|
|
use Psr\Http\Message\UriInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Request |
11
|
|
|
* |
12
|
|
|
* @package Zapheus |
13
|
|
|
* @author Rougin Gutib <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Request extends Message implements RequestInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $target = '/'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $method = 'GET'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Psr\Http\Message\UriInterface |
29
|
|
|
*/ |
30
|
|
|
protected $uri; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Initializes the request instance. |
34
|
|
|
* |
35
|
|
|
* @param string $method |
36
|
|
|
* @param string $target |
37
|
|
|
* @param \Psr\Http\Message\UriInterface|null $uri |
38
|
|
|
* @param \Psr\Http\Message\StreamInterface|null $body |
39
|
|
|
* @param array $headers |
40
|
|
|
* @param string $version |
41
|
|
|
*/ |
42
|
81 |
|
public function __construct($method = 'GET', $target = '/', UriInterface $uri = null, StreamInterface $body = null, array $headers = array(), $version = '1.1') |
43
|
|
|
{ |
44
|
81 |
|
parent::__construct($body, $headers, $version); |
45
|
|
|
|
46
|
81 |
|
$this->method = $method; |
47
|
|
|
|
48
|
81 |
|
$this->target = $target; |
49
|
|
|
|
50
|
81 |
|
$this->uri = $uri === null ? new Uri : $uri; |
51
|
81 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Retrieves the HTTP method of the request. |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
3 |
|
public function getMethod() |
59
|
|
|
{ |
60
|
3 |
|
return $this->method; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Retrieves the message's request target. |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
3 |
|
public function getRequestTarget() |
69
|
|
|
{ |
70
|
3 |
|
return $this->target; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Retrieves the URI instance. |
75
|
|
|
* |
76
|
|
|
* @return \Psr\Http\Message\UriInterface |
77
|
|
|
*/ |
78
|
30 |
|
public function getUri() |
79
|
|
|
{ |
80
|
30 |
|
return $this->uri; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns an instance with the provided HTTP method. |
85
|
|
|
* |
86
|
|
|
* @param string $method |
87
|
|
|
* @return static |
88
|
|
|
* |
89
|
|
|
* @throws \InvalidArgumentException |
90
|
|
|
*/ |
91
|
3 |
|
public function withMethod($method) |
92
|
|
|
{ |
93
|
|
|
// TODO: Add \InvalidArgumentException |
94
|
|
|
|
95
|
3 |
|
$static = clone $this; |
96
|
|
|
|
97
|
3 |
|
$static->method = $method; |
98
|
|
|
|
99
|
3 |
|
return $static; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns an instance with the specific request-target. |
104
|
|
|
* |
105
|
|
|
* @param mixed $target |
106
|
|
|
* @return static |
107
|
|
|
*/ |
108
|
3 |
|
public function withRequestTarget($target) |
109
|
|
|
{ |
110
|
3 |
|
$static = clone $this; |
111
|
|
|
|
112
|
3 |
|
$static->target = $target; |
113
|
|
|
|
114
|
3 |
|
return $static; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns an instance with the provided URI. |
119
|
|
|
* |
120
|
|
|
* @param \Psr\Http\Message\UriInterface $uri |
121
|
|
|
* @param boolean $preserve |
122
|
|
|
* @return static |
123
|
|
|
*/ |
124
|
3 |
|
public function withUri(UriInterface $uri, $preserve = false) |
125
|
|
|
{ |
126
|
3 |
|
$static = clone $this; |
127
|
|
|
|
128
|
3 |
|
$static->uri = $uri; |
129
|
|
|
|
130
|
3 |
|
if (! $preserve && $host = $uri->getHost()) |
131
|
1 |
|
{ |
132
|
3 |
|
$port = $host . ':' . $uri->getPort(); |
133
|
|
|
|
134
|
3 |
|
$host = $uri->getPort() ? $port : $host; |
135
|
|
|
|
136
|
3 |
|
$static->headers['Host'] = (array) $host; |
137
|
1 |
|
} |
138
|
|
|
|
139
|
3 |
|
return $static; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|