Source for file ArrayList.php

Documentation is available at ArrayList.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/Collection.php';
  23. 23:  
  24. 24: require_once dirname(__FILE__'/../Collections/IList.php';
  25. 25:  
  26. 26:  
  27. 27:  
  28. 28: /**
  29. 29:  * Provides the base class for a generic list (items can be accessed by index).
  30. 30:  *
  31. 31:  * @author     David Grudl
  32. 32:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  33. 33:  * @package    Nette\Collections
  34. 34:  */
  35. 35: class ArrayList extends Collection implements IList
  36. 36: {
  37. 37:     /** @var int */
  38. 38:     protected $base = 0;
  39. 39:  
  40. 40:  
  41. 41:     /**
  42. 42:      * Inserts the specified element at the specified position in this list.
  43. 43:      * @param  int 
  44. 44:      * @param  mixed 
  45. 45:      * @return bool 
  46. 46:      * @throws ArgumentOutOfRangeException
  47. 47:      */
  48. 48:     public function insertAt($index$item)
  49. 49:     {
  50. 50:         $index -= $this->base;
  51. 51:         if ($index || $index count($this)) {
  52. 52:             throw new ArgumentOutOfRangeException;
  53. 53:         }
  54. 54:  
  55. 55:         $this->beforeAdd($item);
  56. 56:         $data $this->getArrayCopy();
  57. 57:         array_splice($data(int) $index0array($item));
  58. 58:         $this->setArray($data);
  59. 59:         return TRUE;
  60. 60:     }
  61. 61:  
  62. 62:  
  63. 63:  
  64. 64:     /**
  65. 65:      * Removes the first occurrence of the specified element.
  66. 66:      * @param  mixed 
  67. 67:      * @return bool  true if this list changed as a result of the call
  68. 68:      * @throws NotSupportedException
  69. 69:      */
  70. 70:     public function remove($item)
  71. 71:     {
  72. 72:         $this->updating();
  73. 73:  
  74. 74:         $index $this->search($item);
  75. 75:         if ($index === FALSE{
  76. 76:             return FALSE;
  77. 77:         else {
  78. 78:             $data $this->getArrayCopy();
  79. 79:             array_splice($data$index1);
  80. 80:             $this->setArray($data);
  81. 81:             return TRUE;
  82. 82:         }
  83. 83:     }
  84. 84:  
  85. 85:  
  86. 86:  
  87. 87:     /**
  88. 88:      * Returns the index of the first occurrence of the specified element,.
  89. 89:      * or FALSE if this list does not contain this element.
  90. 90:      * @param  mixed 
  91. 91:      * @return int|FALSE
  92. 92:      */
  93. 93:     public function indexOf($item)
  94. 94:     {
  95. 95:         $index $this->search($item);
  96. 96:         return $index === FALSE FALSE $this->base + $index;
  97. 97:     }
  98. 98:  
  99. 99:  
  100. 100:  
  101. 101:     /********************* interface \ArrayAccess ****************d*g**/
  102. 102:  
  103. 103:  
  104. 104:  
  105. 105:     /**
  106. 106:      * Replaces (or appends) the item (\ArrayAccess implementation).
  107. 107:      * @param  int index
  108. 108:      * @param  object 
  109. 109:      * @return void 
  110. 110:      * @throws InvalidArgumentException, \NotSupportedException, \ArgumentOutOfRangeException
  111. 111:      */
  112. 112:     public function offsetSet($index$item)
  113. 113:     {
  114. 114:         $this->beforeAdd($item);
  115. 115:  
  116. 116:         if ($index === NULL)  // append
  117. 117:             parent::offsetSet(NULL$item);
  118. 118:  
  119. 119:         else // replace
  120. 120:             $index -= $this->base;
  121. 121:             if ($index || $index >= count($this)) {
  122. 122:                 throw new ArgumentOutOfRangeException;
  123. 123:             }
  124. 124:             parent::offsetSet($index$item);
  125. 125:         }
  126. 126:     }
  127. 127:  
  128. 128:  
  129. 129:  
  130. 130:     /**
  131. 131:      * Returns item (\ArrayAccess implementation).
  132. 132:      * @param  int index
  133. 133:      * @return mixed 
  134. 134:      * @throws ArgumentOutOfRangeException
  135. 135:      */
  136. 136:     public function offsetGet($index)
  137. 137:     {
  138. 138:         $index -= $this->base;
  139. 139:         if ($index || $index >= count($this)) {
  140. 140:             throw new ArgumentOutOfRangeException;
  141. 141:         }
  142. 142:  
  143. 143:         return parent::offsetGet($index);
  144. 144:     }
  145. 145:  
  146. 146:  
  147. 147:  
  148. 148:     /**
  149. 149:      * Exists item? (\ArrayAccess implementation).
  150. 150:      * @param  int index
  151. 151:      * @return bool 
  152. 152:      */
  153. 153:     public function offsetExists($index)
  154. 154:     {
  155. 155:         $index -= $this->base;
  156. 156:         return $index >= && $index count($this);
  157. 157:     }
  158. 158:  
  159. 159:  
  160. 160:  
  161. 161:     /**
  162. 162:      * Removes the element at the specified position in this list.
  163. 163:      * @param  int index
  164. 164:      * @return void 
  165. 165:      * @throws NotSupportedException, \ArgumentOutOfRangeException
  166. 166:      */
  167. 167:     public function offsetUnset($index)
  168. 168:     {
  169. 169:         $this->updating();
  170. 170:  
  171. 171:         $index -= $this->base;
  172. 172:         if ($index || $index >= count($this)) {
  173. 173:             throw new ArgumentOutOfRangeException;
  174. 174:         }
  175. 175:  
  176. 176:         $data $this->getArrayCopy();
  177. 177:         array_splice($data(int) $index1);
  178. 178:         $this->setArray($data);
  179. 179:     }
  180. 180: