Open Journal Systems  3.3.0
vendor/guzzlehttp/psr7/src/Request.php
1 <?php
2 namespace GuzzleHttp\Psr7;
3 
4 use InvalidArgumentException;
8 
12 class Request implements RequestInterface
13 {
14  use MessageTrait;
15 
17  private $method;
18 
20  private $requestTarget;
21 
23  private $uri;
24 
32  public function __construct(
33  $method,
34  $uri,
35  array $headers = [],
36  $body = null,
37  $version = '1.1'
38  ) {
39  $this->assertMethod($method);
40  if (!($uri instanceof UriInterface)) {
41  $uri = new Uri($uri);
42  }
43 
44  $this->method = strtoupper($method);
45  $this->uri = $uri;
46  $this->setHeaders($headers);
47  $this->protocol = $version;
48 
49  if (!isset($this->headerNames['host'])) {
50  $this->updateHostFromUri();
51  }
52 
53  if ($body !== '' && $body !== null) {
54  $this->stream = stream_for($body);
55  }
56  }
57 
58  public function getRequestTarget()
59  {
60  if ($this->requestTarget !== null) {
61  return $this->requestTarget;
62  }
63 
64  $target = $this->uri->getPath();
65  if ($target == '') {
66  $target = '/';
67  }
68  if ($this->uri->getQuery() != '') {
69  $target .= '?' . $this->uri->getQuery();
70  }
71 
72  return $target;
73  }
74 
75  public function withRequestTarget($requestTarget)
76  {
77  if (preg_match('#\s#', $requestTarget)) {
78  throw new InvalidArgumentException(
79  'Invalid request target provided; cannot contain whitespace'
80  );
81  }
82 
83  $new = clone $this;
84  $new->requestTarget = $requestTarget;
85  return $new;
86  }
87 
88  public function getMethod()
89  {
90  return $this->method;
91  }
92 
93  public function withMethod($method)
94  {
95  $this->assertMethod($method);
96  $new = clone $this;
97  $new->method = strtoupper($method);
98  return $new;
99  }
100 
101  public function getUri()
102  {
103  return $this->uri;
104  }
105 
106  public function withUri(UriInterface $uri, $preserveHost = false)
107  {
108  if ($uri === $this->uri) {
109  return $this;
110  }
111 
112  $new = clone $this;
113  $new->uri = $uri;
114 
115  if (!$preserveHost || !isset($this->headerNames['host'])) {
116  $new->updateHostFromUri();
117  }
118 
119  return $new;
120  }
121 
122  private function updateHostFromUri()
123  {
124  $host = $this->uri->getHost();
125 
126  if ($host == '') {
127  return;
128  }
129 
130  if (($port = $this->uri->getPort()) !== null) {
131  $host .= ':' . $port;
132  }
133 
134  if (isset($this->headerNames['host'])) {
135  $header = $this->headerNames['host'];
136  } else {
137  $header = 'Host';
138  $this->headerNames['host'] = 'Host';
139  }
140  // Ensure Host is the first header.
141  // See: http://tools.ietf.org/html/rfc7230#section-5.4
142  $this->headers = [$header => [$host]] + $this->headers;
143  }
144 
145  private function assertMethod($method)
146  {
147  if (!is_string($method) || $method === '') {
148  throw new \InvalidArgumentException('Method must be a non-empty string.');
149  }
150  }
151 }
Psr\Http\Message\StreamInterface
Definition: vendor/psr/http-message/src/StreamInterface.php:12
Psr\Http\Message\RequestInterface
Definition: vendor/psr/http-message/src/RequestInterface.php:24
GuzzleHttp\Psr7\Request\getUri
getUri()
Definition: vendor/guzzlehttp/psr7/src/Request.php:110
GuzzleHttp\Psr7\Request\__construct
__construct( $method, $uri, array $headers=[], $body=null, $version='1.1')
Definition: vendor/guzzlehttp/psr7/src/Request.php:41
GuzzleHttp\Psr7\Request\withUri
withUri(UriInterface $uri, $preserveHost=false)
Definition: vendor/guzzlehttp/psr7/src/Request.php:115
GuzzleHttp\Psr7\MessageTrait
trait MessageTrait
Definition: MessageTrait.php:10
GuzzleHttp\Psr7\Uri
Definition: Uri.php:13
GuzzleHttp\Psr7\Request\withRequestTarget
withRequestTarget($requestTarget)
Definition: vendor/guzzlehttp/psr7/src/Request.php:84
GuzzleHttp\Psr7\Request\getRequestTarget
getRequestTarget()
Definition: vendor/guzzlehttp/psr7/src/Request.php:67
GuzzleHttp\Psr7\Request\getMethod
getMethod()
Definition: vendor/guzzlehttp/psr7/src/Request.php:97
GuzzleHttp\Psr7
Definition: AppendStream.php:2
Psr\Http\Message\UriInterface
Definition: UriInterface.php:24
GuzzleHttp\Psr7\Request
Definition: vendor/guzzlehttp/psr7/src/Request.php:12
GuzzleHttp\Psr7\Request\withMethod
withMethod($method)
Definition: vendor/guzzlehttp/psr7/src/Request.php:102
GuzzleHttp\Psr7\stream_for
stream_for($resource='', array $options=[])
Definition: guzzlehttp/psr7/src/functions.php:78
GuzzleHttp\Exception\InvalidArgumentException
Definition: vendor/guzzlehttp/guzzle/src/Exception/InvalidArgumentException.php:5