|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CodeJet\Http; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\RequestInterface; |
|
6
|
|
|
use Psr\Http\Message\UriInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Request |
|
10
|
|
|
* @package CodeJet\Http |
|
11
|
|
|
*/ |
|
12
|
|
|
class Request extends Message implements RequestInterface |
|
13
|
|
|
{ |
|
14
|
|
|
const DEFAULT_REQUEST_TARGET = "/"; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Request Method SHOULD be one of GET,PUT,POST,PATCH,DELETE |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $method = 'GET'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var UriInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $uri; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $requestTarget; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @inheritdoc |
|
34
|
|
|
*/ |
|
35
|
|
|
public function getRequestTarget() |
|
36
|
|
|
{ |
|
37
|
|
|
if ($this->requestTarget) { |
|
38
|
|
|
return $this->requestTarget; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$requestTarget = self::DEFAULT_REQUEST_TARGET; |
|
42
|
|
|
|
|
43
|
|
|
if ($this->uri) { |
|
44
|
|
|
$requestTarget = $this->uri->getPath(); |
|
45
|
|
|
|
|
46
|
|
|
if (!empty($this->uri->getQuery())) { |
|
47
|
|
|
$requestTarget .= '?' . $this->uri->getQuery(); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $requestTarget; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @inheritdoc |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getMethod() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->method; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @inheritdoc |
|
64
|
|
|
*/ |
|
65
|
|
|
public function withRequestTarget($requestTarget) |
|
66
|
|
|
{ |
|
67
|
|
|
$clone = clone $this; |
|
68
|
|
|
$clone->requestTarget = $requestTarget; |
|
69
|
|
|
|
|
70
|
|
|
return $clone; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @inheritdoc |
|
75
|
|
|
*/ |
|
76
|
|
|
public function withMethod($method) |
|
77
|
|
|
{ |
|
78
|
|
|
$clone = clone $this; |
|
79
|
|
|
$clone->method = $method; |
|
80
|
|
|
|
|
81
|
|
|
return $clone; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @inheritdoc |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getUri() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->uri; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @inheritdoc |
|
94
|
|
|
*/ |
|
95
|
|
|
public function withUri(UriInterface $uri, $preserveHost = false) |
|
96
|
|
|
{ |
|
97
|
|
|
$clone = clone $this; |
|
98
|
|
|
$clone->uri = $uri; |
|
99
|
|
|
|
|
100
|
|
|
if (!empty($uri->getHost()) && $preserveHost === false) { |
|
101
|
|
|
return $clone->withHeader('host', $uri->getHost()); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if (empty($clone->getHeader('host')) && !empty($uri->getHost()) && $preserveHost === true) { |
|
105
|
|
|
$clone = $clone->withHeader('host', $uri->getHost()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $clone; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|