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 |
|
if ('' === $target = $this->uri->getPath()) { |
32
|
|
|
$target = '/'; |
33
|
|
|
} |
34
|
1 |
|
if ('' !== $this->uri->getQuery()) { |
35
|
|
|
$target .= '?'.$this->uri->getQuery(); |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
return $target; |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
public function withRequestTarget($requestTarget): self |
42
|
|
|
{ |
43
|
1 |
|
if (preg_match('#\s#', $requestTarget)) { |
44
|
|
|
throw new InvalidArgumentException('Invalid request target provided; cannot contain whitespace'); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
$new = clone $this; |
48
|
1 |
|
$new->requestTarget = $requestTarget; |
49
|
|
|
|
50
|
1 |
|
return $new; |
51
|
|
|
} |
52
|
|
|
|
53
|
26 |
|
public function getMethod(): string |
54
|
|
|
{ |
55
|
26 |
|
return $this->method; |
56
|
|
|
} |
57
|
|
|
|
58
|
4 |
|
public function withMethod($method): self |
59
|
|
|
{ |
60
|
4 |
|
if (!is_string($method)) { |
61
|
3 |
|
throw new \InvalidArgumentException('Method must be a string'); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
$new = clone $this; |
65
|
1 |
|
$new->method = $method; |
66
|
|
|
|
67
|
1 |
|
return $new; |
68
|
|
|
} |
69
|
|
|
|
70
|
26 |
|
public function getUri(): UriInterface |
71
|
|
|
{ |
72
|
26 |
|
return $this->uri; |
73
|
|
|
} |
74
|
|
|
|
75
|
4 |
|
public function withUri(UriInterface $uri, $preserveHost = false): self |
76
|
|
|
{ |
77
|
4 |
|
if ($uri === $this->uri) { |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
4 |
|
$new = clone $this; |
82
|
4 |
|
$new->uri = $uri; |
83
|
|
|
|
84
|
4 |
|
if (!$preserveHost || !$this->hasHeader('Host')) { |
|
|
|
|
85
|
4 |
|
$new->updateHostFromUri(); |
86
|
|
|
} |
87
|
|
|
|
88
|
4 |
|
return $new; |
89
|
|
|
} |
90
|
|
|
|
91
|
79 |
|
private function updateHostFromUri(): void |
92
|
|
|
{ |
93
|
79 |
|
if ('' === $host = $this->uri->getHost()) { |
94
|
50 |
|
return; |
95
|
|
|
} |
96
|
|
|
|
97
|
32 |
|
if (null !== ($port = $this->uri->getPort())) { |
98
|
|
|
$host .= ':'.$port; |
99
|
|
|
} |
100
|
|
|
|
101
|
32 |
|
if (isset($this->headerNames['host'])) { |
102
|
|
|
$header = $this->headerNames['host']; |
|
|
|
|
103
|
|
|
} else { |
104
|
32 |
|
$header = 'Host'; |
105
|
32 |
|
$this->headerNames['host'] = 'Host'; |
106
|
|
|
} |
107
|
|
|
// Ensure Host is the first header. |
108
|
|
|
// See: http://tools.ietf.org/html/rfc7230#section-5.4 |
109
|
32 |
|
$this->headers = [$header => [$host]] + $this->headers; |
|
|
|
|
110
|
32 |
|
} |
111
|
|
|
} |
112
|
|
|
|
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
Idable
provides a methodequalsId
that 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.