Commit 95135f11 by Qiang Xue

...

parent a00afed1
...@@ -179,7 +179,7 @@ class Dictionary extends Object implements \IteratorAggregate, \ArrayAccess, \Co ...@@ -179,7 +179,7 @@ class Dictionary extends Object implements \IteratorAggregate, \ArrayAccess, \Co
* @param mixed $data the data to be copied from, must be an array or an object implementing `Traversable` * @param mixed $data the data to be copied from, must be an array or an object implementing `Traversable`
* @throws Exception if data is neither an array nor an iterator. * @throws Exception if data is neither an array nor an iterator.
*/ */
public function copyFrom($data) public function fromArray($data)
{ {
if (is_array($data) || $data instanceof \Traversable) { if (is_array($data) || $data instanceof \Traversable) {
if ($this->_d !== array()) { if ($this->_d !== array()) {
...@@ -211,7 +211,7 @@ class Dictionary extends Object implements \IteratorAggregate, \ArrayAccess, \Co ...@@ -211,7 +211,7 @@ class Dictionary extends Object implements \IteratorAggregate, \ArrayAccess, \Co
* @param mixed $data the data to be merged with, must be an array or object implementing Traversable * @param mixed $data the data to be merged with, must be an array or object implementing Traversable
* @param boolean $recursive whether the merging should be recursive. * @param boolean $recursive whether the merging should be recursive.
* *
* @throws CException If data is neither an array nor an iterator. * @throws Exception If data is neither an array nor an iterator.
*/ */
public function mergeWith($data, $recursive = true) public function mergeWith($data, $recursive = true)
{ {
......
...@@ -342,4 +342,27 @@ class Object ...@@ -342,4 +342,27 @@ class Object
return $object; return $object;
} }
/**
* Configures the object properties with the specified array.
* @param array $array name-value pairs to be used to initialize the properties of this object.
* @return Object the object itself
*/
public function fromArray($array)
{
foreach ($array as $name => $value) {
$this->$name = $value;
}
return $this;
}
/**
* Returns the object in terms of an array.
* The default implementation will return the result of PHP function `get_object_vars()`.
* @return array the array representation of this object.
*/
public function toArray()
{
return get_object_vars($this);
}
} }
...@@ -240,7 +240,7 @@ class Vector extends Object implements \IteratorAggregate, \ArrayAccess, \Counta ...@@ -240,7 +240,7 @@ class Vector extends Object implements \IteratorAggregate, \ArrayAccess, \Counta
* @param mixed $data the data to be copied from, must be an array or an object implementing `Traversable` * @param mixed $data the data to be copied from, must be an array or an object implementing `Traversable`
* @throws Exception if data is neither an array nor an object implementing `Traversable`. * @throws Exception if data is neither an array nor an object implementing `Traversable`.
*/ */
public function copyFrom($data) public function fromArray($data)
{ {
if (is_array($data) || $data instanceof \Traversable) { if (is_array($data) || $data instanceof \Traversable) {
if ($this->_c > 0) { if ($this->_c > 0) {
......
...@@ -83,17 +83,17 @@ class DictionaryTest extends \yiiunit\TestCase ...@@ -83,17 +83,17 @@ class DictionaryTest extends \yiiunit\TestCase
$this->assertFalse($this->dictionary->contains('key3')); $this->assertFalse($this->dictionary->contains('key3'));
} }
public function testCopyFrom() public function testFromArray()
{ {
$array=array('key3'=>$this->item3,'key4'=>$this->item1); $array=array('key3'=>$this->item3,'key4'=>$this->item1);
$this->dictionary->copyFrom($array); $this->dictionary->fromArray($array);
$this->assertEquals(2, $this->dictionary->getCount()); $this->assertEquals(2, $this->dictionary->getCount());
$this->assertEquals($this->item3, $this->dictionary['key3']); $this->assertEquals($this->item3, $this->dictionary['key3']);
$this->assertEquals($this->item1, $this->dictionary['key4']); $this->assertEquals($this->item1, $this->dictionary['key4']);
$this->setExpectedException('yii\base\Exception'); $this->setExpectedException('yii\base\Exception');
$this->dictionary->copyFrom($this); $this->dictionary->fromArray($this);
} }
public function testMergeWith() public function testMergeWith()
......
...@@ -110,13 +110,13 @@ class VectorTest extends \yiiunit\TestCase ...@@ -110,13 +110,13 @@ class VectorTest extends \yiiunit\TestCase
$this->assertEquals(-1,$this->vector->indexOf($this->item3)); $this->assertEquals(-1,$this->vector->indexOf($this->item3));
} }
public function testCopyFrom() public function testFromArray()
{ {
$array=array($this->item3,$this->item1); $array=array($this->item3,$this->item1);
$this->vector->copyFrom($array); $this->vector->fromArray($array);
$this->assertTrue(count($array)==2 && $this->vector[0]===$this->item3 && $this->vector[1]===$this->item1); $this->assertTrue(count($array)==2 && $this->vector[0]===$this->item3 && $this->vector[1]===$this->item1);
$this->setExpectedException('yii\base\Exception'); $this->setExpectedException('yii\base\Exception');
$this->vector->copyFrom($this); $this->vector->fromArray($this);
} }
public function testMergeWith() public function testMergeWith()
......
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