Source for file Collection.php

Documentation is available at Collection.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 http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Collections
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/../Collections/ICollection.php';
  23. 23:  
  24. 24:  
  25. 25:  
  26. 26: /**
  27. 27:  * SPL ArrayObject customization.
  28. 28:  *
  29. 29:  * @author     David Grudl
  30. 30:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  31. 31:  * @package    Nette\Collections
  32. 32:  *
  33. 33:  * @property-read bool $frozen 
  34. 34:  */
  35. 35: abstract class Collection extends ArrayObject implements ICollection
  36. 36: {
  37. 37:     /** @var string  type (class, interface, PHP type) */
  38. 38:     private $itemType;
  39. 39:  
  40. 40:     /** @var string  function to verify type */
  41. 41:     private $checkFunc;
  42. 42:  
  43. 43:     /** @var bool */
  44. 44:     private $frozen FALSE;
  45. 45:  
  46. 46:  
  47. 47:  
  48. 48:     /**
  49. 49:      * @param  array to wrap
  50. 50:      * @param  string class/interface name or ':type'
  51. 51:      * @throws InvalidArgumentException
  52. 52:      */
  53. 53:     public function __construct($arr NULL$type NULL)
  54. 54:     {
  55. 55:         if (substr($type01=== ':'{
  56. 56:             $this->itemType substr($type1);
  57. 57:             $this->checkFunc 'is_' $this->itemType;
  58. 58:         else {
  59. 59:             $this->itemType $type;
  60. 60:         }
  61. 61:  
  62. 62:         if ($arr !== NULL{
  63. 63:             $this->import($arr);
  64. 64:         }
  65. 65:     }
  66. 66:  
  67. 67:  
  68. 68:  
  69. 69:     /**
  70. 70:      * Appends the specified element to the end of this collection.
  71. 71:      * @param  mixed 
  72. 72:      * @return void 
  73. 73:      * @throws InvalidArgumentException
  74. 74:      */
  75. 75:     public function append($item)
  76. 76:     {
  77. 77:         $this->beforeAdd($item);
  78. 78:         parent::append($item);
  79. 79:     }
  80. 80:  
  81. 81:  
  82. 82:  
  83. 83:     /**
  84. 84:      * Removes the first occurrence of the specified element.
  85. 85:      * @param  mixed 
  86. 86:      * @return bool  true if this collection changed as a result of the call
  87. 87:      * @throws NotSupportedException
  88. 88:      */
  89. 89:     public function remove($item)
  90. 90:     {
  91. 91:         $this->updating();
  92. 92:         $index $this->search($item);
  93. 93:         if ($index === FALSE{
  94. 94:             return FALSE;
  95. 95:         else {
  96. 96:             parent::offsetUnset($index);
  97. 97:             return TRUE;
  98. 98:         }
  99. 99:     }
  100. 100:  
  101. 101:  
  102. 102:  
  103. 103:     /**
  104. 104:      * Returns the index of the first occurrence of the specified element,.
  105. 105:      * or FALSE if this collection does not contain this element.
  106. 106:      * @param  mixed 
  107. 107:      * @return int|FALSE
  108. 108:      */
  109. 109:     protected function search($item)
  110. 110:     {
  111. 111:         return array_search($item$this->getArrayCopy()TRUE);
  112. 112:     }
  113. 113:  
  114. 114:  
  115. 115:  
  116. 116:     /**
  117. 117:      * Removes all of the elements from this collection.
  118. 118:      * @return void 
  119. 119:      * @throws NotSupportedException
  120. 120:      */
  121. 121:     public function clear()
  122. 122:     {
  123. 123:         $this->updating();
  124. 124:         parent::exchangeArray(array());
  125. 125:     }
  126. 126:  
  127. 127:  
  128. 128:  
  129. 129:     /**
  130. 130:      * Returns true if this collection contains the specified item.
  131. 131:      * @param  mixed 
  132. 132:      * @return bool 
  133. 133:      */
  134. 134:     public function contains($item)
  135. 135:     {
  136. 136:         return $this->search($item!== FALSE;
  137. 137:     }
  138. 138:  
  139. 139:  
  140. 140:  
  141. 141:     /**
  142. 142:      * Import from array or any traversable object.
  143. 143:      * @param  array|\Traversable
  144. 144:      * @return void 
  145. 145:      * @throws InvalidArgumentException
  146. 146:      */
  147. 147:     public function import($arr)
  148. 148:     {
  149. 149:         if (!(is_array($arr|| $arr instanceof Traversable)) {
  150. 150:             throw new InvalidArgumentException("Argument must be traversable.");
  151. 151:         }
  152. 152:  
  153. 153:         $this->clear();
  154. 154:         foreach ($arr as $item{
  155. 155:             $this->offsetSet(NULL$item);
  156. 156:         }
  157. 157:     }
  158. 158:  
  159. 159:  
  160. 160:  
  161. 161:     /**
  162. 162:      * Returns the item type.
  163. 163:      * @return string 
  164. 164:      */
  165. 165:     public function getItemType()
  166. 166:     {
  167. 167:         return $this->itemType;
  168. 168:     }
  169. 169:  
  170. 170:  
  171. 171:  
  172. 172:     /**
  173. 173:      * @deprecated
  174. 174:      */
  175. 175:     public function setReadOnly()
  176. 176:     {
  177. 177:         throw new DeprecatedException(__METHOD__ . '() is deprecated; use freeze() instead.');
  178. 178:     }
  179. 179:  
  180. 180:  
  181. 181:  
  182. 182:     /**
  183. 183:      * @deprecated
  184. 184:      */
  185. 185:     public function isReadOnly()
  186. 186:     {
  187. 187:         throw new DeprecatedException(__METHOD__ . '() is deprecated; use isFrozen() instead.');
  188. 188:     }
  189. 189:  
  190. 190:  
  191. 191:  
  192. 192:     /********************* internal notifications ****************d*g**/
  193. 193:  
  194. 194:  
  195. 195:  
  196. 196:     /**
  197. 197:      * Responds when the item is about to be added to the collection.
  198. 198:      * @param  mixed 
  199. 199:      * @return void 
  200. 200:      * @throws InvalidArgumentException, \NotSupportedException
  201. 201:      */
  202. 202:     protected function beforeAdd($item)
  203. 203:     {
  204. 204:         $this->updating();
  205. 205:  
  206. 206:         if ($this->itemType !== NULL{
  207. 207:             if ($this->checkFunc === NULL{
  208. 208:                 if (!($item instanceof $this->itemType)) {
  209. 209:                     throw new InvalidArgumentException("Item must be '$this->itemType' object.");
  210. 210:                 }
  211. 211:             else {
  212. 212:                 $fnc $this->checkFunc;
  213. 213:                 if (!$fnc($item)) {
  214. 214:                     throw new InvalidArgumentException("Item must be $this->itemType type.");
  215. 215:                 }
  216. 216:             }
  217. 217:         }
  218. 218:     }
  219. 219:  
  220. 220:  
  221. 221:  
  222. 222:     /********************* ArrayObject cooperation ****************d*g**/
  223. 223:  
  224. 224:  
  225. 225:  
  226. 226:     /**
  227. 227:      * Returns the iterator.
  228. 228:      * @return ArrayIterator 
  229. 229:      */
  230. 230:     public function getIterator()
  231. 231:     {
  232. 232:         return new ArrayIterator($this->getArrayCopy());
  233. 233:     }
  234. 234:  
  235. 235:  
  236. 236:  
  237. 237:     /**
  238. 238:      * Not supported. Use import().
  239. 239:      */
  240. 240:     public function exchangeArray($array)
  241. 241:     {
  242. 242:         throw new NotSupportedException('Use ' . __CLASS__ . '::import()');
  243. 243:     }
  244. 244:  
  245. 245:  
  246. 246:  
  247. 247:     /**
  248. 248:      * Protected exchangeArray().
  249. 249:      * @param  array  new array
  250. 250:      * @return Collection  provides a fluent interface
  251. 251:      */
  252. 252:     protected function setArray($array)
  253. 253:     {
  254. 254:         parent::exchangeArray($array);
  255. 255:         return $this;
  256. 256:     }
  257. 257:  
  258. 258:  
  259. 259:  
  260. 260:     /********************* Nette\Object behaviour ****************d*g**/
  261. 261:  
  262. 262:  
  263. 263:  
  264. 264:     /**
  265. 265:      * Returns the name of the class of this object.
  266. 266:      *
  267. 267:      * @return string 
  268. 268:      */
  269. 269:     final public function getClass()
  270. 270:     {
  271. 271:         return get_class($this);
  272. 272:     }
  273. 273:  
  274. 274:  
  275. 275:  
  276. 276:     /**
  277. 277:      * Call to undefined method.
  278. 278:      *
  279. 279:      * @throws MemberAccessException
  280. 280:      */
  281. 281:     public function __call($name$args)
  282. 282:     {
  283. 283:         return ObjectMixin::call($this$name$args);
  284. 284:     }
  285. 285:  
  286. 286:  
  287. 287:  
  288. 288:     /**
  289. 289:      * Call to undefined static method.
  290. 290:      *
  291. 291:      * @throws MemberAccessException
  292. 292:      */
  293. 293:     public static function __callStatic($name$args)
  294. 294:     {
  295. 295:         $class get_called_class();
  296. 296:         throw new MemberAccessException("Call to undefined static method $class::$name().");
  297. 297:     }
  298. 298:  
  299. 299:  
  300. 300:  
  301. 301:     /**
  302. 302:      * Returns property value. Do not call directly.
  303. 303:      *
  304. 304:      * @throws MemberAccessException if the property is not defined.
  305. 305:      */
  306. 306:     public function &__get($name)
  307. 307:     {
  308. 308:         return ObjectMixin::get($this$name);
  309. 309:     }
  310. 310:  
  311. 311:  
  312. 312:  
  313. 313:     /**
  314. 314:      * Sets value of a property. Do not call directly.
  315. 315:      *
  316. 316:      * @throws MemberAccessException if the property is not defined or is read-only
  317. 317:      */
  318. 318:     public function __set($name$value)
  319. 319:     {
  320. 320:         return ObjectMixin::set($this$name$value);
  321. 321:     }
  322. 322:  
  323. 323:  
  324. 324:  
  325. 325:     /**
  326. 326:      * Is property defined?
  327. 327:      *
  328. 328:      * @param  string  property name
  329. 329:      * @return bool 
  330. 330:      */
  331. 331:     public function __isset($name)
  332. 332:     {
  333. 333:         return ObjectMixin::has($this$name);
  334. 334:     }
  335. 335:  
  336. 336:  
  337. 337:  
  338. 338:     /**
  339. 339:      * Access to undeclared property.
  340. 340:      *
  341. 341:      * @throws MemberAccessException
  342. 342:      */
  343. 343:     public function __unset($name)
  344. 344:     {
  345. 345:         throw new MemberAccessException("Cannot unset the property $this->class::\$$name.");
  346. 346:     }
  347. 347:  
  348. 348:  
  349. 349:  
  350. 350:     /********************* Nette\FreezableObject behaviour ****************d*g**/
  351. 351:  
  352. 352:  
  353. 353:  
  354. 354:     /**
  355. 355:      * Makes the object unmodifiable.
  356. 356:      * @return void 
  357. 357:      */
  358. 358:     public function freeze()
  359. 359:     {
  360. 360:         $this->frozen TRUE;
  361. 361:     }
  362. 362:  
  363. 363:  
  364. 364:  
  365. 365:     /**
  366. 366:      * Is the object unmodifiable?
  367. 367:      * @return bool 
  368. 368:      */
  369. 369:     final public function isFrozen()
  370. 370:     {
  371. 371:         return $this->frozen;
  372. 372:     }
  373. 373:  
  374. 374:  
  375. 375:  
  376. 376:     /**
  377. 377:      * Creates a modifiable clone of the object.
  378. 378:      * @return void 
  379. 379:      */
  380. 380:     public function __clone()
  381. 381:     {
  382. 382:         $this->frozen FALSE;
  383. 383:     }
  384. 384:  
  385. 385:  
  386. 386:  
  387. 387:     /**
  388. 388:      * @return void 
  389. 389:      */
  390. 390:     protected function updating()
  391. 391:     {
  392. 392:         if ($this->frozen{
  393. 393:             $class get_class($this);
  394. 394:             throw new InvalidStateException("Cannot modify a frozen object '$class'.");
  395. 395:         }
  396. 396:     }
  397. 397: