1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Igni\Network\Http; |
4
|
|
|
|
5
|
|
|
use Igni\Network\Exception\InvalidArgumentException; |
6
|
|
|
use Psr\Http\Message\RequestInterface; |
7
|
|
|
use Psr\Http\Message\StreamInterface; |
8
|
|
|
use Psr\Http\Message\UriInterface; |
9
|
|
|
use Zend\Diactoros\RequestTrait; |
10
|
|
|
use Zend\Diactoros\Uri; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* PSR-7 implementation of RequestInterface. |
14
|
|
|
* Utilizes zend/diactoros implementation. |
15
|
|
|
* |
16
|
|
|
* @see RequestInterface |
17
|
|
|
* @package Igni\Http |
18
|
|
|
*/ |
19
|
|
|
class Request implements RequestInterface |
20
|
|
|
{ |
21
|
|
|
use RequestTrait; |
22
|
|
|
|
23
|
|
|
public const METHOD_GET = 'GET'; |
24
|
|
|
public const METHOD_HEAD = 'HEAD'; |
25
|
|
|
public const METHOD_POST = 'POST'; |
26
|
|
|
public const METHOD_PUT = 'PUT'; |
27
|
|
|
public const METHOD_DELETE = 'DELETE'; |
28
|
|
|
public const METHOD_CONNECT = 'CONNECT'; |
29
|
|
|
public const METHOD_OPTIONS = 'OPTIONS'; |
30
|
|
|
public const METHOD_PATCH = 'PATCH'; |
31
|
|
|
public const METHOD_TRACE = 'TRACE'; |
32
|
|
|
|
33
|
|
|
/** @var StreamInterface */ |
34
|
|
|
private $stream; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param null|string $uri URI for the request, if any. |
38
|
|
|
* @param string $method HTTP method for the request, if any. |
39
|
|
|
* @param string|resource|StreamInterface $body Output body, if any. |
40
|
|
|
* @param array $headers Headers for the message, if any. |
41
|
|
|
* @throws InvalidArgumentException for any invalid value. |
42
|
|
|
*/ |
43
|
8 |
|
public function __construct(string $uri = null, string $method = self::METHOD_GET, $body = 'php://temp', array $headers = []) |
44
|
|
|
{ |
45
|
8 |
|
$this->validateUri($uri); |
46
|
8 |
|
$this->setHeaders($headers); |
47
|
8 |
|
$this->method = $method; |
48
|
8 |
|
$this->uri = $uri ? new Uri($uri) : new Uri(); |
49
|
8 |
|
$this->stream = Stream::create($body, 'wb+'); |
|
|
|
|
50
|
|
|
|
51
|
8 |
|
$headers['Host'] = $headers['Host'] ?? [$this->getHostFromUri()]; |
52
|
8 |
|
} |
53
|
|
|
|
54
|
8 |
|
private function validateUri($uri) |
55
|
|
|
{ |
56
|
8 |
|
if (!$uri instanceof UriInterface && !is_string($uri) && null !== $uri) { |
|
|
|
|
57
|
|
|
throw new InvalidArgumentException( |
58
|
|
|
'Invalid URI provided; must be null, a string, or a Psr\Http\Message\UriInterface instance' |
59
|
|
|
); |
60
|
|
|
} |
61
|
8 |
|
} |
62
|
|
|
} |
63
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..