1 | <?php |
||||
2 | |||||
3 | namespace DMT\Aura\Psr\Message; |
||||
4 | |||||
5 | use Aura\Web\Request as AuraRequest; |
||||
6 | use Aura\Web\WebFactory; |
||||
7 | use DMT\Aura\Psr\Factory\StreamFactory; |
||||
8 | use DMT\Aura\Psr\Factory\UriFactory; |
||||
9 | use InvalidArgumentException; |
||||
10 | use Psr\Http\Message\RequestInterface; |
||||
11 | use Psr\Http\Message\UriInterface; |
||||
12 | |||||
13 | /** |
||||
14 | * Class Request |
||||
15 | * |
||||
16 | * @package DMT\Aura\Psr\Message |
||||
17 | */ |
||||
18 | class Request implements RequestInterface |
||||
19 | { |
||||
20 | use MessageTrait; |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
21 | |||||
22 | /** @var UriInterface $uri */ |
||||
23 | protected $uri; |
||||
24 | |||||
25 | /** |
||||
26 | * Request constructor. |
||||
27 | * |
||||
28 | * @param string $method The HTTP method associated with the request. |
||||
29 | * @param UriInterface|string $uri The URI associated with the request. |
||||
30 | */ |
||||
31 | 54 | public function __construct(string $method, $uri) |
|||
32 | { |
||||
33 | 54 | $request = $this->getInnerObject(); |
|||
34 | 54 | $this->setObjectProperty($request->method , 'value', $method); |
|||
35 | |||||
36 | 54 | $this->uri = (new UriFactory())->createUri($uri); |
|||
37 | 54 | $this->body = (new StreamFactory())->createStream(); |
|||
38 | 54 | } |
|||
39 | |||||
40 | /** |
||||
41 | * @return AuraRequest |
||||
42 | */ |
||||
43 | 54 | public function getInnerObject(): AuraRequest |
|||
44 | { |
||||
45 | 54 | if (!$this->object) { |
|||
46 | 54 | $this->object = (new WebFactory([]))->newRequest(); |
|||
47 | } |
||||
48 | |||||
49 | 54 | $this->setObjectProperty( |
|||
50 | 54 | $this->object , |
|||
51 | 54 | 'client', |
|||
52 | 54 | new AuraRequest\Client($this->object->server->get()) |
|||
53 | ); |
||||
54 | |||||
55 | 54 | return $this->object; |
|||
0 ignored issues
–
show
|
|||||
56 | } |
||||
57 | |||||
58 | /** |
||||
59 | * Retrieves the message's request target. |
||||
60 | * |
||||
61 | * @return string |
||||
62 | */ |
||||
63 | 1 | public function getRequestTarget(): string |
|||
64 | { |
||||
65 | 1 | return $this->getInnerObject()->server->get('REQUEST_URI', '/'); |
|||
0 ignored issues
–
show
|
|||||
66 | } |
||||
67 | |||||
68 | /** |
||||
69 | * Return an instance with the specific request-target. |
||||
70 | * |
||||
71 | * @param mixed $requestTarget |
||||
72 | * @return static |
||||
73 | */ |
||||
74 | 1 | public function withRequestTarget($requestTarget): self |
|||
75 | { |
||||
76 | 1 | $instance = clone($this); |
|||
77 | |||||
78 | 1 | $server = $instance->getInnerObject()->server; |
|||
79 | 1 | $server['REQUEST_URI'] = $requestTarget; |
|||
80 | |||||
81 | 1 | return $instance; |
|||
82 | } |
||||
83 | |||||
84 | /** |
||||
85 | * Retrieves the HTTP method of the request. |
||||
86 | * |
||||
87 | * @return string Returns the request method. |
||||
88 | */ |
||||
89 | 2 | public function getMethod(): string |
|||
90 | { |
||||
91 | 2 | return $this->getInnerObject()->method->get() ?: 'GET'; |
|||
92 | } |
||||
93 | |||||
94 | /** |
||||
95 | * Return an instance with the provided HTTP method. |
||||
96 | * |
||||
97 | * @param string $method Case-sensitive method. |
||||
98 | * @return static |
||||
99 | * @throws InvalidArgumentException for invalid HTTP methods. |
||||
100 | */ |
||||
101 | 7 | public function withMethod($method): self |
|||
102 | { |
||||
103 | 7 | if (!is_string($method)) { |
|||
0 ignored issues
–
show
|
|||||
104 | 6 | throw new InvalidArgumentException('Invalid method given'); |
|||
105 | } |
||||
106 | |||||
107 | 1 | $instance = clone($this); |
|||
108 | 1 | $instance->setObjectProperty($instance->getInnerObject()->method, 'value', $method); |
|||
109 | |||||
110 | 1 | return $instance; |
|||
111 | } |
||||
112 | |||||
113 | /** |
||||
114 | * Retrieves the URI instance. |
||||
115 | * |
||||
116 | * @return UriInterface |
||||
117 | */ |
||||
118 | 2 | public function getUri(): UriInterface |
|||
119 | { |
||||
120 | 2 | if (!$this->uri instanceof Uri) { |
|||
121 | $this->uri = new Uri($this->getInnerObject()->url); |
||||
0 ignored issues
–
show
$this->getInnerObject()->url of type Aura\Web\Request\Url is incompatible with the type null|string expected by parameter $uri of DMT\Aura\Psr\Message\Uri::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
122 | } |
||||
123 | |||||
124 | 2 | return $this->uri; |
|||
125 | } |
||||
126 | |||||
127 | /** |
||||
128 | * Returns an instance with the provided URI. |
||||
129 | * |
||||
130 | * @param UriInterface $uri |
||||
131 | * @param bool $preserveHost |
||||
132 | * @return static |
||||
133 | */ |
||||
134 | 4 | public function withUri(UriInterface $uri, $preserveHost = false): self |
|||
135 | { |
||||
136 | 4 | if (!$uri instanceof Uri) { |
|||
137 | $uri = (new UriFactory())->createUri((string)$uri); |
||||
138 | } |
||||
139 | |||||
140 | 4 | $instance = clone($this); |
|||
141 | 4 | $instance->uri = $uri; |
|||
142 | 4 | $instance->setObjectProperty($instance->getInnerObject(), 'url', $uri->getInnerObject()); |
|||
143 | |||||
144 | 4 | if (!$preserveHost || !$this->hasHeader('host')) { |
|||
145 | 4 | return $instance->updateHostFromUri(); |
|||
146 | } |
||||
147 | |||||
148 | 1 | return $instance; |
|||
149 | } |
||||
150 | |||||
151 | /** |
||||
152 | * Update the host whilst setting a new URI. |
||||
153 | * |
||||
154 | * @return static |
||||
155 | */ |
||||
156 | 4 | protected function updateHostFromUri(): self |
|||
157 | { |
||||
158 | 4 | $host = $this->uri->getHost(); |
|||
159 | 4 | $port = $this->uri->getPort(); |
|||
160 | |||||
161 | 4 | if ($host === '') { |
|||
162 | 2 | return $this; |
|||
163 | } |
||||
164 | |||||
165 | 3 | if ($port !== null) { |
|||
166 | $host .= ':' . $port; |
||||
167 | } |
||||
168 | |||||
169 | 3 | return $this->withHeader('host', $host); |
|||
170 | } |
||||
171 | } |
||||
172 |