CaseInsensitiveDictionary.php
5 months ago
FilteredIterator.php
5 months ago
InputValidator.php
5 months ago
CaseInsensitiveDictionary.php
140 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Case-insensitive dictionary, suitable for HTTP headers |
| 4 | * |
| 5 | * @package Requests\Utilities |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaVendor\WpOrg\Requests\Utility; |
| 9 | |
| 10 | use ArrayAccess; |
| 11 | use ArrayIterator; |
| 12 | use IteratorAggregate; |
| 13 | use ReturnTypeWillChange; |
| 14 | use AmeliaVendor\WpOrg\Requests\Exception; |
| 15 | |
| 16 | /** |
| 17 | * Case-insensitive dictionary, suitable for HTTP headers |
| 18 | * |
| 19 | * @package Requests\Utilities |
| 20 | */ |
| 21 | class CaseInsensitiveDictionary implements ArrayAccess, IteratorAggregate { |
| 22 | /** |
| 23 | * Actual item data |
| 24 | * |
| 25 | * @var array |
| 26 | */ |
| 27 | protected $data = []; |
| 28 | |
| 29 | /** |
| 30 | * Creates a case insensitive dictionary. |
| 31 | * |
| 32 | * @param array $data Dictionary/map to convert to case-insensitive |
| 33 | */ |
| 34 | public function __construct(array $data = []) { |
| 35 | foreach ($data as $offset => $value) { |
| 36 | $this->offsetSet($offset, $value); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Check if the given item exists |
| 42 | * |
| 43 | * @param string $offset Item key |
| 44 | * @return boolean Does the item exist? |
| 45 | */ |
| 46 | #[ReturnTypeWillChange] |
| 47 | public function offsetExists($offset) { |
| 48 | if (is_string($offset)) { |
| 49 | $offset = strtolower($offset); |
| 50 | } |
| 51 | |
| 52 | if ($offset === null) { |
| 53 | $offset = ''; |
| 54 | } |
| 55 | |
| 56 | return isset($this->data[$offset]); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get the value for the item |
| 61 | * |
| 62 | * @param string $offset Item key |
| 63 | * @return string|null Item value (null if the item key doesn't exist) |
| 64 | */ |
| 65 | #[ReturnTypeWillChange] |
| 66 | public function offsetGet($offset) { |
| 67 | if (is_string($offset)) { |
| 68 | $offset = strtolower($offset); |
| 69 | } |
| 70 | |
| 71 | if ($offset === null) { |
| 72 | $offset = ''; |
| 73 | } |
| 74 | |
| 75 | if (!isset($this->data[$offset])) { |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | return $this->data[$offset]; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Set the given item |
| 84 | * |
| 85 | * @param string $offset Item name |
| 86 | * @param string $value Item value |
| 87 | * |
| 88 | * @throws \AmeliaVendor\WpOrg\Requests\Exception On attempting to use dictionary as list (`invalidset`) |
| 89 | */ |
| 90 | #[ReturnTypeWillChange] |
| 91 | public function offsetSet($offset, $value) { |
| 92 | if ($offset === null) { |
| 93 | throw new Exception('Object is a dictionary, not a list', 'invalidset'); |
| 94 | } |
| 95 | |
| 96 | if (is_string($offset)) { |
| 97 | $offset = strtolower($offset); |
| 98 | } |
| 99 | |
| 100 | $this->data[$offset] = $value; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Unset the given header |
| 105 | * |
| 106 | * @param string $offset The key for the item to unset. |
| 107 | */ |
| 108 | #[ReturnTypeWillChange] |
| 109 | public function offsetUnset($offset) { |
| 110 | if (is_string($offset)) { |
| 111 | $offset = strtolower($offset); |
| 112 | } |
| 113 | |
| 114 | if ($offset === null) { |
| 115 | $offset = ''; |
| 116 | } |
| 117 | |
| 118 | unset($this->data[$offset]); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Get an iterator for the data |
| 123 | * |
| 124 | * @return \ArrayIterator |
| 125 | */ |
| 126 | #[ReturnTypeWillChange] |
| 127 | public function getIterator() { |
| 128 | return new ArrayIterator($this->data); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get the headers as an array |
| 133 | * |
| 134 | * @return array Header data |
| 135 | */ |
| 136 | public function getAll() { |
| 137 | return $this->data; |
| 138 | } |
| 139 | } |
| 140 |