Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

setFromArray method. And the json issue.

  • Loading branch information...
commit acf64190f33eb25cd449f25ddf8a1d955f092531 1 parent c5ac7fc
@kkamkou authored
View
28 src/morph/Object.php
@@ -99,12 +99,30 @@ protected function addProperty(\morph\property\Generic $property, $alias = null)
{
$this->propertySet[$property->getName()] = $property;
if (null !== $alias) {
- $this->propertySet->setStorageName($property->getName(), $alias);
+ $this->propertySet->setStorageName($property->getName(), $alias);
}
return $this;
}
/**
+ * 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
@@ -139,7 +157,7 @@ public function __getData()
}
$data['_ns'] = \get_class($this);
foreach($this->propertySet as $property) {
- $storageName = $this->propertySet->getStorageName($property->getName());
+ $storageName = $this->propertySet->getStorageName($property->getName());
$data[$storageName] = $property->__getRawValue();
}
return $data;
@@ -150,7 +168,7 @@ public function __getData()
*/
public function __getPropertySet()
{
- return $this->propertySet;
+ return $this->propertySet;
}
// ********************** //
@@ -278,7 +296,7 @@ public function fetchAll()
*/
public function delete()
{
- return Storage::instance()->delete($this);
+ return Storage::instance()->delete($this);
}
// ***************** //
@@ -303,7 +321,7 @@ public function __toString()
// iterate through all the properties this object has and print them out
foreach ($this->propertySet as $name => $property) {
- $data[$name] = (string)$property;
+ $data[$name] = $property->__toString();
}
return \json_encode($data);
View
14 src/morph/property/Generic.php
@@ -32,27 +32,25 @@ class Generic
* @var mixed
*/
protected $value;
-
+
/**
* The current state of this property
- *
+ *
* @see Morph_Enum::STATE_*
* @var string
*/
protected $state = \morph\Enum::STATE_NEW;
/**
- *
- * @param $name
- * @param $default
- * @return ar_Field
+ * @param string $name
+ * @param mixed $default
*/
public function __construct($name, $default = null)
{
$this->name = $name;
$this->value = $default;
}
-
+
/**
* @return string
*/
@@ -126,6 +124,6 @@ public function __setRawValue($value, $state = null)
*/
public function __toString()
{
- return (string)$this->getValue();
+ return $this->getValue();
}
}
View
25 unit-tests/TestObject.php
@@ -13,8 +13,6 @@
*/
class TestObject extends \PHPUnit_Framework_TestCase
{
-
-
public function testGetCollection()
{
$obj = new ForTesting();
@@ -40,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()
@@ -75,8 +85,13 @@ public function test__toString()
{
$expected = '{"Id":null,"State":"New","testField":"TEST"}';
$obj = new ForTesting();
+
$obj->testField = 'TEST';
- $this->assertEquals($expected, $obj->__toString());
- }
+ $this->assertEquals($expected, (string)$obj);
-}
+ // null test
+ $expected = '{"Id":null,"State":"New","testField":null}';
+ $obj->testField = null;
+ $this->assertEquals($expected, (string)$obj);
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.