Source for file Hashtable.php

Documentation is available at Hashtable.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see https://nette.org
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    https://nette.org/license  Nette license
  15. 15:  * @link       https://nette.org
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Collections
  18. 18:  * @version    $Id$
  19. 19:  */
  20. 20:  
  21. 21:  
  22. 22:  
  23. 23: require_once dirname(__FILE__'/../Collections/Collection.php';
  24. 24:  
  25. 25: require_once dirname(__FILE__'/../Collections/IMap.php';
  26. 26:  
  27. 27:  
  28. 28:  
  29. 29: /**
  30. 30:  * The exception that is thrown when the key specified for accessing
  31. 31:  * an element in a collection does not match any key.
  32. 32:  * @package    Nette\Collections
  33. 33:  */
  34. 34: class KeyNotFoundException extends RuntimeException
  35. 35: {
  36. 36: }
  37. 37:  
  38. 38:  
  39. 39:  
  40. 40: /**
  41. 41:  * Provides the base class for a generic collection of keys and values.
  42. 42:  *
  43. 43:  * @author     David Grudl
  44. 44:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  45. 45:  * @package    Nette\Collections
  46. 46:  */
  47. 47: class Hashtable extends Collection implements IMap
  48. 48: {
  49. 49:     /** @var bool */
  50. 50:     private $throwKeyNotFound FALSE;
  51. 51:  
  52. 52:  
  53. 53:  
  54. 54:     /**
  55. 55:      * Inserts the specified element to the map.
  56. 56:      * @param  mixed 
  57. 57:      * @param  mixed 
  58. 58:      * @return bool 
  59. 59:      * @throws InvalidArgumentException, \InvalidStateException
  60. 60:      */
  61. 61:     public function add($key$item)
  62. 62:     {
  63. 63:         // note: $item is nullable to be compatible with that of ICollection::add()
  64. 64:         if (!is_scalar($key)) {
  65. 65:             throw new InvalidArgumentException("Key must be either a string or an integer, " gettype($key." given.");
  66. 66:         }
  67. 67:  
  68. 68:         if (parent::offsetExists($key)) {
  69. 69:             throw new InvalidStateException('An element with the same key already exists.');
  70. 70:         }
  71. 71:  
  72. 72:         $this->beforeAdd($item);
  73. 73:         parent::offsetSet($key$item);
  74. 74:         return TRUE;
  75. 75:     }
  76. 76:  
  77. 77:  
  78. 78:  
  79. 79:     /**
  80. 80:      * Append is not supported.
  81. 81:      */
  82. 82:     public function append($item)
  83. 83:     {
  84. 84:         throw new NotSupportedException;
  85. 85:     }
  86. 86:  
  87. 87:  
  88. 88:  
  89. 89:     /**
  90. 90:      * Returns a array of the keys contained in this map.
  91. 91:      * return array
  92. 92:      */
  93. 93:     public function getKeys()
  94. 94:     {
  95. 95:         return array_keys($this->getArrayCopy());
  96. 96:     }
  97. 97:  
  98. 98:  
  99. 99:  
  100. 100:     /**
  101. 101:      * Returns the key of the first occurrence of the specified element,.
  102. 102:      * or FALSE if this map does not contain this element.
  103. 103:      * @param  mixed 
  104. 104:      * @return mixed 
  105. 105:      */
  106. 106:     public function search($item)
  107. 107:     {
  108. 108:         return array_search($item$this->getArrayCopy()TRUE);
  109. 109:     }
  110. 110:  
  111. 111:  
  112. 112:  
  113. 113:     /**
  114. 114:      * Import from array or any traversable object.
  115. 115:      * @param  array|\Traversable
  116. 116:      * @return void 
  117. 117:      * @throws InvalidArgumentException
  118. 118:      */
  119. 119:     public function import($arr)
  120. 120:     {
  121. 121:         if (!(is_array($arr|| $arr instanceof Traversable)) {
  122. 122:             throw new InvalidArgumentException("Argument must be traversable.");
  123. 123:         }
  124. 124:  
  125. 125:         if (!$this->readOnly && $this->itemType === NULL// optimalization
  126. 126:             $this->setArray((array) $arr);
  127. 127:  
  128. 128:         else {
  129. 129:             $this->clear();
  130. 130:             foreach ($arr as $key => $item{
  131. 131:                 $this->offsetSet($key$item);
  132. 132:             }
  133. 133:         }
  134. 134:     }
  135. 135:  
  136. 136:  
  137. 137:  
  138. 138:     /**
  139. 139:      * Returns variable or $default if there is no element.
  140. 140:      * @param  string key
  141. 141:      * @param  mixed  default value
  142. 142:      * @return mixed 
  143. 143:      * @throws InvalidArgumentException
  144. 144:      */
  145. 145:     public function get($key$default NULL)
  146. 146:     {
  147. 147:         if (!is_scalar($key)) {
  148. 148:             throw new InvalidArgumentException("Key must be either a string or an integer, " gettype($key." given.");
  149. 149:         }
  150. 150:  
  151. 151:         if (parent::offsetExists($key)) {
  152. 152:             return parent::offsetGet($key);
  153. 153:  
  154. 154:         else {
  155. 155:             return $default;
  156. 156:         }
  157. 157:     }
  158. 158:  
  159. 159:  
  160. 160:  
  161. 161:     /**
  162. 162:      * Do throw KeyNotFoundException?
  163. 163:      * @return void 
  164. 164:      */
  165. 165:     public function throwKeyNotFound($val TRUE)
  166. 166:     {
  167. 167:         $this->throwKeyNotFound = (bool) $val;
  168. 168:     }
  169. 169:  
  170. 170:  
  171. 171:  
  172. 172:     /********************* interface \ArrayAccess ****************d*g**/
  173. 173:  
  174. 174:  
  175. 175:  
  176. 176:     /**
  177. 177:      * Inserts (replaces) item (\ArrayAccess implementation).
  178. 178:      * @param  string key
  179. 179:      * @param  object 
  180. 180:      * @return void 
  181. 181:      * @throws NotSupportedException, \InvalidArgumentException
  182. 182:      */
  183. 183:     public function offsetSet($key$item)
  184. 184:     {
  185. 185:         if (!is_scalar($key)) // prevents NULL
  186. 186:             throw new InvalidArgumentException("Key must be either a string or an integer, " gettype($key." given.");
  187. 187:         }
  188. 188:  
  189. 189:         $this->beforeAdd($item);
  190. 190:         parent::offsetSet($key$item);
  191. 191:     }
  192. 192:  
  193. 193:  
  194. 194:  
  195. 195:     /**
  196. 196:      * Returns item (\ArrayAccess implementation).
  197. 197:      * @param  string key
  198. 198:      * @return mixed 
  199. 199:      * @throws KeyNotFoundException, \InvalidArgumentException
  200. 200:      */
  201. 201:     public function offsetGet($key)
  202. 202:     {
  203. 203:         if (!is_scalar($key)) {
  204. 204:             throw new InvalidArgumentException("Key must be either a string or an integer, " gettype($key." given.");
  205. 205:         }
  206. 206:  
  207. 207:         if (parent::offsetExists($key)) {
  208. 208:             return parent::offsetGet($key);
  209. 209:  
  210. 210:         elseif ($this->throwKeyNotFound{
  211. 211:             throw new KeyNotFoundException;
  212. 212:  
  213. 213:         else {
  214. 214:             return NULL;
  215. 215:         }
  216. 216:     }
  217. 217:  
  218. 218:  
  219. 219:  
  220. 220:     /**
  221. 221:      * Exists item? (\ArrayAccess implementation).
  222. 222:      * @param  string key
  223. 223:      * @return bool 
  224. 224:      * @throws InvalidArgumentException
  225. 225:      */
  226. 226:     public function offsetExists($key)
  227. 227:     {
  228. 228:         if (!is_scalar($key)) {
  229. 229:             throw new InvalidArgumentException("Key must be either a string or an integer, " gettype($key." given.");
  230. 230:         }
  231. 231:  
  232. 232:         return parent::offsetExists($key);
  233. 233:     }
  234. 234:  
  235. 235:  
  236. 236:  
  237. 237:     /**
  238. 238:      * Removes the element at the specified position in this list.
  239. 239:      * @param  string key
  240. 240:      * @return void 
  241. 241:      * @throws NotSupportedException, \InvalidArgumentException
  242. 242:      */
  243. 243:     public function offsetUnset($key)
  244. 244:     {
  245. 245:         if (!is_scalar($key)) {
  246. 246:             throw new InvalidArgumentException("Key must be either a string or an integer, " gettype($key." given.");
  247. 247:         }
  248. 248:  
  249. 249:         $this->beforeRemove();
  250. 250:         if (parent::offsetExists($key)) {
  251. 251:             parent::offsetUnset($key);
  252. 252:         }
  253. 253:     }
  254. 254: