Open Journal Systems  3.3.0
MessageTrait.php
1 <?php
2 namespace GuzzleHttp\Psr7;
3 
5 
9 trait MessageTrait
10 {
12  private $headers = [];
13 
15  private $headerNames = [];
16 
18  private $protocol = '1.1';
19 
21  private $stream;
22 
23  public function getProtocolVersion()
24  {
25  return $this->protocol;
26  }
27 
28  public function withProtocolVersion($version)
29  {
30  if ($this->protocol === $version) {
31  return $this;
32  }
33 
34  $new = clone $this;
35  $new->protocol = $version;
36  return $new;
37  }
38 
39  public function getHeaders()
40  {
41  return $this->headers;
42  }
43 
44  public function hasHeader($header)
45  {
46  return isset($this->headerNames[strtolower($header)]);
47  }
48 
49  public function getHeader($header)
50  {
51  $header = strtolower($header);
52 
53  if (!isset($this->headerNames[$header])) {
54  return [];
55  }
56 
57  $header = $this->headerNames[$header];
58 
59  return $this->headers[$header];
60  }
61 
62  public function getHeaderLine($header)
63  {
64  return implode(', ', $this->getHeader($header));
65  }
66 
67  public function withHeader($header, $value)
68  {
69  $this->assertHeader($header);
70  $value = $this->normalizeHeaderValue($value);
71  $normalized = strtolower($header);
72 
73  $new = clone $this;
74  if (isset($new->headerNames[$normalized])) {
75  unset($new->headers[$new->headerNames[$normalized]]);
76  }
77  $new->headerNames[$normalized] = $header;
78  $new->headers[$header] = $value;
79 
80  return $new;
81  }
82 
83  public function withAddedHeader($header, $value)
84  {
85  $this->assertHeader($header);
86  $value = $this->normalizeHeaderValue($value);
87  $normalized = strtolower($header);
88 
89  $new = clone $this;
90  if (isset($new->headerNames[$normalized])) {
91  $header = $this->headerNames[$normalized];
92  $new->headers[$header] = array_merge($this->headers[$header], $value);
93  } else {
94  $new->headerNames[$normalized] = $header;
95  $new->headers[$header] = $value;
96  }
97 
98  return $new;
99  }
100 
101  public function withoutHeader($header)
102  {
103  $normalized = strtolower($header);
104 
105  if (!isset($this->headerNames[$normalized])) {
106  return $this;
107  }
108 
109  $header = $this->headerNames[$normalized];
110 
111  $new = clone $this;
112  unset($new->headers[$header], $new->headerNames[$normalized]);
113 
114  return $new;
115  }
116 
117  public function getBody()
118  {
119  if (!$this->stream) {
120  $this->stream = stream_for('');
121  }
122 
123  return $this->stream;
124  }
125 
126  public function withBody(StreamInterface $body)
127  {
128  if ($body === $this->stream) {
129  return $this;
130  }
131 
132  $new = clone $this;
133  $new->stream = $body;
134  return $new;
135  }
136 
137  private function setHeaders(array $headers)
138  {
139  $this->headerNames = $this->headers = [];
140  foreach ($headers as $header => $value) {
141  if (is_int($header)) {
142  // Numeric array keys are converted to int by PHP but having a header name '123' is not forbidden by the spec
143  // and also allowed in withHeader(). So we need to cast it to string again for the following assertion to pass.
144  $header = (string) $header;
145  }
146  $this->assertHeader($header);
147  $value = $this->normalizeHeaderValue($value);
148  $normalized = strtolower($header);
149  if (isset($this->headerNames[$normalized])) {
150  $header = $this->headerNames[$normalized];
151  $this->headers[$header] = array_merge($this->headers[$header], $value);
152  } else {
153  $this->headerNames[$normalized] = $header;
154  $this->headers[$header] = $value;
155  }
156  }
157  }
158 
159  private function normalizeHeaderValue($value)
160  {
161  if (!is_array($value)) {
162  return $this->trimHeaderValues([$value]);
163  }
164 
165  if (count($value) === 0) {
166  throw new \InvalidArgumentException('Header value can not be an empty array.');
167  }
168 
169  return $this->trimHeaderValues($value);
170  }
171 
186  private function trimHeaderValues(array $values)
187  {
188  return array_map(function ($value) {
189  if (!is_scalar($value) && null !== $value) {
190  throw new \InvalidArgumentException(sprintf(
191  'Header value must be scalar or null but %s provided.',
192  is_object($value) ? get_class($value) : gettype($value)
193  ));
194  }
195 
196  return trim((string) $value, " \t");
197  }, $values);
198  }
199 
200  private function assertHeader($header)
201  {
202  if (!is_string($header)) {
203  throw new \InvalidArgumentException(sprintf(
204  'Header name must be a string but %s provided.',
205  is_object($header) ? get_class($header) : gettype($header)
206  ));
207  }
208 
209  if ($header === '') {
210  throw new \InvalidArgumentException('Header name can not be empty.');
211  }
212  }
213 }
GuzzleHttp\Psr7\getHeaderLine
getHeaderLine($header)
Definition: MessageTrait.php:74
GuzzleHttp\Psr7\getHeader
getHeader($header)
Definition: MessageTrait.php:61
GuzzleHttp\Psr7\hasHeader
hasHeader($header)
Definition: MessageTrait.php:56
GuzzleHttp\Psr7\withBody
withBody(StreamInterface $body)
Definition: MessageTrait.php:138
GuzzleHttp\Psr7\withAddedHeader
withAddedHeader($header, $value)
Definition: MessageTrait.php:95
Psr\Http\Message\StreamInterface
Definition: vendor/psr/http-message/src/StreamInterface.php:12
GuzzleHttp\Psr7\getHeaders
getHeaders()
Definition: MessageTrait.php:51
GuzzleHttp\Psr7\MessageTrait
trait MessageTrait
Definition: MessageTrait.php:10
GuzzleHttp\Psr7\withoutHeader
withoutHeader($header)
Definition: MessageTrait.php:113
GuzzleHttp\Psr7\withHeader
withHeader($header, $value)
Definition: MessageTrait.php:79
GuzzleHttp\Psr7
Definition: AppendStream.php:2
Seboettg\Collection\count
count()
Definition: ArrayListTrait.php:253
GuzzleHttp\Psr7\getBody
getBody()
Definition: MessageTrait.php:129
GuzzleHttp\Psr7\withProtocolVersion
withProtocolVersion($version)
Definition: MessageTrait.php:40
GuzzleHttp\Psr7\stream_for
stream_for($resource='', array $options=[])
Definition: guzzlehttp/psr7/src/functions.php:78
GuzzleHttp\Psr7\getProtocolVersion
getProtocolVersion()
Definition: MessageTrait.php:35