Commit 688870c1 by Qiang Xue

finished Dictionary.

parent 83068e0d
<?php <?php
namespace yii\base;
/** /**
* CMapIterator class file. * DictionaryIterator class file.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.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/ * @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> * @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: CMapIterator.php 3186 2011-04-15 22:34:55Z alexander.makarow $ * @since 2.0
* @package system.collections
* @since 1.0
*/ */
class DictionaryIterator implements \Iterator class DictionaryIterator implements \Iterator
{ {
...@@ -42,23 +40,23 @@ class DictionaryIterator implements \Iterator ...@@ -42,23 +40,23 @@ class DictionaryIterator implements \Iterator
*/ */
public function __construct(&$data) public function __construct(&$data)
{ {
$this->_d=&$data; $this->_d = &$data;
$this->_keys=array_keys($data); $this->_keys = array_keys($data);
$this->_key=reset($this->_keys); $this->_key = reset($this->_keys);
} }
/** /**
* Rewinds internal array pointer. * Rewinds the index of the current item.
* This method is required by the interface Iterator. * This method is required by the SPL interface `Iterator`.
*/ */
public function rewind() public function rewind()
{ {
$this->_key=reset($this->_keys); $this->_key = reset($this->_keys);
} }
/** /**
* Returns the key of the current array element. * 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 * @return mixed the key of the current array element
*/ */
public function key() public function key()
...@@ -68,7 +66,7 @@ class DictionaryIterator implements \Iterator ...@@ -68,7 +66,7 @@ class DictionaryIterator implements \Iterator
/** /**
* Returns the current array element. * 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 * @return mixed the current array element
*/ */
public function current() public function current()
...@@ -77,21 +75,21 @@ class DictionaryIterator implements \Iterator ...@@ -77,21 +75,21 @@ class DictionaryIterator implements \Iterator
} }
/** /**
* Moves the internal pointer to the next array element. * Moves the internal pointer to the next element.
* This method is required by the interface Iterator. * This method is required by the SPL interface `Iterator`.
*/ */
public function next() public function next()
{ {
$this->_key=next($this->_keys); $this->_key = next($this->_keys);
} }
/** /**
* Returns whether there is an element at current position. * Returns whether there is an element at current position.
* This method is required by the interface Iterator. * This method is required by the SPL interface `Iterator`.
* @return boolean * @return boolean whether there is an item at current position.
*/ */
public function valid() public function valid()
{ {
return $this->_key!==false; return $this->_key !== false;
} }
} }
...@@ -58,7 +58,7 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou ...@@ -58,7 +58,7 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
* Constructor. * Constructor.
* Initializes the vector with an array or an iterable object. * Initializes the vector with an array or an iterable object.
* @param mixed $data the initial data to be populated into the vector. * @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. * @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) * @throws Exception if data is not well formed (neither an array nor an iterable object)
*/ */
...@@ -115,8 +115,9 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou ...@@ -115,8 +115,9 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
elseif ($index >= 0 && $index < $this->_c) { // in case the value is null elseif ($index >= 0 && $index < $this->_c) { // in case the value is null
return $this->_d[$index]; return $this->_d[$index];
} }
else {
throw new Exception('Index out of range: ' . $index); throw new Exception('Index out of range: ' . $index);
}
} }
/** /**
...@@ -149,9 +150,13 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou ...@@ -149,9 +150,13 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
array_splice($this->_d, $index, 0, array($item)); array_splice($this->_d, $index, 0, array($item));
$this->_c++; $this->_c++;
} }
throw new Exception('Index out of range: ' . $index); else {
throw new Exception('Index out of range: ' . $index);
}
}
else {
throw new Exception('Vector is read only.');
} }
throw new Exception('Vector is read only.');
} }
/** /**
...@@ -170,8 +175,9 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou ...@@ -170,8 +175,9 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
$this->removeAt($index); $this->removeAt($index);
return $index; return $index;
} }
else else {
return false; return false;
}
} }
/** /**
...@@ -194,9 +200,13 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou ...@@ -194,9 +200,13 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
return $item; return $item;
} }
} }
throw new Exception('Index out of range: ' . $index); else {
throw new Exception('Index out of range: ' . $index);
}
}
else {
throw new Exception('Vector is read only.');
} }
throw new Exception('Vector is read only.');
} }
/** /**
...@@ -261,7 +271,9 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou ...@@ -261,7 +271,9 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
$this->add($item); $this->add($item);
} }
} }
throw new Exception('Data must be either an array or an object implementing Traversable.'); else {
throw new Exception('Data must be either an array or an object implementing Traversable.');
}
} }
/** /**
...@@ -280,7 +292,9 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou ...@@ -280,7 +292,9 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
$this->add($item); $this->add($item);
} }
} }
throw new Exception('Data must be either an array or an object implementing Traversable.'); else {
throw new Exception('Data must be either an array or an object implementing Traversable.');
}
} }
/** /**
...@@ -299,6 +313,7 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou ...@@ -299,6 +313,7 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
* Returns the item at the specified offset. * Returns the item at the specified offset.
* This method is required by the SPL interface `ArrayAccess`. * This method is required by the SPL interface `ArrayAccess`.
* It is implicitly called when you use something like `$value = $vector[$index];`. * 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. * @param integer $offset the offset to retrieve item.
* @return mixed the item at the offset * @return mixed the item at the offset
* @throws Exception if the offset is out of range * @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