-
Notifications
You must be signed in to change notification settings - Fork 850
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store HTTP response headers in case-insensitive array
- Loading branch information
Showing
4 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Stripe\Util; | ||
|
||
use ArrayAccess; | ||
|
||
/** | ||
* CaseInsensitiveArray is an array-like class that ignores case for keys. | ||
* | ||
* It is used to store HTTP headers. Per RFC 2616, section 4.2: | ||
* Each header field consists of a name followed by a colon (":") and the field value. Field names | ||
* are case-insensitive. | ||
* | ||
* In the context of stripe-php, this is useful because the API will return headers with different | ||
* case depending on whether HTTP/2 is used or not (with HTTP/2, headers are always in lowercase). | ||
*/ | ||
class CaseInsensitiveArray implements ArrayAccess | ||
{ | ||
private $container = array(); | ||
|
||
public function __construct($initial_array = array()) | ||
{ | ||
$this->container = array_map("strtolower", $initial_array); | ||
} | ||
|
||
public function offsetSet($offset, $value) | ||
{ | ||
$offset = static::maybeLowercase($offset); | ||
if (is_null($offset)) { | ||
$this->container[] = $value; | ||
} else { | ||
$this->container[$offset] = $value; | ||
} | ||
} | ||
|
||
public function offsetExists($offset) | ||
{ | ||
$offset = static::maybeLowercase($offset); | ||
return isset($this->container[$offset]); | ||
} | ||
|
||
public function offsetUnset($offset) | ||
{ | ||
$offset = static::maybeLowercase($offset); | ||
unset($this->container[$offset]); | ||
} | ||
|
||
public function offsetGet($offset) | ||
{ | ||
$offset = static::maybeLowercase($offset); | ||
return isset($this->container[$offset]) ? $this->container[$offset] : null; | ||
} | ||
|
||
private static function maybeLowercase($v) | ||
{ | ||
if (is_string($v)) { | ||
return strtolower($v); | ||
} else { | ||
return $v; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters