|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cakasim\Payone\Sdk\Http\Message; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Message\RequestInterface; |
|
8
|
|
|
use Psr\Http\Message\StreamInterface; |
|
9
|
|
|
use Psr\Http\Message\UriInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* The HTTP request message implementation. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Fabian Böttcher <[email protected]> |
|
15
|
|
|
* @since 0.1.0 |
|
16
|
|
|
*/ |
|
17
|
|
|
class Request extends AbstractMessage implements RequestInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string The request method. |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $method; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var UriInterface The request URI. |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $uri; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string|null The request target or null if no concrete target is set. |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $requestTarget = null; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Constructs an HTTP request. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $method The request method. |
|
38
|
|
|
* @param UriInterface $uri The request URI. |
|
39
|
|
|
* @param string $protocolVersion The HTTP version. |
|
40
|
|
|
* @param StreamInterface $body The body of this request. |
|
41
|
|
|
* @param string[] $headers The headers of this request. |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(string $method, UriInterface $uri, string $protocolVersion, StreamInterface $body, array $headers) |
|
44
|
|
|
{ |
|
45
|
|
|
parent::__construct($protocolVersion, $body, $headers); |
|
46
|
|
|
$this->setMethod($method); |
|
47
|
|
|
$this->setUri($uri); |
|
48
|
|
|
|
|
49
|
|
|
// According to PSR-7 set the Host header from the |
|
50
|
|
|
// provided URI if not already set. |
|
51
|
|
|
if (empty($this->getHeader('Host')) && !empty($host = $uri->getHost())) { |
|
52
|
|
|
$this->setHeader('Host', [$host]); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @inheritDoc |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getRequestTarget() |
|
60
|
|
|
{ |
|
61
|
|
|
if ($this->requestTarget) { |
|
62
|
|
|
return $this->requestTarget; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$path = "{$this->getUri()->getPath()}?{$this->getUri()->getQuery()}"; |
|
66
|
|
|
$path = '/' . ltrim($path, '/'); |
|
67
|
|
|
$path = rtrim($path, '?'); |
|
68
|
|
|
|
|
69
|
|
|
return $path; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Sets the request target. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $requestTarget The request target. |
|
76
|
|
|
* @return $this |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function setRequestTarget(string $requestTarget): self |
|
79
|
|
|
{ |
|
80
|
|
|
$this->requestTarget = $requestTarget; |
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @inheritDoc |
|
86
|
|
|
*/ |
|
87
|
|
|
public function withRequestTarget($requestTarget) |
|
88
|
|
|
{ |
|
89
|
|
|
return (clone $this)->setRequestTarget($requestTarget); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @inheritDoc |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getMethod() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->method; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Sets the request method. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $method The request method. |
|
104
|
|
|
* @return $this |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function setMethod(string $method): self |
|
107
|
|
|
{ |
|
108
|
|
|
$this->method = $method; |
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @inheritDoc |
|
114
|
|
|
*/ |
|
115
|
|
|
public function withMethod($method) |
|
116
|
|
|
{ |
|
117
|
|
|
return (clone $this)->setMethod($method); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @inheritDoc |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getUri() |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->uri; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param UriInterface $uri The URI. |
|
130
|
|
|
* @return $this |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function setUri(UriInterface $uri): self |
|
133
|
|
|
{ |
|
134
|
|
|
$this->uri = $uri; |
|
135
|
|
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @inheritDoc |
|
140
|
|
|
*/ |
|
141
|
|
|
public function withUri(UriInterface $uri, $preserveHost = false) |
|
142
|
|
|
{ |
|
143
|
|
|
$req = clone $this; |
|
144
|
|
|
|
|
145
|
|
|
// According to PSR-7 update the Host header if the host part of |
|
146
|
|
|
// the provided URI is not empty and if $preserveHost is not true |
|
147
|
|
|
// or the current request has no or an empty Host header. |
|
148
|
|
|
if (!empty($host = $uri->getHost()) && ($preserveHost !== true || empty($req->getHeader('Host')))) { |
|
149
|
|
|
$req->setHeader('Host', [$host]); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $req->setUri($uri); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|