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