Skip to content

Commit

Permalink
Implemented generic __set_state
Browse files Browse the repository at this point in the history
  • Loading branch information
kore committed Jun 24, 2014
1 parent cb80b27 commit d460aa0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Kore/DataObject/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,15 @@ private function cloneArray(array &$array)
}
}
}

/**
* Restore object from var_export
*
* @param array $values
* @return DataObject
*/
public static function __set_state(array $values)
{
return new static($values);
}
}
23 changes: 23 additions & 0 deletions tests/Kore/DataObject/DataObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,27 @@ public function testCloneAggregateInDeepArray()
$clone->property[0][0]
);
}

public function testSetState()
{
$struct = new TestDataObject();
$struct->property = 42;

$restored = eval("return " . var_export($struct, true) . ';');

$this->assertEquals($struct, $restored);
$this->assertNotSame($struct, $restored);
}

/**
* @expectedException \OutOfRangeException
*/
public function testFailOnInvalidSetState()
{
TestDataObject::__set_state(
array(
'invalid' => 42,
)
);
}
}

0 comments on commit d460aa0

Please sign in to comment.