comparison vendor/symfony/http-foundation/HeaderBag.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
16 * 16 *
17 * @author Fabien Potencier <fabien@symfony.com> 17 * @author Fabien Potencier <fabien@symfony.com>
18 */ 18 */
19 class HeaderBag implements \IteratorAggregate, \Countable 19 class HeaderBag implements \IteratorAggregate, \Countable
20 { 20 {
21 protected $headers = array(); 21 protected $headers = [];
22 protected $cacheControl = array(); 22 protected $cacheControl = [];
23 23
24 /** 24 /**
25 * @param array $headers An array of HTTP headers 25 * @param array $headers An array of HTTP headers
26 */ 26 */
27 public function __construct(array $headers = array()) 27 public function __construct(array $headers = [])
28 { 28 {
29 foreach ($headers as $key => $values) { 29 foreach ($headers as $key => $values) {
30 $this->set($key, $values); 30 $this->set($key, $values);
31 } 31 }
32 } 32 }
78 /** 78 /**
79 * Replaces the current HTTP headers by a new set. 79 * Replaces the current HTTP headers by a new set.
80 * 80 *
81 * @param array $headers An array of HTTP headers 81 * @param array $headers An array of HTTP headers
82 */ 82 */
83 public function replace(array $headers = array()) 83 public function replace(array $headers = [])
84 { 84 {
85 $this->headers = array(); 85 $this->headers = [];
86 $this->add($headers); 86 $this->add($headers);
87 } 87 }
88 88
89 /** 89 /**
90 * Adds new headers the current HTTP headers set. 90 * Adds new headers the current HTTP headers set.
99 } 99 }
100 100
101 /** 101 /**
102 * Returns a header value by name. 102 * Returns a header value by name.
103 * 103 *
104 * @param string $key The header name 104 * @param string $key The header name
105 * @param string|string[] $default The default value 105 * @param string|null $default The default value
106 * @param bool $first Whether to return the first value or all header values 106 * @param bool $first Whether to return the first value or all header values
107 * 107 *
108 * @return string|string[] The first header value or default value if $first is true, an array of values otherwise 108 * @return string|string[]|null The first header value or default value if $first is true, an array of values otherwise
109 */ 109 */
110 public function get($key, $default = null, $first = true) 110 public function get($key, $default = null, $first = true)
111 { 111 {
112 $key = str_replace('_', '-', strtolower($key)); 112 $key = str_replace('_', '-', strtolower($key));
113 $headers = $this->all(); 113 $headers = $this->all();
114 114
115 if (!array_key_exists($key, $headers)) { 115 if (!array_key_exists($key, $headers)) {
116 if (null === $default) { 116 if (null === $default) {
117 return $first ? null : array(); 117 return $first ? null : [];
118 } 118 }
119 119
120 return $first ? $default : array($default); 120 return $first ? $default : [$default];
121 } 121 }
122 122
123 if ($first) { 123 if ($first) {
124 return \count($headers[$key]) ? $headers[$key][0] : $default; 124 return \count($headers[$key]) ? $headers[$key][0] : $default;
125 } 125 }
146 } else { 146 } else {
147 $this->headers[$key] = array_merge($this->headers[$key], $values); 147 $this->headers[$key] = array_merge($this->headers[$key], $values);
148 } 148 }
149 } else { 149 } else {
150 if (true === $replace || !isset($this->headers[$key])) { 150 if (true === $replace || !isset($this->headers[$key])) {
151 $this->headers[$key] = array($values); 151 $this->headers[$key] = [$values];
152 } else { 152 } else {
153 $this->headers[$key][] = $values; 153 $this->headers[$key][] = $values;
154 } 154 }
155 } 155 }
156 156
179 * 179 *
180 * @return bool true if the value is contained in the header, false otherwise 180 * @return bool true if the value is contained in the header, false otherwise
181 */ 181 */
182 public function contains($key, $value) 182 public function contains($key, $value)
183 { 183 {
184 return in_array($value, $this->get($key, null, false)); 184 return \in_array($value, $this->get($key, null, false));
185 } 185 }
186 186
187 /** 187 /**
188 * Removes a header. 188 * Removes a header.
189 * 189 *
194 $key = str_replace('_', '-', strtolower($key)); 194 $key = str_replace('_', '-', strtolower($key));
195 195
196 unset($this->headers[$key]); 196 unset($this->headers[$key]);
197 197
198 if ('cache-control' === $key) { 198 if ('cache-control' === $key) {
199 $this->cacheControl = array(); 199 $this->cacheControl = [];
200 } 200 }
201 } 201 }
202 202
203 /** 203 /**
204 * Returns the HTTP header value converted to a date. 204 * Returns the HTTP header value converted to a date.
205 * 205 *
206 * @param string $key The parameter key 206 * @param string $key The parameter key
207 * @param \DateTime $default The default value 207 * @param \DateTime $default The default value
208 * 208 *
209 * @return null|\DateTime The parsed DateTime or the default value if the header does not exist 209 * @return \DateTime|null The parsed DateTime or the default value if the header does not exist
210 * 210 *
211 * @throws \RuntimeException When the HTTP header is not parseable 211 * @throws \RuntimeException When the HTTP header is not parseable
212 */ 212 */
213 public function getDate($key, \DateTime $default = null) 213 public function getDate($key, \DateTime $default = null)
214 { 214 {
287 * 287 *
288 * @return int The number of headers 288 * @return int The number of headers
289 */ 289 */
290 public function count() 290 public function count()
291 { 291 {
292 return count($this->headers); 292 return \count($this->headers);
293 } 293 }
294 294
295 protected function getCacheControlHeader() 295 protected function getCacheControlHeader()
296 { 296 {
297 $parts = array(); 297 $parts = [];
298 ksort($this->cacheControl); 298 ksort($this->cacheControl);
299 foreach ($this->cacheControl as $key => $value) { 299 foreach ($this->cacheControl as $key => $value) {
300 if (true === $value) { 300 if (true === $value) {
301 $parts[] = $key; 301 $parts[] = $key;
302 } else { 302 } else {
318 * 318 *
319 * @return array An array representing the attribute values 319 * @return array An array representing the attribute values
320 */ 320 */
321 protected function parseCacheControl($header) 321 protected function parseCacheControl($header)
322 { 322 {
323 $cacheControl = array(); 323 $cacheControl = [];
324 preg_match_all('#([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?#', $header, $matches, PREG_SET_ORDER); 324 preg_match_all('#([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?#', $header, $matches, PREG_SET_ORDER);
325 foreach ($matches as $match) { 325 foreach ($matches as $match) {
326 $cacheControl[strtolower($match[1])] = isset($match[3]) ? $match[3] : (isset($match[2]) ? $match[2] : true); 326 $cacheControl[strtolower($match[1])] = isset($match[3]) ? $match[3] : (isset($match[2]) ? $match[2] : true);
327 } 327 }
328 328