|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Quantum PHP Framework |
| 7 | + * An open-source software development framework for PHP |
| 8 | + * @link https://quantumphp.io |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Quantum\HttpClient; |
| 12 | + |
| 13 | +use ArrayAccess; |
| 14 | +use Countable; |
| 15 | +use Iterator; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class ResponseHeaders |
| 19 | + * @package Quantum\HttpClient |
| 20 | + * @implements ArrayAccess<int|string, mixed> |
| 21 | + * @implements Iterator<int|string, mixed> |
| 22 | + */ |
| 23 | +class ResponseHeaders implements ArrayAccess, Countable, Iterator |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var array<int|string, mixed> |
| 27 | + */ |
| 28 | + private array $data = []; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var array<string, int|string> |
| 32 | + */ |
| 33 | + private array $keys = []; |
| 34 | + |
| 35 | + /** |
| 36 | + * @param array<int|string, mixed>|null $headers |
| 37 | + */ |
| 38 | + public function __construct(?array $headers = null) |
| 39 | + { |
| 40 | + if ($headers !== null) { |
| 41 | + foreach ($headers as $key => $value) { |
| 42 | + $this->offsetSet($key, $value); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @param int|string|null $offset |
| 49 | + * @param mixed $value |
| 50 | + */ |
| 51 | + public function offsetSet($offset, $value): void |
| 52 | + { |
| 53 | + if ($offset === null) { |
| 54 | + $this->data[] = $value; |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + $normalizedOffset = strtolower((string) $offset); |
| 59 | + $this->data[$normalizedOffset] = $value; |
| 60 | + $this->keys[$normalizedOffset] = $offset; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @param int|string $offset |
| 65 | + */ |
| 66 | + public function offsetExists($offset): bool |
| 67 | + { |
| 68 | + return array_key_exists(strtolower((string) $offset), $this->data); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @param int|string $offset |
| 73 | + */ |
| 74 | + public function offsetUnset($offset): void |
| 75 | + { |
| 76 | + $normalizedOffset = strtolower((string) $offset); |
| 77 | + |
| 78 | + unset($this->data[$normalizedOffset]); |
| 79 | + unset($this->keys[$normalizedOffset]); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @param int|string $offset |
| 84 | + * @return mixed|null |
| 85 | + */ |
| 86 | + public function offsetGet($offset) |
| 87 | + { |
| 88 | + return $this->data[strtolower((string) $offset)] ?? null; |
| 89 | + } |
| 90 | + |
| 91 | + public function count(): int |
| 92 | + { |
| 93 | + return count($this->data); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @return mixed |
| 98 | + */ |
| 99 | + public function current() |
| 100 | + { |
| 101 | + return current($this->data); |
| 102 | + } |
| 103 | + |
| 104 | + public function next(): void |
| 105 | + { |
| 106 | + next($this->data); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @return int|string|null |
| 111 | + */ |
| 112 | + public function key() |
| 113 | + { |
| 114 | + $key = key($this->data); |
| 115 | + |
| 116 | + if ($key === null) { |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
| 120 | + return $this->keys[$key] ?? $key; |
| 121 | + } |
| 122 | + |
| 123 | + public function valid(): bool |
| 124 | + { |
| 125 | + return key($this->data) !== null; |
| 126 | + } |
| 127 | + |
| 128 | + public function rewind(): void |
| 129 | + { |
| 130 | + reset($this->data); |
| 131 | + } |
| 132 | +} |
0 commit comments