Source for file Hashtable.php
Documentation is available at Hashtable.php
6: * @copyright Copyright (c) 2004, 2010 David Grudl
7: * @license http://nettephp.com/license Nette license
8: * @link http://nettephp.com
10: * @package Nette\Collections
16: * The exception that is thrown when the key specified for accessing
17: * an element in a collection does not match any key.
18: * @package Nette\Collections
27: * Provides the base class for a generic collection of keys and values.
29: * @copyright Copyright (c) 2004, 2010 David Grudl
30: * @package Nette\Collections
35: private $throwKeyNotFound =
FALSE;
40: * Inserts the specified element to the map.
44: * @throws InvalidArgumentException, \InvalidStateException
46: public function add($key, $item)
48: // note: $item is nullable to be compatible with that of ICollection::add()
50: throw new InvalidArgumentException("Key must be either a string or an integer, " .
gettype($key) .
" given.");
53: if (parent::offsetExists($key)) {
58: parent::offsetSet($key, $item);
65: * Append is not supported.
75: * Returns a array of the keys contained in this map.
86: * Returns the key of the first occurrence of the specified element,.
87: * or FALSE if this map does not contain this element.
99: * Import from array or any traversable object.
100: * @param array|\Traversable
102: * @throws InvalidArgumentException
108: if (!(is_array($arr) ||
$arr instanceof
Traversable)) {
109: throw new InvalidArgumentException("Argument must be traversable.");
117: foreach ($arr as $key =>
$item) {
126: * Returns variable or $default if there is no element.
128: * @param mixed default value
130: * @throws InvalidArgumentException
132: public function get($key, $default =
NULL)
135: throw new InvalidArgumentException("Key must be either a string or an integer, " .
gettype($key) .
" given.");
138: if (parent::offsetExists($key)) {
139: return parent::offsetGet($key);
149: * Do throw KeyNotFoundException?
154: $this->throwKeyNotFound = (bool)
$val;
159: /********************* interface \ArrayAccess ****************d*g**/
164: * Inserts (replaces) item (\ArrayAccess implementation).
168: * @throws NotSupportedException, \InvalidArgumentException
173: throw new InvalidArgumentException("Key must be either a string or an integer, " .
gettype($key) .
" given.");
177: parent::offsetSet($key, $item);
183: * Returns item (\ArrayAccess implementation).
186: * @throws KeyNotFoundException, \InvalidArgumentException
191: throw new InvalidArgumentException("Key must be either a string or an integer, " .
gettype($key) .
" given.");
194: if (parent::offsetExists($key)) {
195: return parent::offsetGet($key);
197: } elseif ($this->throwKeyNotFound) {
208: * Exists item? (\ArrayAccess implementation).
211: * @throws InvalidArgumentException
216: throw new InvalidArgumentException("Key must be either a string or an integer, " .
gettype($key) .
" given.");
219: return parent::offsetExists($key);
225: * Removes the element at the specified position in this list.
228: * @throws NotSupportedException, \InvalidArgumentException
235: throw new InvalidArgumentException("Key must be either a string or an integer, " .
gettype($key) .
" given.");
238: if (parent::offsetExists($key)) {
239: parent::offsetUnset($key);