1 | <?php |
||
25 | class Request extends Message implements RequestInterface |
||
26 | { |
||
27 | /** @var string */ |
||
28 | protected $method; |
||
29 | /** @var null|UriInterface */ |
||
30 | protected $uri; |
||
31 | /** @var null|string */ |
||
32 | protected $requestTarget; |
||
33 | |||
34 | /** |
||
35 | * @param null|string $method HTTP method for the request. |
||
36 | * @param null|string $uri URI for the request. |
||
37 | * |
||
38 | * @throws InvalidArgumentException for an invalid URI |
||
39 | */ |
||
40 | 9 | public function __construct($method = null, $uri = null) |
|
41 | { |
||
42 | 9 | if (is_string($uri)) { |
|
43 | 6 | $this->uri = new Uri($uri); |
|
44 | 6 | $this->addHostHeader($this->uri); |
|
45 | 9 | } elseif ($uri instanceof UriInterface) { |
|
46 | 3 | $this->uri = $uri; |
|
47 | 3 | $this->addHostHeader($this->uri); |
|
48 | 3 | } |
|
49 | |||
50 | 9 | if (is_string($method)) { |
|
51 | 9 | $this->method = strtoupper($method); |
|
52 | 9 | } |
|
53 | 9 | } |
|
54 | |||
55 | /** |
||
56 | * Build host. |
||
57 | * |
||
58 | * @param UriInterface $uri |
||
59 | */ |
||
60 | 9 | protected function addHostHeader(UriInterface $uri) |
|
61 | { |
||
62 | 9 | $host = $uri->getHost(); |
|
63 | 9 | if ($port = $uri->getPort()) { |
|
64 | 1 | $host = sprintf('%s:%s', $host, $port); |
|
65 | 1 | } |
|
66 | |||
67 | 9 | $this->headers['host'] = array($host); |
|
68 | 9 | } |
|
69 | |||
70 | /** {@inheritdoc} */ |
||
71 | 1 | public function getRequestTarget() |
|
72 | { |
||
73 | 1 | if ($this->requestTarget !== null) { |
|
74 | 1 | return $this->requestTarget; |
|
75 | } |
||
76 | |||
77 | 1 | $target = $this->uri->getPath(); |
|
78 | |||
79 | 1 | if ('' === $target) { |
|
80 | 1 | $target = '/'; |
|
81 | 1 | } |
|
82 | |||
83 | 1 | if ($this->uri->getQuery()) { |
|
84 | 1 | $target .= '?' . $this->uri->getQuery(); |
|
85 | 1 | } |
|
86 | |||
87 | 1 | return $target; |
|
88 | } |
||
89 | |||
90 | /** {@inheritdoc} */ |
||
91 | 1 | public function withRequestTarget($requestTarget) |
|
102 | |||
103 | /** {@inheritdoc} */ |
||
104 | 3 | public function getMethod() |
|
108 | |||
109 | /** {@inheritdoc} */ |
||
110 | 2 | public function withMethod($method) |
|
116 | |||
117 | /** {@inheritdoc} */ |
||
118 | 3 | public function getUri() |
|
119 | { |
||
120 | 3 | return $this->uri; |
|
121 | } |
||
122 | |||
123 | /** {@inheritdoc} */ |
||
124 | 2 | public function withUri(UriInterface $uri, $preserveHost = false) |
|
136 | } |
||
137 |