Last active
August 19, 2016 07:37
-
-
Save m3g4p0p/d4d8dd3b1b9cbe42564c3f603eafe8fc to your computer and use it in GitHub Desktop.
Store and restore the states of objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Optional convenience class. Objects extending the | |
* StateHandler class can register with a StateArchivist | |
* instance; then calling remember() or restore() on the | |
* archivist will be propagated to all registered objects. | |
*/ | |
class StateArchivist { | |
/** | |
* The array of objects | |
* @var array | |
*/ | |
private $objects = []; | |
/** | |
* Register an object that extends the StateHandler | |
* class and initially have it remember its state | |
* (unless specified otherwise). | |
* @param StateHandler $object | |
* @param boolean $remember | |
*/ | |
public function register( | |
StateHandler $object, | |
$remember = true | |
) { | |
if ($remember) { | |
$object->remember(); | |
} | |
$this->objects[] = $object; | |
} | |
/** | |
* Call the remember() function on | |
* all registered objects | |
*/ | |
public function remember() { | |
foreach ($this->objects as $object) { | |
$object->remember(); | |
} | |
} | |
/** | |
* Call the restore function on all objects | |
*/ | |
public function restore() { | |
foreach ($this->objects as $object) { | |
$object->restore(); | |
} | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Objects extending this class can remember | |
* and later restore their state | |
*/ | |
class StateHandler { | |
/** | |
* The array of states | |
* @var array | |
*/ | |
private $states = []; | |
/** | |
* Optionally takes an instance of a StateArchivist | |
* as parameter where it immediately registers itself | |
* @param StateArchivist|null $archivist | |
*/ | |
public function __construct(StateArchivist $archivist = null) { | |
if ($archivist) { | |
$archivist->register($this); | |
} | |
} | |
/** | |
* Store the current state | |
*/ | |
public function remember() { | |
$state = get_object_vars($this); | |
unset($state['states']); | |
$this->states[] = $state; | |
} | |
/** | |
* Restore the last state | |
*/ | |
public function restore() { | |
$state = array_pop($this->states); | |
if (!$state) return; | |
foreach ($state as $key => $value) { | |
$this->$key = $value; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample usage:
License
MIT