comparison vendor/symfony/http-foundation/HeaderBag.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
20 { 20 {
21 protected $headers = array(); 21 protected $headers = array();
22 protected $cacheControl = array(); 22 protected $cacheControl = array();
23 23
24 /** 24 /**
25 * Constructor.
26 *
27 * @param array $headers An array of HTTP headers 25 * @param array $headers An array of HTTP headers
28 */ 26 */
29 public function __construct(array $headers = array()) 27 public function __construct(array $headers = array())
30 { 28 {
31 foreach ($headers as $key => $values) { 29 foreach ($headers as $key => $values) {
38 * 36 *
39 * @return string The headers 37 * @return string The headers
40 */ 38 */
41 public function __toString() 39 public function __toString()
42 { 40 {
43 if (!$this->headers) { 41 if (!$headers = $this->all()) {
44 return ''; 42 return '';
45 } 43 }
46 44
47 $max = max(array_map('strlen', array_keys($this->headers))) + 1; 45 ksort($headers);
46 $max = max(array_map('strlen', array_keys($headers))) + 1;
48 $content = ''; 47 $content = '';
49 ksort($this->headers); 48 foreach ($headers as $name => $values) {
50 foreach ($this->headers as $name => $values) {
51 $name = implode('-', array_map('ucfirst', explode('-', $name))); 49 $name = implode('-', array_map('ucfirst', explode('-', $name)));
52 foreach ($values as $value) { 50 foreach ($values as $value) {
53 $content .= sprintf("%-{$max}s %s\r\n", $name.':', $value); 51 $content .= sprintf("%-{$max}s %s\r\n", $name.':', $value);
54 } 52 }
55 } 53 }
72 * 70 *
73 * @return array An array of parameter keys 71 * @return array An array of parameter keys
74 */ 72 */
75 public function keys() 73 public function keys()
76 { 74 {
77 return array_keys($this->headers); 75 return array_keys($this->all());
78 } 76 }
79 77
80 /** 78 /**
81 * Replaces the current HTTP headers by a new set. 79 * Replaces the current HTTP headers by a new set.
82 * 80 *
101 } 99 }
102 100
103 /** 101 /**
104 * Returns a header value by name. 102 * Returns a header value by name.
105 * 103 *
106 * @param string $key The header name 104 * @param string $key The header name
107 * @param mixed $default The default value 105 * @param string|string[] $default The default value
108 * @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
109 * 107 *
110 * @return string|array The first header value if $first is true, an array of values otherwise 108 * @return string|string[] The first header value or default value if $first is true, an array of values otherwise
111 */ 109 */
112 public function get($key, $default = null, $first = true) 110 public function get($key, $default = null, $first = true)
113 { 111 {
114 $key = str_replace('_', '-', strtolower($key)); 112 $key = str_replace('_', '-', strtolower($key));
115 113 $headers = $this->all();
116 if (!array_key_exists($key, $this->headers)) { 114
115 if (!array_key_exists($key, $headers)) {
117 if (null === $default) { 116 if (null === $default) {
118 return $first ? null : array(); 117 return $first ? null : array();
119 } 118 }
120 119
121 return $first ? $default : array($default); 120 return $first ? $default : array($default);
122 } 121 }
123 122
124 if ($first) { 123 if ($first) {
125 return count($this->headers[$key]) ? $this->headers[$key][0] : $default; 124 return \count($headers[$key]) ? $headers[$key][0] : $default;
126 } 125 }
127 126
128 return $this->headers[$key]; 127 return $headers[$key];
129 } 128 }
130 129
131 /** 130 /**
132 * Sets a header by name. 131 * Sets a header by name.
133 * 132 *
134 * @param string $key The key 133 * @param string $key The key
135 * @param string|array $values The value or an array of values 134 * @param string|string[] $values The value or an array of values
136 * @param bool $replace Whether to replace the actual value or not (true by default) 135 * @param bool $replace Whether to replace the actual value or not (true by default)
137 */ 136 */
138 public function set($key, $values, $replace = true) 137 public function set($key, $values, $replace = true)
139 { 138 {
140 $key = str_replace('_', '-', strtolower($key)); 139 $key = str_replace('_', '-', strtolower($key));
141 140
142 $values = array_values((array) $values); 141 if (\is_array($values)) {
143 142 $values = array_values($values);
144 if (true === $replace || !isset($this->headers[$key])) { 143
145 $this->headers[$key] = $values; 144 if (true === $replace || !isset($this->headers[$key])) {
145 $this->headers[$key] = $values;
146 } else {
147 $this->headers[$key] = array_merge($this->headers[$key], $values);
148 }
146 } else { 149 } else {
147 $this->headers[$key] = array_merge($this->headers[$key], $values); 150 if (true === $replace || !isset($this->headers[$key])) {
151 $this->headers[$key] = array($values);
152 } else {
153 $this->headers[$key][] = $values;
154 }
148 } 155 }
149 156
150 if ('cache-control' === $key) { 157 if ('cache-control' === $key) {
151 $this->cacheControl = $this->parseCacheControl($values[0]); 158 $this->cacheControl = $this->parseCacheControl(implode(', ', $this->headers[$key]));
152 } 159 }
153 } 160 }
154 161
155 /** 162 /**
156 * Returns true if the HTTP header is defined. 163 * Returns true if the HTTP header is defined.
159 * 166 *
160 * @return bool true if the parameter exists, false otherwise 167 * @return bool true if the parameter exists, false otherwise
161 */ 168 */
162 public function has($key) 169 public function has($key)
163 { 170 {
164 return array_key_exists(str_replace('_', '-', strtolower($key)), $this->headers); 171 return array_key_exists(str_replace('_', '-', strtolower($key)), $this->all());
165 } 172 }
166 173
167 /** 174 /**
168 * Returns true if the given HTTP header contains the given value. 175 * Returns true if the given HTTP header contains the given value.
169 * 176 *