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.
93 lines
2.0 KiB
93 lines
2.0 KiB
14 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
12 years ago
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
14 years ago
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
14 years ago
|
namespace yii\base;
|
||
|
|
||
14 years ago
|
/**
|
||
14 years ago
|
* DictionaryIterator implements the SPL `Iterator` interface for [[Dictionary]].
|
||
14 years ago
|
*
|
||
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>
|
||
14 years ago
|
* @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)
|
||
|
{
|
||
14 years ago
|
$this->_d = &$data;
|
||
|
$this->_keys = array_keys($data);
|
||
|
$this->_key = reset($this->_keys);
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
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()
|
||
|
{
|
||
14 years ago
|
$this->_key = reset($this->_keys);
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the key of the current array element.
|
||
14 years ago
|
* 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.
|
||
14 years ago
|
* 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];
|
||
|
}
|
||
|
|
||
|
/**
|
||
14 years ago
|
* Moves the internal pointer to the next element.
|
||
|
* This method is required by the SPL interface `Iterator`.
|
||
14 years ago
|
*/
|
||
|
public function next()
|
||
|
{
|
||
14 years ago
|
$this->_key = next($this->_keys);
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns whether there is an element at current position.
|
||
14 years ago
|
* 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()
|
||
|
{
|
||
14 years ago
|
return $this->_key !== false;
|
||
14 years ago
|
}
|
||
|
}
|