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