AutoPagingIterator.php
6 years ago
CaseInsensitiveArray.php
6 years ago
DefaultLogger.php
6 years ago
LoggerInterface.php
6 years ago
RandomGenerator.php
6 years ago
RequestOptions.php
6 years ago
Set.php
6 years ago
Util.php
6 years ago
CaseInsensitiveArray.php
63 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Stripe\Util; |
| 4 | |
| 5 | use ArrayAccess; |
| 6 | |
| 7 | /** |
| 8 | * CaseInsensitiveArray is an array-like class that ignores case for keys. |
| 9 | * |
| 10 | * It is used to store HTTP headers. Per RFC 2616, section 4.2: |
| 11 | * Each header field consists of a name followed by a colon (":") and the field value. Field names |
| 12 | * are case-insensitive. |
| 13 | * |
| 14 | * In the context of stripe-php, this is useful because the API will return headers with different |
| 15 | * case depending on whether HTTP/2 is used or not (with HTTP/2, headers are always in lowercase). |
| 16 | */ |
| 17 | class CaseInsensitiveArray implements ArrayAccess |
| 18 | { |
| 19 | private $container = array(); |
| 20 | |
| 21 | public function __construct($initial_array = array()) |
| 22 | { |
| 23 | $this->container = array_map("strtolower", $initial_array); |
| 24 | } |
| 25 | |
| 26 | public function offsetSet($offset, $value) |
| 27 | { |
| 28 | $offset = static::maybeLowercase($offset); |
| 29 | if (is_null($offset)) { |
| 30 | $this->container[] = $value; |
| 31 | } else { |
| 32 | $this->container[$offset] = $value; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | public function offsetExists($offset) |
| 37 | { |
| 38 | $offset = static::maybeLowercase($offset); |
| 39 | return isset($this->container[$offset]); |
| 40 | } |
| 41 | |
| 42 | public function offsetUnset($offset) |
| 43 | { |
| 44 | $offset = static::maybeLowercase($offset); |
| 45 | unset($this->container[$offset]); |
| 46 | } |
| 47 | |
| 48 | public function offsetGet($offset) |
| 49 | { |
| 50 | $offset = static::maybeLowercase($offset); |
| 51 | return isset($this->container[$offset]) ? $this->container[$offset] : null; |
| 52 | } |
| 53 | |
| 54 | private static function maybeLowercase($v) |
| 55 | { |
| 56 | if (is_string($v)) { |
| 57 | return strtolower($v); |
| 58 | } else { |
| 59 | return $v; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 |