You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.0 KiB

14 years ago
<?php
/**
* DictionaryIterator class file.
14 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
14 years ago
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
14 years ago
/**
* DictionaryIterator implements the SPL `Iterator` interface for [[Dictionary]].
14 years ago
*
* It allows [[Dictionary]] to return a new iterator for data traversing purpose.
* You normally do not use this class directly.
14 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
14 years ago
*/
class DictionaryIterator implements \Iterator
{
/**
* @var array the data to be iterated through
*/
private $_d;
/**
* @var array list of keys in the map
*/
private $_keys;
/**
* @var mixed current key
*/
private $_key;
/**
* Constructor.
* @param array $data the data to be iterated through
*/
public function __construct(&$data)
{
$this->_d = &$data;
$this->_keys = array_keys($data);
$this->_key = reset($this->_keys);
14 years ago
}
/**
* Rewinds the index of the current item.
* This method is required by the SPL interface `Iterator`.
14 years ago
*/
public function rewind()
{
$this->_key = reset($this->_keys);
14 years ago
}
/**
* Returns the key of the current array element.
* This method is required by the SPL interface `Iterator`.
14 years ago
* @return mixed the key of the current array element
*/
public function key()
{
return $this->_key;
}
/**
* Returns the current array element.
* This method is required by the SPL interface `Iterator`.
14 years ago
* @return mixed the current array element
*/
public function current()
{
return $this->_d[$this->_key];
}
/**
* Moves the internal pointer to the next element.
* This method is required by the SPL interface `Iterator`.
14 years ago
*/
public function next()
{
$this->_key = next($this->_keys);
14 years ago
}
/**
* Returns whether there is an element at current position.
* This method is required by the SPL interface `Iterator`.
* @return boolean whether there is an item at current position.
14 years ago
*/
public function valid()
{
return $this->_key !== false;
14 years ago
}
}