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