CList реализует класс коллекции с индексами типа integer.
Вы можете получить доступ, добавить, вставить, удалить элемент, используя itemAt, add, insertAt, remove и removeAt. Для получения количества элементов в списке используйте getCount. CList также может использоваться как обычный массив следующим образом,
$list[]=$item; // append at the end
$list[$index]=$item; // $index must be between 0 and $list->Count
unset($list[$index]); // remove the item at $index
if(isset($list[$index])) // if the list has an item at $index
foreach($list as $index=>$item) // traverse each item in the list
$n=count($list); // returns the number of items in the list
Чтобы расширить CList, выполнив дополнительные операции при каждой операции добавления или удаления (например, выполнение проверки типа), переопределите insertAt() и removeAt().
public function copyFrom($data) { if(is_array($data) || ($data instanceof Traversable)) { if($this->_c>0) $this->clear(); if($data instanceof CList) $data=$data->_d; foreach($data as $item) $this->add($item); } elseif($data!==null) throw new CException(Yii::t('yii','List data must be an array or an object implementing Traversable.')); }
Копирует итерируемые данные в список. Примечание: существующие данные в списке будут сначала очищены.
public function insertAt($index,$item) { if(!$this->_r) { if($index===$this->_c) $this->_d[$this->_c++]=$item; elseif($index>=0 && $index<$this->_c) { array_splice($this->_d,$index,0,array($item)); $this->_c++; } else throw new CException(Yii::t('yii','List index "{index}" is out of bound.', array('{index}'=>$index))); } else throw new CException(Yii::t('yii','The list is read only.')); }
Вставляет элемент в указанную позицию. Исходный элемент в этой позиции и последующие смещаются на одну позицию.
public function itemAt($index) { if(isset($this->_d[$index])) return $this->_d[$index]; elseif($index>=0 && $index<$this->_c) // in case the value is null return $this->_d[$index]; else throw new CException(Yii::t('yii','List index "{index}" is out of bound.', array('{index}'=>$index))); }
Возвращает элемент по указанному смещению. Этот метод идентичен offsetGet.
mergeWith() метод
public void mergeWith(mixed $data)
$data
mixed
данные для слияния, должны быть массивом или объектом, реализующим интерфейс Traversable
public function mergeWith($data) { if(is_array($data) || ($data instanceof Traversable)) { if($data instanceof CList) $data=$data->_d; foreach($data as $item) $this->add($item); } elseif($data!==null) throw new CException(Yii::t('yii','List data must be an array or an object implementing Traversable.')); }
Сливает итерируемые данные в список. Новые данные будут добавлены в конец существующих.
public function removeAt($index) { if(!$this->_r) { if($index>=0 && $index<$this->_c) { $this->_c--; if($index===$this->_c) return array_pop($this->_d); else { $item=$this->_d[$index]; array_splice($this->_d,$index,1); return $item; } } else throw new CException(Yii::t('yii','List index "{index}" is out of bound.', array('{index}'=>$index))); } else throw new CException(Yii::t('yii','The list is read only.')); }