-
Notifications
You must be signed in to change notification settings - Fork 498
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store headers internally as arrays and refactor
This lets us do more magical stuff in other places, since we might need case-insensitive dictionaries elsewhere. The internal use of arrays breaks one test at the moment, this will need to be fixed.
- Loading branch information
Showing
3 changed files
with
165 additions
and
52 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
/** | ||
* Case-insensitive dictionary, suitable for HTTP headers | ||
* | ||
* @package Requests | ||
* @subpackage Utilities | ||
*/ | ||
|
||
/** | ||
* Case-insensitive dictionary, suitable for HTTP headers | ||
* | ||
* @package Requests | ||
* @subpackage Utilities | ||
*/ | ||
class Requests_Utility_CaseInsensitiveDictionary implements ArrayAccess, IteratorAggregate { | ||
/** | ||
* Actual item data | ||
* | ||
* @var array | ||
*/ | ||
protected $data = array(); | ||
|
||
/** | ||
* Check if the given item exists | ||
* | ||
* @param string $key Item key | ||
* @return boolean Does the item exist? | ||
*/ | ||
public function offsetExists($key) { | ||
$key = strtolower($key); | ||
return isset($this->data[$key]); | ||
} | ||
|
||
/** | ||
* Get the value for the item | ||
* | ||
* @param string $key Item key | ||
* @return string Item value | ||
*/ | ||
public function offsetGet($key) { | ||
$key = strtolower($key); | ||
if (!isset($this->data[$key])) | ||
return null; | ||
|
||
return $this->data[$key]; | ||
} | ||
|
||
/** | ||
* Set the given item | ||
* | ||
* @throws Requests_Exception On attempting to use dictionary as list (`invalidset`) | ||
* | ||
* @param string $key Item name | ||
* @param string $value Item value | ||
*/ | ||
public function offsetSet($key, $value) { | ||
if ($key === null) { | ||
throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset'); | ||
} | ||
|
||
$key = strtolower($key); | ||
|
||
if (!isset($this->data[$key])) { | ||
$this->data[$key] = array(); | ||
} | ||
|
||
$this->data[$key][] = $value; | ||
} | ||
|
||
/** | ||
* Unset the given header | ||
* | ||
* @param string $key | ||
*/ | ||
public function offsetUnset($key) { | ||
unset($this->data[strtolower($key)]); | ||
} | ||
|
||
/** | ||
* Get an iterator for the data | ||
* | ||
* @return ArrayIterator | ||
*/ | ||
public function getIterator() { | ||
return new ArrayIterator($this->data); | ||
} | ||
|
||
/** | ||
* Get the headers as an array | ||
* | ||
* @return array Header data | ||
*/ | ||
public function getAll() { | ||
return $this->data; | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
/** | ||
* Iterator for arrays requiring filtered values | ||
* | ||
* @package Requests | ||
* @subpackage Utilities | ||
*/ | ||
|
||
/** | ||
* Iterator for arrays requiring filtered values | ||
* | ||
* @package Requests | ||
* @subpackage Utilities | ||
*/ | ||
class Requests_Utility_FilteredIterator extends ArrayIterator { | ||
/** | ||
* Create a new iterator | ||
* | ||
* @param array $data | ||
* @param callable $callback Callback to be called on each value | ||
*/ | ||
public function __construct($data, $callback) { | ||
parent::__construct($data); | ||
|
||
$this->callback = $callback; | ||
} | ||
|
||
/** | ||
* Get the current item's value after filtering | ||
* | ||
* @return string | ||
*/ | ||
public function current() { | ||
$value = parent::current(); | ||
$value = call_user_func($this->callback, $value); | ||
return $value; | ||
} | ||
} |