123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- namespace App\Services\Common;
- class CaseInsensitiveArrayService implements \ArrayAccess, \Countable, \Iterator
- {
-
- private $data = array();
-
- private $keys = array();
-
- public function __construct(Array $initial = null)
- {
- if ($initial !== null) {
- foreach ($initial as $key => $value) {
- $this->offsetSet($key, $value);
- }
- }
- }
-
- public function offsetSet($offset, $value)
- {
- if ($offset === null) {
- $this->data[] = $value;
- } else {
- $offsetlower = strtolower($offset);
- $this->data[$offsetlower] = $value;
- $this->keys[$offsetlower] = $offset;
- }
- }
-
- public function offsetExists($offset)
- {
- return (bool) array_key_exists(strtolower($offset), $this->data);
- }
-
- public function offsetUnset($offset)
- {
- $offsetlower = strtolower($offset);
- unset($this->data[$offsetlower]);
- unset($this->keys[$offsetlower]);
- }
-
- public function offsetGet($offset)
- {
- $offsetlower = strtolower($offset);
- return isset($this->data[$offsetlower]) ? $this->data[$offsetlower] : null;
- }
-
- public function count()
- {
- return (int) count($this->data);
- }
-
- public function current()
- {
- return current($this->data);
- }
-
- public function next()
- {
- next($this->data);
- }
-
- public function key()
- {
- $key = key($this->data);
- return isset($this->keys[$key]) ? $this->keys[$key] : $key;
- }
-
- public function valid()
- {
- return (bool) !(key($this->data) === null);
- }
-
- public function rewind()
- {
- reset($this->data);
- }
- }
|