Browse Source

w

tags/2.0.0-beta
Qiang Xue 13 years ago
parent
commit
19430d8d9e
  1. 39
      framework/base/Dictionary.php
  2. 49
      framework/base/Vector.php
  3. 20
      tests/unit/framework/base/DictionaryTest.php
  4. 22
      tests/unit/framework/base/VectorTest.php

39
framework/base/Dictionary.php

@ -34,11 +34,6 @@ namespace yii\base;
class Dictionary extends Component implements \IteratorAggregate, \ArrayAccess, \Countable
{
/**
* @var boolean whether this vector is read-only or not.
* If the vector is read-only, adding or moving items will throw an exception.
*/
public $readOnly;
/**
* @var array internal data storage
*/
private $_d = array();
@ -48,15 +43,13 @@ class Dictionary extends Component implements \IteratorAggregate, \ArrayAccess,
* Initializes the dictionary with an array or an iterable object.
* @param mixed $data the initial data to be populated into the dictionary.
* This can be an array or an iterable object.
* @param boolean $readOnly whether the dictionary is read-only
* @throws Exception if data is not well formed (neither an array nor an iterable object)
*/
public function __construct($data = array(), $readOnly = false)
public function __construct($data = array())
{
if ($data !== array()) {
$this->copyFrom($data);
}
$this->readOnly = $readOnly;
}
/**
@ -119,16 +112,11 @@ class Dictionary extends Component implements \IteratorAggregate, \ArrayAccess,
*/
public function add($key, $value)
{
if (!$this->readOnly) {
if ($key === null) {
$this->_d[] = $value;
}
else {
$this->_d[$key] = $value;
}
if ($key === null) {
$this->_d[] = $value;
}
else {
throw new Exception('Dictionary is read only.');
$this->_d[$key] = $value;
}
}
@ -140,19 +128,14 @@ class Dictionary extends Component implements \IteratorAggregate, \ArrayAccess,
*/
public function remove($key)
{
if (!$this->readOnly) {
if (isset($this->_d[$key])) {
$value = $this->_d[$key];
unset($this->_d[$key]);
return $value;
}
else { // the value is null
unset($this->_d[$key]);
return null;
}
if (isset($this->_d[$key])) {
$value = $this->_d[$key];
unset($this->_d[$key]);
return $value;
}
else {
throw new Exception('Dictionary is read only.');
else { // the value is null
unset($this->_d[$key]);
return null;
}
}

49
framework/base/Vector.php

@ -40,11 +40,6 @@ namespace yii\base;
class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Countable
{
/**
* @var boolean whether this vector is read-only or not.
* If the vector is read-only, adding or moving items will throw an exception.
*/
public $readOnly;
/**
* @var array internal data storage
*/
private $_d = array();
@ -58,15 +53,13 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
* 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.
* @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)
*/
public function __construct($data = array(), $readOnly = false)
public function __construct($data = array())
{
if ($data !== array()) {
$this->copyFrom($data);
}
$this->readOnly = $readOnly;
}
/**
@ -141,20 +134,15 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
*/
public function insertAt($index, $item)
{
if (!$this->readOnly) {
if ($index === $this->_c) {
$this->_d[$this->_c++] = $item;
}
elseif ($index >= 0 && $index < $this->_c) {
array_splice($this->_d, $index, 0, array($item));
$this->_c++;
}
else {
throw new Exception('Index out of range: ' . $index);
}
if ($index === $this->_c) {
$this->_d[$this->_c++] = $item;
}
elseif ($index >= 0 && $index < $this->_c) {
array_splice($this->_d, $index, 0, array($item));
$this->_c++;
}
else {
throw new Exception('Vector is read only.');
throw new Exception('Index out of range: ' . $index);
}
}
@ -187,24 +175,19 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
*/
public function removeAt($index)
{
if (!$this->readOnly) {
if ($index >= 0 && $index < $this->_c) {
$this->_c--;
if ($index === $this->_c) {
return array_pop($this->_d);
}
else {
$item = $this->_d[$index];
array_splice($this->_d, $index, 1);
return $item;
}
if ($index >= 0 && $index < $this->_c) {
$this->_c--;
if ($index === $this->_c) {
return array_pop($this->_d);
}
else {
throw new Exception('Index out of range: ' . $index);
$item = $this->_d[$index];
array_splice($this->_d, $index, 1);
return $item;
}
}
else {
throw new Exception('Vector is read only.');
throw new Exception('Index out of range: ' . $index);
}
}

20
tests/unit/framework/base/DictionaryTest.php

@ -37,12 +37,6 @@ class DictionaryTest extends \yii\test\TestCase
$this->assertEquals(2,$dictionary2->getCount());
}
public function testReadOnly()
{
$dictionary = new \yii\base\Dictionary(array(), true);
self::assertEquals(true, $dictionary->readOnly, 'List is not read-only');
}
public function testGetCount()
{
$this->assertEquals(2,$this->dictionary->getCount());
@ -63,13 +57,6 @@ class DictionaryTest extends \yii\test\TestCase
$this->assertTrue($this->dictionary->contains('key3'));
}
public function testCanNotAddWhenReadOnly()
{
$dictionary = new \yii\base\Dictionary(array(), true);
$this->setExpectedException('yii\base\Exception');
$dictionary->add('key', 'value');
}
public function testRemove()
{
$this->dictionary->remove('key1');
@ -78,13 +65,6 @@ class DictionaryTest extends \yii\test\TestCase
$this->assertTrue($this->dictionary->remove('unknown key')===null);
}
public function testCanNotRemoveWhenReadOnly()
{
$dictionary = new \yii\base\Dictionary(array('key' => 'value'), true);
$this->setExpectedException('yii\base\Exception');
$dictionary->remove('key');
}
public function testClear()
{
$this->dictionary->clear();

22
tests/unit/framework/base/VectorTest.php

@ -37,14 +37,6 @@ class VectorTest extends \yii\test\TestCase
$this->assertEquals(2,$vector2->getCount());
}
public function testReadOnly()
{
$vector = new \yii\base\Vector(array(), true);
$this->assertEquals(true, $vector->readOnly, 'List is not read-only');
$vector = new \yii\base\Vector(array(), false);
$this->assertEquals(false, $vector->readOnly, 'List is read-only');
}
public function testGetCount()
{
$this->assertEquals(2,$this->vector->getCount());
@ -70,13 +62,6 @@ class VectorTest extends \yii\test\TestCase
$this->vector->insertAt(4,$this->item3);
}
public function testCanNotInsertWhenReadOnly()
{
$vector = new \yii\base\Vector(array(), true);
$this->setExpectedException('yii\base\Exception');
$vector->insertAt(1, 2);
}
public function testRemove()
{
$this->vector->remove($this->item1);
@ -99,13 +84,6 @@ class VectorTest extends \yii\test\TestCase
$this->vector->removeAt(2);
}
public function testCanNotRemoveWhenReadOnly()
{
$vector = new \yii\base\Vector(array(1, 2, 3), true);
$this->setExpectedException('yii\base\Exception');
$vector->removeAt(2);
}
public function testClear()
{
$this->vector->clear();

Loading…
Cancel
Save