Browse Source

w

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

19
framework/base/Dictionary.php

@ -34,11 +34,6 @@ namespace yii\base;
class Dictionary extends Component implements \IteratorAggregate, \ArrayAccess, \Countable 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 * @var array internal data storage
*/ */
private $_d = array(); private $_d = array();
@ -48,15 +43,13 @@ class Dictionary extends Component implements \IteratorAggregate, \ArrayAccess,
* Initializes the dictionary with an array or an iterable object. * Initializes the dictionary with an array or an iterable object.
* @param mixed $data the initial data to be populated into the dictionary. * @param mixed $data the initial data to be populated into the dictionary.
* This can be an array or an iterable object. * 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) * @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()) { if ($data !== array()) {
$this->copyFrom($data); $this->copyFrom($data);
} }
$this->readOnly = $readOnly;
} }
/** /**
@ -119,7 +112,6 @@ class Dictionary extends Component implements \IteratorAggregate, \ArrayAccess,
*/ */
public function add($key, $value) public function add($key, $value)
{ {
if (!$this->readOnly) {
if ($key === null) { if ($key === null) {
$this->_d[] = $value; $this->_d[] = $value;
} }
@ -127,10 +119,6 @@ class Dictionary extends Component implements \IteratorAggregate, \ArrayAccess,
$this->_d[$key] = $value; $this->_d[$key] = $value;
} }
} }
else {
throw new Exception('Dictionary is read only.');
}
}
/** /**
* Removes an item from the dictionary by its key. * Removes an item from the dictionary by its key.
@ -140,7 +128,6 @@ class Dictionary extends Component implements \IteratorAggregate, \ArrayAccess,
*/ */
public function remove($key) public function remove($key)
{ {
if (!$this->readOnly) {
if (isset($this->_d[$key])) { if (isset($this->_d[$key])) {
$value = $this->_d[$key]; $value = $this->_d[$key];
unset($this->_d[$key]); unset($this->_d[$key]);
@ -151,10 +138,6 @@ class Dictionary extends Component implements \IteratorAggregate, \ArrayAccess,
return null; return null;
} }
} }
else {
throw new Exception('Dictionary is read only.');
}
}
/** /**
* Removes all items in the dictionary. * Removes all items in the dictionary.

19
framework/base/Vector.php

@ -40,11 +40,6 @@ namespace yii\base;
class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Countable 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 * @var array internal data storage
*/ */
private $_d = array(); 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. * 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. * 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) * @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()) { if ($data !== array()) {
$this->copyFrom($data); $this->copyFrom($data);
} }
$this->readOnly = $readOnly;
} }
/** /**
@ -141,7 +134,6 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
*/ */
public function insertAt($index, $item) public function insertAt($index, $item)
{ {
if (!$this->readOnly) {
if ($index === $this->_c) { if ($index === $this->_c) {
$this->_d[$this->_c++] = $item; $this->_d[$this->_c++] = $item;
} }
@ -153,10 +145,6 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
throw new Exception('Index out of range: ' . $index); throw new Exception('Index out of range: ' . $index);
} }
} }
else {
throw new Exception('Vector is read only.');
}
}
/** /**
* Removes an item from the vector. * Removes an item from the vector.
@ -187,7 +175,6 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
*/ */
public function removeAt($index) public function removeAt($index)
{ {
if (!$this->readOnly) {
if ($index >= 0 && $index < $this->_c) { if ($index >= 0 && $index < $this->_c) {
$this->_c--; $this->_c--;
if ($index === $this->_c) { if ($index === $this->_c) {
@ -203,10 +190,6 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
throw new Exception('Index out of range: ' . $index); throw new Exception('Index out of range: ' . $index);
} }
} }
else {
throw new Exception('Vector is read only.');
}
}
/** /**
* Removes all items from the vector. * Removes all items from the vector.

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

@ -37,12 +37,6 @@ class DictionaryTest extends \yii\test\TestCase
$this->assertEquals(2,$dictionary2->getCount()); $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() public function testGetCount()
{ {
$this->assertEquals(2,$this->dictionary->getCount()); $this->assertEquals(2,$this->dictionary->getCount());
@ -63,13 +57,6 @@ class DictionaryTest extends \yii\test\TestCase
$this->assertTrue($this->dictionary->contains('key3')); $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() public function testRemove()
{ {
$this->dictionary->remove('key1'); $this->dictionary->remove('key1');
@ -78,13 +65,6 @@ class DictionaryTest extends \yii\test\TestCase
$this->assertTrue($this->dictionary->remove('unknown key')===null); $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() public function testClear()
{ {
$this->dictionary->clear(); $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()); $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() public function testGetCount()
{ {
$this->assertEquals(2,$this->vector->getCount()); $this->assertEquals(2,$this->vector->getCount());
@ -70,13 +62,6 @@ class VectorTest extends \yii\test\TestCase
$this->vector->insertAt(4,$this->item3); $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() public function testRemove()
{ {
$this->vector->remove($this->item1); $this->vector->remove($this->item1);
@ -99,13 +84,6 @@ class VectorTest extends \yii\test\TestCase
$this->vector->removeAt(2); $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() public function testClear()
{ {
$this->vector->clear(); $this->vector->clear();

Loading…
Cancel
Save