Skip to content

Commit d460aa0

Browse files
committed
Implemented generic __set_state
1 parent cb80b27 commit d460aa0

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/Kore/DataObject/DataObject.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,15 @@ private function cloneArray(array &$array)
8686
}
8787
}
8888
}
89+
90+
/**
91+
* Restore object from var_export
92+
*
93+
* @param array $values
94+
* @return DataObject
95+
*/
96+
public static function __set_state(array $values)
97+
{
98+
return new static($values);
99+
}
89100
}

tests/Kore/DataObject/DataObjectTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,27 @@ public function testCloneAggregateInDeepArray()
137137
$clone->property[0][0]
138138
);
139139
}
140+
141+
public function testSetState()
142+
{
143+
$struct = new TestDataObject();
144+
$struct->property = 42;
145+
146+
$restored = eval("return " . var_export($struct, true) . ';');
147+
148+
$this->assertEquals($struct, $restored);
149+
$this->assertNotSame($struct, $restored);
150+
}
151+
152+
/**
153+
* @expectedException \OutOfRangeException
154+
*/
155+
public function testFailOnInvalidSetState()
156+
{
157+
TestDataObject::__set_state(
158+
array(
159+
'invalid' => 42,
160+
)
161+
);
162+
}
140163
}

0 commit comments

Comments
 (0)