Commit 688870c1 by Qiang Xue

finished Dictionary.

parent 83068e0d
<?php
namespace yii\base;
/**
* CMapIterator class file.
* DictionaryIterator class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
/**
* CMapIterator implements an interator for {@link CMap}.
* DictionaryIterator implements the SPL `Iterator` interface for [[Dictionary]].
*
* It allows CMap to return a new iterator for traversing the items in the map.
* It allows [[Dictionary]] to return a new iterator for data traversing purpose.
* You normally do not use this class directly.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: CMapIterator.php 3186 2011-04-15 22:34:55Z alexander.makarow $
* @package system.collections
* @since 1.0
* @since 2.0
*/
class DictionaryIterator implements \Iterator
{
......@@ -42,23 +40,23 @@ class DictionaryIterator implements \Iterator
*/
public function __construct(&$data)
{
$this->_d=&$data;
$this->_keys=array_keys($data);
$this->_key=reset($this->_keys);
$this->_d = &$data;
$this->_keys = array_keys($data);
$this->_key = reset($this->_keys);
}
/**
* Rewinds internal array pointer.
* This method is required by the interface Iterator.
* Rewinds the index of the current item.
* This method is required by the SPL interface `Iterator`.
*/
public function rewind()
{
$this->_key=reset($this->_keys);
$this->_key = reset($this->_keys);
}
/**
* Returns the key of the current array element.
* This method is required by the interface Iterator.
* This method is required by the SPL interface `Iterator`.
* @return mixed the key of the current array element
*/
public function key()
......@@ -68,7 +66,7 @@ class DictionaryIterator implements \Iterator
/**
* Returns the current array element.
* This method is required by the interface Iterator.
* This method is required by the SPL interface `Iterator`.
* @return mixed the current array element
*/
public function current()
......@@ -77,21 +75,21 @@ class DictionaryIterator implements \Iterator
}
/**
* Moves the internal pointer to the next array element.
* This method is required by the interface Iterator.
* Moves the internal pointer to the next element.
* This method is required by the SPL interface `Iterator`.
*/
public function next()
{
$this->_key=next($this->_keys);
$this->_key = next($this->_keys);
}
/**
* Returns whether there is an element at current position.
* This method is required by the interface Iterator.
* @return boolean
* This method is required by the SPL interface `Iterator`.
* @return boolean whether there is an item at current position.
*/
public function valid()
{
return $this->_key!==false;
return $this->_key !== false;
}
}
......@@ -58,7 +58,7 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
* Constructor.
* Initializes the vector with an array or an iterable object.
* @param mixed $data the initial data to be populated into the vector.
* This can be an array or an iterable object. If null, the vector will be set as empty.
* This can be an array or an iterable object.
* @param boolean $readOnly whether the vector should be marked as read-only.
* @throws Exception if data is not well formed (neither an array nor an iterable object)
*/
......@@ -115,9 +115,10 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
elseif ($index >= 0 && $index < $this->_c) { // in case the value is null
return $this->_d[$index];
}
else {
throw new Exception('Index out of range: ' . $index);
}
}
/**
* Appends an item at the end of the vector.
......@@ -149,10 +150,14 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
array_splice($this->_d, $index, 0, array($item));
$this->_c++;
}
else {
throw new Exception('Index out of range: ' . $index);
}
}
else {
throw new Exception('Vector is read only.');
}
}
/**
* Removes an item from the vector.
......@@ -170,9 +175,10 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
$this->removeAt($index);
return $index;
}
else
else {
return false;
}
}
/**
* Removes an item at the specified position.
......@@ -194,10 +200,14 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
return $item;
}
}
else {
throw new Exception('Index out of range: ' . $index);
}
}
else {
throw new Exception('Vector is read only.');
}
}
/**
* Removes all items from the vector.
......@@ -261,8 +271,10 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
$this->add($item);
}
}
else {
throw new Exception('Data must be either an array or an object implementing Traversable.');
}
}
/**
* Merges iterable data into the vector.
......@@ -280,8 +292,10 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
$this->add($item);
}
}
else {
throw new Exception('Data must be either an array or an object implementing Traversable.');
}
}
/**
* Returns a value indicating whether there is an item at the specified offset.
......@@ -299,6 +313,7 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
* Returns the item at the specified offset.
* This method is required by the SPL interface `ArrayAccess`.
* It is implicitly called when you use something like `$value = $vector[$index];`.
* This is equivalent to [[itemAt]].
* @param integer $offset the offset to retrieve item.
* @return mixed the item at the offset
* @throws Exception if the offset is out of range
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment