|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Nyholm\Psr7; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Psr\Http\Message\UriInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Michael Dowling and contributors to guzzlehttp/psr7 |
|
12
|
|
|
* @author Tobias Nyholm <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
trait RequestTrait |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
private $method; |
|
18
|
|
|
|
|
19
|
|
|
/** @var null|string */ |
|
20
|
|
|
private $requestTarget; |
|
21
|
|
|
|
|
22
|
|
|
/** @var null|UriInterface */ |
|
23
|
|
|
private $uri; |
|
24
|
|
|
|
|
25
|
1 |
|
public function getRequestTarget(): string |
|
26
|
|
|
{ |
|
27
|
1 |
|
if (null !== $this->requestTarget) { |
|
28
|
1 |
|
return $this->requestTarget; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
$target = $this->uri->getPath(); |
|
32
|
1 |
|
if ('' === $target) { |
|
33
|
|
|
$target = '/'; |
|
34
|
|
|
} |
|
35
|
1 |
|
if ('' !== $this->uri->getQuery()) { |
|
36
|
|
|
$target .= '?'.$this->uri->getQuery(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
1 |
|
return $target; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
public function withRequestTarget($requestTarget): self |
|
43
|
|
|
{ |
|
44
|
1 |
|
if (preg_match('#\s#', $requestTarget)) { |
|
45
|
|
|
throw new InvalidArgumentException('Invalid request target provided; cannot contain whitespace'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
$new = clone $this; |
|
49
|
1 |
|
$new->requestTarget = $requestTarget; |
|
50
|
|
|
|
|
51
|
1 |
|
return $new; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
26 |
|
public function getMethod(): string |
|
55
|
|
|
{ |
|
56
|
26 |
|
return $this->method; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
4 |
|
public function withMethod($method): self |
|
60
|
|
|
{ |
|
61
|
4 |
|
if (!is_string($method)) { |
|
62
|
3 |
|
throw new \InvalidArgumentException('Method must be a string'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
$new = clone $this; |
|
66
|
1 |
|
$new->method = $method; |
|
67
|
|
|
|
|
68
|
1 |
|
return $new; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
26 |
|
public function getUri(): UriInterface |
|
72
|
|
|
{ |
|
73
|
26 |
|
return $this->uri; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
4 |
|
public function withUri(UriInterface $uri, $preserveHost = false): self |
|
77
|
|
|
{ |
|
78
|
4 |
|
if ($uri === $this->uri) { |
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
4 |
|
$new = clone $this; |
|
83
|
4 |
|
$new->uri = $uri; |
|
84
|
|
|
|
|
85
|
4 |
|
if (!$preserveHost || !$this->hasHeader('Host')) { |
|
|
|
|
|
|
86
|
4 |
|
$new->updateHostFromUri(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
4 |
|
return $new; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
79 |
|
private function updateHostFromUri(): void |
|
93
|
|
|
{ |
|
94
|
79 |
|
$host = $this->uri->getHost(); |
|
95
|
|
|
|
|
96
|
79 |
|
if ('' === $host) { |
|
97
|
50 |
|
return; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
32 |
|
if (null !== ($port = $this->uri->getPort())) { |
|
101
|
|
|
$host .= ':'.$port; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
32 |
|
if (isset($this->headerNames['host'])) { |
|
105
|
|
|
$header = $this->headerNames['host']; |
|
|
|
|
|
|
106
|
|
|
} else { |
|
107
|
32 |
|
$header = 'Host'; |
|
108
|
32 |
|
$this->headerNames['host'] = 'Host'; |
|
109
|
|
|
} |
|
110
|
|
|
// Ensure Host is the first header. |
|
111
|
|
|
// See: http://tools.ietf.org/html/rfc7230#section-5.4 |
|
112
|
32 |
|
$this->headers = [$header => [$host]] + $this->headers; |
|
|
|
|
|
|
113
|
32 |
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.