Page MenuHomePhabricator
Diviner libphutil Tech Docs CaseInsensitiveArray

final class CaseInsensitiveArray
libphutil Technical Documentation (Core Utilities)

A case-insensitive associative array.

This class represents an associative array in which the keys are considered to be case insensitive. This means that $array['key'] and $array['KEY'] will refer to the same array element.

$array = new CaseInsensitiveArray();

$array['key'] = 'value';
echo $array['KEY']; // 'value'

$array['kEy'] = 'foobar';
var_dump($array->toArray()); // array('key' => 'foobar')

Note that it is not possible to reuse case variants of a key. That is, if the array contains key xyz then it is not possible to use any of the following case variants as an array key: xyZ, xYz, xYZ, Xyz, XyZ, XYz, XYZ. In order to use a case variant as a key, it is necessary to first unset the original case variant.

$array = new CaseInsensitiveArray(array('key' => 'foo', 'KEY' => 'bar'));
var_dump($array->toArray()); // array('key' => 'bar')

$array['KEY'] = 'baz';
var_dump($array->toArray()); // array('key' => 'baz')

unset($array['key']);
$array['KEY'] = 'baz';
var_dump($array->toArray()); // array('KEY' => 'baz')

Methods

public function __construct($data)

Construct a new array object.

Parameters
array$dataThe input array.
Return
this//Implicit.//

public function getKeys()

This method is not documented.
Return
wild

public function offsetExists($key)

This method is not documented.
Parameters
$key
Return
wild

public function offsetGet($key)

This method is not documented.
Parameters
$key
Return
wild

public function offsetSet($key, $value)

This method is not documented.
Parameters
$key
$value
Return
wild

public function offsetUnset($key)

This method is not documented.
Parameters
$key
Return
wild

private function transformKey($key)

Transform an array key.

This method transforms an array key to be case-invariant. We could just call [[http://php.net/manual/en/function.strtolower.php | strtolower]] directly, but this method allows us to contain the key transformation logic within a single method, should it ever change.

Theoretically, we should be able to use any of the following functions for the purpose of key transformations:

Parameters
string$keyThe input key.
Return
stringThe transformed key.