ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
MessageTrait.php
Go to the documentation of this file.
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  if (!is_array($value)) {
70  $value = [$value];
71  }
72 
73  $value = $this->trimHeaderValues($value);
74  $normalized = strtolower($header);
75 
76  $new = clone $this;
77  if (isset($new->headerNames[$normalized])) {
78  unset($new->headers[$new->headerNames[$normalized]]);
79  }
80  $new->headerNames[$normalized] = $header;
81  $new->headers[$header] = $value;
82 
83  return $new;
84  }
85 
86  public function withAddedHeader($header, $value)
87  {
88  if (!is_array($value)) {
89  $value = [$value];
90  }
91 
92  $value = $this->trimHeaderValues($value);
93  $normalized = strtolower($header);
94 
95  $new = clone $this;
96  if (isset($new->headerNames[$normalized])) {
97  $header = $this->headerNames[$normalized];
98  $new->headers[$header] = array_merge($this->headers[$header], $value);
99  } else {
100  $new->headerNames[$normalized] = $header;
101  $new->headers[$header] = $value;
102  }
103 
104  return $new;
105  }
106 
107  public function withoutHeader($header)
108  {
109  $normalized = strtolower($header);
110 
111  if (!isset($this->headerNames[$normalized])) {
112  return $this;
113  }
114 
115  $header = $this->headerNames[$normalized];
116 
117  $new = clone $this;
118  unset($new->headers[$header], $new->headerNames[$normalized]);
119 
120  return $new;
121  }
122 
123  public function getBody()
124  {
125  if (!$this->stream) {
126  $this->stream = stream_for('');
127  }
128 
129  return $this->stream;
130  }
131 
132  public function withBody(StreamInterface $body)
133  {
134  if ($body === $this->stream) {
135  return $this;
136  }
137 
138  $new = clone $this;
139  $new->stream = $body;
140  return $new;
141  }
142 
143  private function setHeaders(array $headers)
144  {
145  $this->headerNames = $this->headers = [];
146  foreach ($headers as $header => $value) {
147  if (!is_array($value)) {
148  $value = [$value];
149  }
150 
151  $value = $this->trimHeaderValues($value);
152  $normalized = strtolower($header);
153  if (isset($this->headerNames[$normalized])) {
154  $header = $this->headerNames[$normalized];
155  $this->headers[$header] = array_merge($this->headers[$header], $value);
156  } else {
157  $this->headerNames[$normalized] = $header;
158  $this->headers[$header] = $value;
159  }
160  }
161  }
162 
177  private function trimHeaderValues(array $values)
178  {
179  return array_map(function ($value) {
180  return trim($value, " \t");
181  }, $values);
182  }
183 }
getHeader($header)
withoutHeader($header)
setHeaders(array $headers)
withHeader($header, $value)
withAddedHeader($header, $value)
$stream
PHP stream implementation.
stream_for($resource='', array $options=[])
Create a new stream based on the input type.
Definition: functions.php:78
$version
Definition: build.php:27
getHeaderLine($header)
withBody(StreamInterface $body)
$values
withProtocolVersion($version)
trimHeaderValues(array $values)
Trims whitespace from the header values.
hasHeader($header)
Describes a data stream.