Skip to content
Browse files

setFromArray helper

  • Loading branch information...
1 parent 71f9046 commit 8fa0bd60d94a518a06371b1c0069f1e4c89da1df @kkamkou kkamkou committed Mar 5, 2012
Showing with 30 additions and 0 deletions.
  1. +18 −0 src/morph/Object.php
  2. +12 −0 unit-tests/TestObject.php
View
18 src/morph/Object.php
@@ -105,6 +105,24 @@ protected function addProperty(\morph\property\Generic $property, $alias = null)
}
/**
+ * Sets properties from the array list
+ *
+ * @param array $set
+ * @return \morph\Object
+ */
+ public function setFromArray(array $set)
+ {
+ foreach ($this->__getPropertySet() as $property) {
+ $propName = $property->getName();
+ if (array_key_exists($propName, $set)) {
+ $this->{$propName} = $set[$propName];
+ }
+ }
+
+ return $this;
+ }
+
+ /**
* Sets the property data for this object
*
* @param array $data
View
12 unit-tests/TestObject.php
@@ -38,11 +38,23 @@ public function testMagicGettersSetter()
public function testSetData()
{
$data = array('testField' => 'value1');
+
+ // the state is DIRTY
$obj = new ForTesting();
$obj->__setData($data);
$this->assertEquals($data['testField'], $obj->testField);
$this->assertEquals(Enum::STATE_DIRTY, $obj->state());
+ // setFromArray, the state is DIRTY
+ $obj->setFromArray($data);
+ $this->assertEquals($data['testField'], $obj->testField);
+ $this->assertEquals(Enum::STATE_DIRTY, $obj->state());
+
+ // setFromArray, the state is NEW
+ $objNew = new ForTesting();
+ $objNew->setFromArray($data);
+ $this->assertEquals($data['testField'], $objNew->testField);
+ $this->assertEquals(Enum::STATE_NEW, $objNew->state());
}
public function testGetData()

0 comments on commit 8fa0bd6

Please sign in to comment.
Something went wrong with that request. Please try again.