Mercurial > hg > isophonics-drupal-site
comparison vendor/guzzlehttp/psr7/src/MessageTrait.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 namespace GuzzleHttp\Psr7; | |
3 | |
4 use Psr\Http\Message\StreamInterface; | |
5 | |
6 /** | |
7 * Trait implementing functionality common to requests and responses. | |
8 */ | |
9 trait MessageTrait | |
10 { | |
11 /** @var array Map of all registered headers, as original name => array of values */ | |
12 private $headers = []; | |
13 | |
14 /** @var array Map of lowercase header name => original name at registration */ | |
15 private $headerNames = []; | |
16 | |
17 /** @var string */ | |
18 private $protocol = '1.1'; | |
19 | |
20 /** @var StreamInterface */ | |
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 | |
163 /** | |
164 * Trims whitespace from the header values. | |
165 * | |
166 * Spaces and tabs ought to be excluded by parsers when extracting the field value from a header field. | |
167 * | |
168 * header-field = field-name ":" OWS field-value OWS | |
169 * OWS = *( SP / HTAB ) | |
170 * | |
171 * @param string[] $values Header values | |
172 * | |
173 * @return string[] Trimmed header values | |
174 * | |
175 * @see https://tools.ietf.org/html/rfc7230#section-3.2.4 | |
176 */ | |
177 private function trimHeaderValues(array $values) | |
178 { | |
179 return array_map(function ($value) { | |
180 return trim($value, " \t"); | |
181 }, $values); | |
182 } | |
183 } |