CList.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. /**
  3. * This file contains classes implementing list feature.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CList implements an integer-indexed collection class.
  12. *
  13. * You can access, append, insert, remove an item by using
  14. * {@link itemAt}, {@link add}, {@link insertAt}, {@link remove}, and {@link removeAt}.
  15. * To get the number of the items in the list, use {@link getCount}.
  16. * CList can also be used like a regular array as follows,
  17. * <pre>
  18. * $list[]=$item; // append at the end
  19. * $list[$index]=$item; // $index must be between 0 and $list->Count
  20. * unset($list[$index]); // remove the item at $index
  21. * if(isset($list[$index])) // if the list has an item at $index
  22. * foreach($list as $index=>$item) // traverse each item in the list
  23. * $n=count($list); // returns the number of items in the list
  24. * </pre>
  25. *
  26. * To extend CList by doing additional operations with each addition or removal
  27. * operation (e.g. performing type check), override {@link insertAt()}, and {@link removeAt()}.
  28. *
  29. * @property boolean $readOnly Whether this list is read-only or not. Defaults to false.
  30. * @property Iterator $iterator An iterator for traversing the items in the list.
  31. * @property integer $count The number of items in the list.
  32. *
  33. * @author Qiang Xue <qiang.xue@gmail.com>
  34. * @package system.collections
  35. * @since 1.0
  36. */
  37. class CList extends CComponent implements IteratorAggregate,ArrayAccess,Countable
  38. {
  39. /**
  40. * @var array internal data storage
  41. */
  42. private $_d=array();
  43. /**
  44. * @var integer number of items
  45. */
  46. private $_c=0;
  47. /**
  48. * @var boolean whether this list is read-only
  49. */
  50. private $_r=false;
  51. /**
  52. * Constructor.
  53. * Initializes the list with an array or an iterable object.
  54. * @param array $data the initial data. Default is null, meaning no initialization.
  55. * @param boolean $readOnly whether the list is read-only
  56. * @throws CException If data is not null and neither an array nor an iterator.
  57. */
  58. public function __construct($data=null,$readOnly=false)
  59. {
  60. if($data!==null)
  61. $this->copyFrom($data);
  62. $this->setReadOnly($readOnly);
  63. }
  64. /**
  65. * @return boolean whether this list is read-only or not. Defaults to false.
  66. */
  67. public function getReadOnly()
  68. {
  69. return $this->_r;
  70. }
  71. /**
  72. * @param boolean $value whether this list is read-only or not
  73. */
  74. protected function setReadOnly($value)
  75. {
  76. $this->_r=$value;
  77. }
  78. /**
  79. * Returns an iterator for traversing the items in the list.
  80. * This method is required by the interface IteratorAggregate.
  81. * @return Iterator an iterator for traversing the items in the list.
  82. */
  83. public function getIterator()
  84. {
  85. return new CListIterator($this->_d);
  86. }
  87. /**
  88. * Returns the number of items in the list.
  89. * This method is required by Countable interface.
  90. * @return integer number of items in the list.
  91. */
  92. public function count()
  93. {
  94. return $this->getCount();
  95. }
  96. /**
  97. * Returns the number of items in the list.
  98. * @return integer the number of items in the list
  99. */
  100. public function getCount()
  101. {
  102. return $this->_c;
  103. }
  104. /**
  105. * Returns the item at the specified offset.
  106. * This method is exactly the same as {@link offsetGet}.
  107. * @param integer $index the index of the item
  108. * @return mixed the item at the index
  109. * @throws CException if the index is out of the range
  110. */
  111. public function itemAt($index)
  112. {
  113. if(isset($this->_d[$index]))
  114. return $this->_d[$index];
  115. elseif($index>=0 && $index<$this->_c) // in case the value is null
  116. return $this->_d[$index];
  117. else
  118. throw new CException(Yii::t('yii','List index "{index}" is out of bound.',
  119. array('{index}'=>$index)));
  120. }
  121. /**
  122. * Appends an item at the end of the list.
  123. * @param mixed $item new item
  124. * @return integer the zero-based index at which the item is added
  125. */
  126. public function add($item)
  127. {
  128. $this->insertAt($this->_c,$item);
  129. return $this->_c-1;
  130. }
  131. /**
  132. * Inserts an item at the specified position.
  133. * Original item at the position and the next items
  134. * will be moved one step towards the end.
  135. * @param integer $index the specified position.
  136. * @param mixed $item new item
  137. * @throws CException If the index specified exceeds the bound or the list is read-only
  138. */
  139. public function insertAt($index,$item)
  140. {
  141. if(!$this->_r)
  142. {
  143. if($index===$this->_c)
  144. $this->_d[$this->_c++]=$item;
  145. elseif($index>=0 && $index<$this->_c)
  146. {
  147. array_splice($this->_d,$index,0,array($item));
  148. $this->_c++;
  149. }
  150. else
  151. throw new CException(Yii::t('yii','List index "{index}" is out of bound.',
  152. array('{index}'=>$index)));
  153. }
  154. else
  155. throw new CException(Yii::t('yii','The list is read only.'));
  156. }
  157. /**
  158. * Removes an item from the list.
  159. * The list will first search for the item.
  160. * The first item found will be removed from the list.
  161. * @param mixed $item the item to be removed.
  162. * @return integer the index at which the item is being removed
  163. * @throws CException If the item does not exist
  164. */
  165. public function remove($item)
  166. {
  167. if(($index=$this->indexOf($item))>=0)
  168. {
  169. $this->removeAt($index);
  170. return $index;
  171. }
  172. else
  173. return false;
  174. }
  175. /**
  176. * Removes an item at the specified position.
  177. * @param integer $index the index of the item to be removed.
  178. * @return mixed the removed item.
  179. * @throws CException If the index specified exceeds the bound or the list is read-only
  180. */
  181. public function removeAt($index)
  182. {
  183. if(!$this->_r)
  184. {
  185. if($index>=0 && $index<$this->_c)
  186. {
  187. $this->_c--;
  188. if($index===$this->_c)
  189. return array_pop($this->_d);
  190. else
  191. {
  192. $item=$this->_d[$index];
  193. array_splice($this->_d,$index,1);
  194. return $item;
  195. }
  196. }
  197. else
  198. throw new CException(Yii::t('yii','List index "{index}" is out of bound.',
  199. array('{index}'=>$index)));
  200. }
  201. else
  202. throw new CException(Yii::t('yii','The list is read only.'));
  203. }
  204. /**
  205. * Removes all items in the list.
  206. */
  207. public function clear()
  208. {
  209. for($i=$this->_c-1;$i>=0;--$i)
  210. $this->removeAt($i);
  211. }
  212. /**
  213. * @param mixed $item the item
  214. * @return boolean whether the list contains the item
  215. */
  216. public function contains($item)
  217. {
  218. return $this->indexOf($item)>=0;
  219. }
  220. /**
  221. * @param mixed $item the item
  222. * @return integer the index of the item in the list (0 based), -1 if not found.
  223. */
  224. public function indexOf($item)
  225. {
  226. if(($index=array_search($item,$this->_d,true))!==false)
  227. return $index;
  228. else
  229. return -1;
  230. }
  231. /**
  232. * @return array the list of items in array
  233. */
  234. public function toArray()
  235. {
  236. return $this->_d;
  237. }
  238. /**
  239. * Copies iterable data into the list.
  240. * Note, existing data in the list will be cleared first.
  241. * @param mixed $data the data to be copied from, must be an array or object implementing Traversable
  242. * @throws CException If data is neither an array nor a Traversable.
  243. */
  244. public function copyFrom($data)
  245. {
  246. if(is_array($data) || ($data instanceof Traversable))
  247. {
  248. if($this->_c>0)
  249. $this->clear();
  250. if($data instanceof CList)
  251. $data=$data->_d;
  252. foreach($data as $item)
  253. $this->add($item);
  254. }
  255. elseif($data!==null)
  256. throw new CException(Yii::t('yii','List data must be an array or an object implementing Traversable.'));
  257. }
  258. /**
  259. * Merges iterable data into the map.
  260. * New data will be appended to the end of the existing data.
  261. * @param mixed $data the data to be merged with, must be an array or object implementing Traversable
  262. * @throws CException If data is neither an array nor an iterator.
  263. */
  264. public function mergeWith($data)
  265. {
  266. if(is_array($data) || ($data instanceof Traversable))
  267. {
  268. if($data instanceof CList)
  269. $data=$data->_d;
  270. foreach($data as $item)
  271. $this->add($item);
  272. }
  273. elseif($data!==null)
  274. throw new CException(Yii::t('yii','List data must be an array or an object implementing Traversable.'));
  275. }
  276. /**
  277. * Returns whether there is an item at the specified offset.
  278. * This method is required by the interface ArrayAccess.
  279. * @param integer $offset the offset to check on
  280. * @return boolean
  281. */
  282. public function offsetExists($offset)
  283. {
  284. return ($offset>=0 && $offset<$this->_c);
  285. }
  286. /**
  287. * Returns the item at the specified offset.
  288. * This method is required by the interface ArrayAccess.
  289. * @param integer $offset the offset to retrieve item.
  290. * @return mixed the item at the offset
  291. * @throws CException if the offset is invalid
  292. */
  293. public function offsetGet($offset)
  294. {
  295. return $this->itemAt($offset);
  296. }
  297. /**
  298. * Sets the item at the specified offset.
  299. * This method is required by the interface ArrayAccess.
  300. * @param integer $offset the offset to set item
  301. * @param mixed $item the item value
  302. */
  303. public function offsetSet($offset,$item)
  304. {
  305. if($offset===null || $offset===$this->_c)
  306. $this->insertAt($this->_c,$item);
  307. else
  308. {
  309. $this->removeAt($offset);
  310. $this->insertAt($offset,$item);
  311. }
  312. }
  313. /**
  314. * Unsets the item at the specified offset.
  315. * This method is required by the interface ArrayAccess.
  316. * @param integer $offset the offset to unset item
  317. */
  318. public function offsetUnset($offset)
  319. {
  320. $this->removeAt($offset);
  321. }
  322. }