| @@ -14,8 +14,10 @@ | ||
| 14 | 14 | /** |
| 15 | 15 | * HeaderBag is a container for HTTP headers. |
| 16 | 16 | * |
| 17 | 17 | * @author Fabien Potencier <[email protected]> |
| 18 | + * | |
| 19 | + * @implements \IteratorAggregate<string, list<string|null>> | |
| 18 | 20 | */ |
| 19 | 21 | class HeaderBag implements \IteratorAggregate, \Countable |
| 20 | 22 | { |
| 21 | 23 | protected const UPPER = '_ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| @@ -20,8 +22,11 @@ | ||
| 20 | 22 | { |
| 21 | 23 | protected const UPPER = '_ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 22 | 24 | protected const LOWER = '-abcdefghijklmnopqrstuvwxyz'; |
| 23 | 25 | |
| 26 | + /** | |
| 27 | + * @var array<string, list<string|null>> | |
| 28 | + */ | |
| 24 | 29 | protected $headers = []; |
| 25 | 30 | protected $cacheControl = []; |
| 26 | 31 | |
| 27 | 32 | public function __construct(array $headers = []) |
| @@ -33,9 +38,9 @@ | ||
| 33 | 38 | |
| 34 | 39 | /** |
| 35 | 40 | * Returns the headers as a string. |
| 36 | 41 | * |
| 37 | - * @return string The headers | |
| 42 | + * @return string | |
| 38 | 43 | */ |
| 39 | 44 | public function __toString() |
| 40 | 45 | { |
| 41 | 46 | if (!$headers = $this->all()) { |
| @@ -59,13 +64,13 @@ | ||
| 59 | 64 | * Returns the headers. |
| 60 | 65 | * |
| 61 | 66 | * @param string|null $key The name of the headers to return or null to get them all |
| 62 | 67 | * |
| 63 | - * @return array An array of headers | |
| 68 | + * @return array<string, array<int, string|null>>|array<int, string|null> | |
| 64 | 69 | */ |
| 65 | - public function all(/* string $key = null */) | |
| 70 | + public function all(?string $key = null) | |
| 66 | 71 | { |
| 67 | - if (1 <= \func_num_args() && null !== $key = func_get_arg(0)) { | |
| 72 | + if (null !== $key) { | |
| 68 | 73 | return $this->headers[strtr($key, self::UPPER, self::LOWER)] ?? []; |
| 69 | 74 | } |
| 70 | 75 | |
| 71 | 76 | return $this->headers; |
| @@ -73,9 +78,9 @@ | ||
| 73 | 78 | |
| 74 | 79 | /** |
| 75 | 80 | * Returns the parameter keys. |
| 76 | 81 | * |
| 77 | - * @return array An array of parameter keys | |
| 82 | + * @return string[] | |
| 78 | 83 | */ |
| 79 | 84 | public function keys() |
| 80 | 85 | { |
| 81 | 86 | return array_keys($this->all()); |
| @@ -100,26 +105,16 @@ | ||
| 100 | 105 | } |
| 101 | 106 | } |
| 102 | 107 | |
| 103 | 108 | /** |
| 104 | - * Returns a header value by name. | |
| 109 | + * Returns the first header by name or the default one. | |
| 105 | 110 | * |
| 106 | - * @param string $key The header name | |
| 107 | - * @param string|null $default The default value | |
| 108 | - * | |
| 109 | - * @return string|null The first header value or default value | |
| 111 | + * @return string|null | |
| 110 | 112 | */ |
| 111 | - public function get($key, $default = null) | |
| 113 | + public function get(string $key, ?string $default = null) | |
| 112 | 114 | { |
| 113 | - $headers = $this->all((string) $key); | |
| 114 | - if (2 < \func_num_args()) { | |
| 115 | - @trigger_error(sprintf('Passing a third argument to "%s()" is deprecated since Symfony 4.4, use method "all()" instead', __METHOD__), \E_USER_DEPRECATED); | |
| 115 | + $headers = $this->all($key); | |
| 116 | 116 | |
| 117 | - if (!func_get_arg(2)) { | |
| 118 | - return $headers; | |
| 119 | - } | |
| 120 | - } | |
| 121 | - | |
| 122 | 117 | if (!$headers) { |
| 123 | 118 | return $default; |
| 124 | 119 | } |
| 125 | 120 | |
| @@ -132,13 +127,12 @@ | ||
| 132 | 127 | |
| 133 | 128 | /** |
| 134 | 129 | * Sets a header by name. |
| 135 | 130 | * |
| 136 | - * @param string $key The key | |
| 137 | 131 | * @param string|string[]|null $values The value or an array of values |
| 138 | 132 | * @param bool $replace Whether to replace the actual value or not (true by default) |
| 139 | 133 | */ |
| 140 | - public function set($key, $values, $replace = true) | |
| 134 | + public function set(string $key, $values, bool $replace = true) | |
| 141 | 135 | { |
| 142 | 136 | $key = strtr($key, self::UPPER, self::LOWER); |
| 143 | 137 | |
| 144 | 138 | if (\is_array($values)) { |
| @@ -164,13 +158,11 @@ | ||
| 164 | 158 | |
| 165 | 159 | /** |
| 166 | 160 | * Returns true if the HTTP header is defined. |
| 167 | 161 | * |
| 168 | - * @param string $key The HTTP header | |
| 169 | - * | |
| 170 | - * @return bool true if the parameter exists, false otherwise | |
| 162 | + * @return bool | |
| 171 | 163 | */ |
| 172 | - public function has($key) | |
| 164 | + public function has(string $key) | |
| 173 | 165 | { |
| 174 | 166 | return \array_key_exists(strtr($key, self::UPPER, self::LOWER), $this->all()); |
| 175 | 167 | } |
| 176 | 168 | |
| @@ -176,24 +168,19 @@ | ||
| 176 | 168 | |
| 177 | 169 | /** |
| 178 | 170 | * Returns true if the given HTTP header contains the given value. |
| 179 | 171 | * |
| 180 | - * @param string $key The HTTP header name | |
| 181 | - * @param string $value The HTTP value | |
| 182 | - * | |
| 183 | - * @return bool true if the value is contained in the header, false otherwise | |
| 172 | + * @return bool | |
| 184 | 173 | */ |
| 185 | - public function contains($key, $value) | |
| 174 | + public function contains(string $key, string $value) | |
| 186 | 175 | { |
| 187 | - return \in_array($value, $this->all((string) $key)); | |
| 176 | + return \in_array($value, $this->all($key)); | |
| 188 | 177 | } |
| 189 | 178 | |
| 190 | 179 | /** |
| 191 | 180 | * Removes a header. |
| 192 | - * | |
| 193 | - * @param string $key The HTTP header name | |
| 194 | 181 | */ |
| 195 | - public function remove($key) | |
| 182 | + public function remove(string $key) | |
| 196 | 183 | { |
| 197 | 184 | $key = strtr($key, self::UPPER, self::LOWER); |
| 198 | 185 | |
| 199 | 186 | unset($this->headers[$key]); |
| @@ -205,15 +192,13 @@ | ||
| 205 | 192 | |
| 206 | 193 | /** |
| 207 | 194 | * Returns the HTTP header value converted to a date. |
| 208 | 195 | * |
| 209 | - * @param string $key The parameter key | |
| 196 | + * @return \DateTimeInterface|null | |
| 210 | 197 | * |
| 211 | - * @return \DateTimeInterface|null The parsed DateTime or the default value if the header does not exist | |
| 212 | - * | |
| 213 | 198 | * @throws \RuntimeException When the HTTP header is not parseable |
| 214 | 199 | */ |
| 215 | - public function getDate($key, \DateTime $default = null) | |
| 200 | + public function getDate(string $key, ?\DateTime $default = null) | |
| 216 | 201 | { |
| 217 | 202 | if (null === $value = $this->get($key)) { |
| 218 | 203 | return $default; |
| 219 | 204 | } |
| @@ -227,12 +212,11 @@ | ||
| 227 | 212 | |
| 228 | 213 | /** |
| 229 | 214 | * Adds a custom Cache-Control directive. |
| 230 | 215 | * |
| 231 | - * @param string $key The Cache-Control directive name | |
| 232 | 216 | * @param bool|string $value The Cache-Control directive value |
| 233 | 217 | */ |
| 234 | - public function addCacheControlDirective($key, $value = true) | |
| 218 | + public function addCacheControlDirective(string $key, $value = true) | |
| 235 | 219 | { |
| 236 | 220 | $this->cacheControl[$key] = $value; |
| 237 | 221 | |
| 238 | 222 | $this->set('Cache-Control', $this->getCacheControlHeader()); |
| @@ -240,13 +224,11 @@ | ||
| 240 | 224 | |
| 241 | 225 | /** |
| 242 | 226 | * Returns true if the Cache-Control directive is defined. |
| 243 | 227 | * |
| 244 | - * @param string $key The Cache-Control directive | |
| 245 | - * | |
| 246 | - * @return bool true if the directive exists, false otherwise | |
| 228 | + * @return bool | |
| 247 | 229 | */ |
| 248 | - public function hasCacheControlDirective($key) | |
| 230 | + public function hasCacheControlDirective(string $key) | |
| 249 | 231 | { |
| 250 | 232 | return \array_key_exists($key, $this->cacheControl); |
| 251 | 233 | } |
| 252 | 234 | |
| @@ -252,13 +234,11 @@ | ||
| 252 | 234 | |
| 253 | 235 | /** |
| 254 | 236 | * Returns a Cache-Control directive value by name. |
| 255 | 237 | * |
| 256 | - * @param string $key The directive name | |
| 257 | - * | |
| 258 | - * @return bool|string|null The directive value if defined, null otherwise | |
| 238 | + * @return bool|string|null | |
| 259 | 239 | */ |
| 260 | - public function getCacheControlDirective($key) | |
| 240 | + public function getCacheControlDirective(string $key) | |
| 261 | 241 | { |
| 262 | 242 | return $this->cacheControl[$key] ?? null; |
| 263 | 243 | } |
| 264 | 244 | |
| @@ -263,12 +243,10 @@ | ||
| 263 | 243 | } |
| 264 | 244 | |
| 265 | 245 | /** |
| 266 | 246 | * Removes a Cache-Control directive. |
| 267 | - * | |
| 268 | - * @param string $key The Cache-Control directive | |
| 269 | 247 | */ |
| 270 | - public function removeCacheControlDirective($key) | |
| 248 | + public function removeCacheControlDirective(string $key) | |
| 271 | 249 | { |
| 272 | 250 | unset($this->cacheControl[$key]); |
| 273 | 251 | |
| 274 | 252 | $this->set('Cache-Control', $this->getCacheControlHeader()); |
| @@ -276,9 +254,9 @@ | ||
| 276 | 254 | |
| 277 | 255 | /** |
| 278 | 256 | * Returns an iterator for headers. |
| 279 | 257 | * |
| 280 | - * @return \ArrayIterator An \ArrayIterator instance | |
| 258 | + * @return \ArrayIterator<string, list<string|null>> | |
| 281 | 259 | */ |
| 282 | 260 | #[\ReturnTypeWillChange] |
| 283 | 261 | public function getIterator() |
| 284 | 262 | { |
| @@ -287,9 +265,9 @@ | ||
| 287 | 265 | |
| 288 | 266 | /** |
| 289 | 267 | * Returns the number of headers. |
| 290 | 268 | * |
| 291 | - * @return int The number of headers | |
| 269 | + * @return int | |
| 292 | 270 | */ |
| 293 | 271 | #[\ReturnTypeWillChange] |
| 294 | 272 | public function count() |
| 295 | 273 | { |
| @@ -305,13 +283,11 @@ | ||
| 305 | 283 | |
| 306 | 284 | /** |
| 307 | 285 | * Parses a Cache-Control HTTP header. |
| 308 | 286 | * |
| 309 | - * @param string $header The value of the Cache-Control HTTP header | |
| 310 | - * | |
| 311 | - * @return array An array representing the attribute values | |
| 287 | + * @return array | |
| 312 | 288 | */ |
| 313 | - protected function parseCacheControl($header) | |
| 289 | + protected function parseCacheControl(string $header) | |
| 314 | 290 | { |
| 315 | 291 | $parts = HeaderUtils::split($header, ',='); |
| 316 | 292 | |
| 317 | 293 | return HeaderUtils::combine($parts); |