CaseInsensitiveDictionary
云策文档标注
概述
CaseInsensitiveDictionary 是一个用于处理 HTTP 头部的类,实现大小写不敏感的字典功能。它实现了 ArrayAccess 和 IteratorAggregate 接口,提供类似数组的访问方式。
关键要点
- 类实现 ArrayAccess 和 IteratorAggregate 接口,支持数组式操作和迭代
- 构造函数 __construct 接受数组参数,初始化时自动转换键名为小写
- 方法 getAll 返回所有数据作为数组
- 方法 getIterator 返回 ArrayIterator 用于遍历数据
- offsetExists、offsetGet、offsetSet、offsetUnset 方法处理键值对的检查、获取、设置和删除,键名自动转换为小写
- offsetSet 方法禁止使用 null 键,抛出异常以防止误用为列表
代码示例
// 示例:创建和使用 CaseInsensitiveDictionary
$data = ['Content-Type' => 'application/json', 'Authorization' => 'Bearer token'];
$dict = new CaseInsensitiveDictionary($data);
// 大小写不敏感访问
echo $dict['content-type']; // 输出: application/json
echo $dict['CONTENT-TYPE']; // 输出: application/json
// 获取所有数据
$allData = $dict->getAll(); // 返回: ['content-type' => 'application/json', 'authorization' => 'Bearer token']
// 迭代数据
foreach ($dict as $key => $value) {
echo "$key: $value";
}注意事项
- 键名在内部存储时自动转换为小写,确保大小写不敏感
- 禁止使用 null 作为键名,否则会抛出异常,因为该类设计为字典而非列表
- 适用于 HTTP 头部处理,但也可用于其他需要大小写不敏感键的场景
原文内容
Case-insensitive dictionary, suitable for HTTP headers
Methods
| Name | Description |
|---|---|
| CaseInsensitiveDictionary::__construct | Creates a case insensitive dictionary. |
| CaseInsensitiveDictionary::getAll | Get the headers as an array |
| CaseInsensitiveDictionary::getIterator | – |
| CaseInsensitiveDictionary::offsetExists | – |
| CaseInsensitiveDictionary::offsetGet | – |
| CaseInsensitiveDictionary::offsetSet | – |
| CaseInsensitiveDictionary::offsetUnset | – |
Source
class CaseInsensitiveDictionary implements ArrayAccess, IteratorAggregate {
/**
* Actual item data
*
* @var array
*/
protected $data = [];
/**
* Creates a case insensitive dictionary.
*
* @param array $data Dictionary/map to convert to case-insensitive
*/
public function __construct(array $data = []) {
foreach ($data as $offset => $value) {
$this->offsetSet($offset, $value);
}
}
/**
* Check if the given item exists
*
* @param string $offset Item key
* @return boolean Does the item exist?
*/
#[ReturnTypeWillChange]
public function offsetExists($offset) {
if (is_string($offset)) {
$offset = strtolower($offset);
}
return isset($this->data[$offset]);
}
/**
* Get the value for the item
*
* @param string $offset Item key
* @return string|null Item value (null if the item key doesn't exist)
*/
#[ReturnTypeWillChange]
public function offsetGet($offset) {
if (is_string($offset)) {
$offset = strtolower($offset);
}
if (!isset($this->data[$offset])) {
return null;
}
return $this->data[$offset];
}
/**
* Set the given item
*
* @param string $offset Item name
* @param string $value Item value
*
* @throws WpOrgRequestsException On attempting to use dictionary as list (`invalidset`)
*/
#[ReturnTypeWillChange]
public function offsetSet($offset, $value) {
if ($offset === null) {
throw new Exception('Object is a dictionary, not a list', 'invalidset');
}
if (is_string($offset)) {
$offset = strtolower($offset);
}
$this->data[$offset] = $value;
}
/**
* Unset the given header
*
* @param string $offset The key for the item to unset.
*/
#[ReturnTypeWillChange]
public function offsetUnset($offset) {
if (is_string($offset)) {
$offset = strtolower($offset);
}
unset($this->data[$offset]);
}
/**
* Get an iterator for the data
*
* @return ArrayIterator
*/
#[ReturnTypeWillChange]
public function getIterator() {
return new ArrayIterator($this->data);
}
/**
* Get the headers as an array
*
* @return array Header data
*/
public function getAll() {
return $this->data;
}
}