1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
<?php
namespace oroboros\core\traits\patterns\behavioral;
trait State {
private $_state;
private $_valid_states = [];
protected function _registerState($state) {
if (!is_string($state)) {
throw new \oroboros\lib\exception\LibraryException('Invalid parameters passed at ' . __LINE__ . ' of ' . __METHOD__, self::EXCEPTION_CODE_BAD_PARAMETERS);
}
if (!in_array($state, $this->_valid_states)) {
$this->_valid_states[] = $state;
return TRUE;
}
return FALSE;
}
protected function _registerStates($states) {
if (!is_array($states) && !is_object($states)) {
throw new \oroboros\lib\exception\LibraryException('Invalid parameters passed at ' . __LINE__ . ' of ' . __METHOD__, self::EXCEPTION_CODE_BAD_PARAMETERS);
}
array_map(function($state) {
$this->_registerState($state);
}, ((is_object($states)) ? get_object_vars($states) : $states));
}
protected function _checkState() {
return $this->_state;
}
protected function _checkValidStates() {
return $this->_valid_states;
}
protected function _updateState($state) {
if (!is_string($state)) {
throw new \oroboros\lib\exception\LibraryException('Invalid parameters passed at ' . __LINE__ . ' of ' . __METHOD__, self::EXCEPTION_CODE_BAD_PARAMETERS);
}
if (!in_array($state, $this->_valid_states)) {
throw new \oroboros\lib\exception\LibraryException('Invalid state: ' . $state . ' specified at ' . __LINE__ . ' of ' . __METHOD__, self::EXCEPTION_CODE_BAD_PARAMETERS);
}
$this->_state = $state;
}
abstract protected function _onStateChange();
}