Commit 19430d8d by Qiang Xue

w

parent d2159b12
...@@ -34,11 +34,6 @@ namespace yii\base; ...@@ -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, ...@@ -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,16 +112,11 @@ class Dictionary extends Component implements \IteratorAggregate, \ArrayAccess, ...@@ -119,16 +112,11 @@ 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;
}
else {
$this->_d[$key] = $value;
}
} }
else { else {
throw new Exception('Dictionary is read only.'); $this->_d[$key] = $value;
} }
} }
...@@ -140,19 +128,14 @@ class Dictionary extends Component implements \IteratorAggregate, \ArrayAccess, ...@@ -140,19 +128,14 @@ 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]); return $value;
return $value;
}
else { // the value is null
unset($this->_d[$key]);
return null;
}
} }
else { else { // the value is null
throw new Exception('Dictionary is read only.'); unset($this->_d[$key]);
return null;
} }
} }
......
...@@ -40,11 +40,6 @@ namespace yii\base; ...@@ -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 ...@@ -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,20 +134,15 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou ...@@ -141,20 +134,15 @@ 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; }
} elseif ($index >= 0 && $index < $this->_c) {
elseif ($index >= 0 && $index < $this->_c) { array_splice($this->_d, $index, 0, array($item));
array_splice($this->_d, $index, 0, array($item)); $this->_c++;
$this->_c++;
}
else {
throw new Exception('Index out of range: ' . $index);
}
} }
else { 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 ...@@ -187,24 +175,19 @@ 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) { return array_pop($this->_d);
return array_pop($this->_d);
}
else {
$item = $this->_d[$index];
array_splice($this->_d, $index, 1);
return $item;
}
} }
else { else {
throw new Exception('Index out of range: ' . $index); $item = $this->_d[$index];
array_splice($this->_d, $index, 1);
return $item;
} }
} }
else { else {
throw new Exception('Vector is read only.'); throw new Exception('Index out of range: ' . $index);
} }
} }
......
...@@ -37,12 +37,6 @@ class DictionaryTest extends \yii\test\TestCase ...@@ -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 ...@@ -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 ...@@ -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();
......
...@@ -37,14 +37,6 @@ class VectorTest extends \yii\test\TestCase ...@@ -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 ...@@ -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 ...@@ -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();
......
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