@@ -8,8 +8,8 @@ discard block |
||
| 8 | 8 | private const NEXT_STATE = 0; |
| 9 | 9 | private const EXEC_ACTION = 1; |
| 10 | 10 | |
| 11 | - protected $sm = array(); |
|
| 12 | - protected $currentState = null; |
|
| 11 | + protected $sm = array(); |
|
| 12 | + protected $currentState = null; |
|
| 13 | 13 | protected $currentEvent = null; |
| 14 | 14 | protected $nextState = null; |
| 15 | 15 | |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | private $eventsQueue = []; |
| 19 | 19 | private $commonTransition = []; |
| 20 | 20 | |
| 21 | - public function __construct(StateMachineBuilder $smb = null) |
|
| 21 | + public function __construct(StateMachineBuilder $smb = null) |
|
| 22 | 22 | { |
| 23 | 23 | if( $smb != null ) |
| 24 | 24 | $sm = $smb->__invoke(); |
@@ -40,7 +40,7 @@ discard block |
||
| 40 | 40 | |
| 41 | 41 | public function addState($state) |
| 42 | 42 | { |
| 43 | - $this->argumentIsValidOrFail($state); |
|
| 43 | + $this->argumentIsValidOrFail($state); |
|
| 44 | 44 | |
| 45 | 45 | $this->setCurrentStateIfThisIsInitialState($state); |
| 46 | 46 | |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | $this->argumentIsValidOrFail($currEvent); |
| 56 | 56 | $this->argumentIsValidOrFail($nextState); |
| 57 | 57 | |
| 58 | - $this->setCurrentStateIfThisIsInitialState($currState); |
|
| 58 | + $this->setCurrentStateIfThisIsInitialState($currState); |
|
| 59 | 59 | |
| 60 | 60 | $this->sm[$currState][$currEvent] = [ |
| 61 | 61 | self::NEXT_STATE => $nextState, |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | $this->commonTransition[$currentEvent] = [ |
| 73 | 73 | self::NEXT_STATE => $nextState, |
| 74 | 74 | self::EXEC_ACTION => $execAction |
| 75 | - ]; |
|
| 75 | + ]; |
|
| 76 | 76 | return $this; |
| 77 | 77 | } |
| 78 | 78 | |
@@ -178,11 +178,11 @@ discard block |
||
| 178 | 178 | } |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | - public function dumpStates(){ |
|
| 182 | - var_dump(array_keys($this->sm)); |
|
| 183 | - } |
|
| 181 | + public function dumpStates(){ |
|
| 182 | + var_dump(array_keys($this->sm)); |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - public function dumpEvents(){ |
|
| 186 | - var_dump($this->sm); |
|
| 187 | - } |
|
| 185 | + public function dumpEvents(){ |
|
| 186 | + var_dump($this->sm); |
|
| 187 | + } |
|
| 188 | 188 | } |
@@ -20,15 +20,15 @@ discard block |
||
| 20 | 20 | |
| 21 | 21 | public function __construct(StateMachineBuilder $smb = null) |
| 22 | 22 | { |
| 23 | - if( $smb != null ) |
|
| 23 | + if ($smb != null) |
|
| 24 | 24 | $sm = $smb->__invoke(); |
| 25 | 25 | |
| 26 | - if( ! empty($sm) ){ |
|
| 26 | + if ( ! empty($sm)) { |
|
| 27 | 27 | // StateEnum::CURRENT_STATE => [ EventEnum::ON_EVENT => [ StateEnum::NEXT_STATE_2, ActionClosureOrFunction ] ], |
| 28 | - foreach ($sm as $state => $transition){ |
|
| 28 | + foreach ($sm as $state => $transition) { |
|
| 29 | 29 | $this->addState($state); |
| 30 | 30 | foreach ($transition as $onEvent => $nextStateAndAction) { |
| 31 | - if( array_key_exists(self::EXEC_ACTION, $nextStateAndAction) ) { |
|
| 31 | + if (array_key_exists(self::EXEC_ACTION, $nextStateAndAction)) { |
|
| 32 | 32 | $this->addTransition($state, $onEvent, $nextStateAndAction[self::NEXT_STATE], $nextStateAndAction[self::EXEC_ACTION]); |
| 33 | 33 | } else { |
| 34 | 34 | $this->addTransition($state, $onEvent, $nextStateAndAction[self::NEXT_STATE]); |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | return $this; |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | - public function addTransition($currState, $currEvent, $nextState, \Closure $execAction = null ) |
|
| 52 | + public function addTransition($currState, $currEvent, $nextState, \Closure $execAction = null) |
|
| 53 | 53 | { |
| 54 | 54 | $this->argumentIsValidOrFail($currState); |
| 55 | 55 | $this->argumentIsValidOrFail($currEvent); |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | return $this; |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | - public function addCommonTransition($currentEvent, $nextState, \Closure $execAction = null ) |
|
| 67 | + public function addCommonTransition($currentEvent, $nextState, \Closure $execAction = null) |
|
| 68 | 68 | { |
| 69 | 69 | $this->argumentIsValidOrFail($currentEvent); |
| 70 | 70 | $this->argumentIsValidOrFail($nextState); |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | */ |
| 82 | 82 | public function fireEvent($event) |
| 83 | 83 | { |
| 84 | - if( $this->transitionInProgress ){ |
|
| 84 | + if ($this->transitionInProgress) { |
|
| 85 | 85 | array_push($this->eventsQueue, $event); |
| 86 | 86 | return $this; |
| 87 | 87 | } |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | $this->argumentIsValidOrFail($event); |
| 91 | 91 | $this->eventMustExistOrFail($event); |
| 92 | 92 | |
| 93 | - if( isset($this->commonTransition[$event]) ){ |
|
| 93 | + if (isset($this->commonTransition[$event])) { |
|
| 94 | 94 | $transition = $this->commonTransition[$event]; |
| 95 | 95 | } else { |
| 96 | 96 | $transition = $this->sm[$this->currentState][$event]; |
@@ -102,11 +102,11 @@ discard block |
||
| 102 | 102 | $this->currentEvent = $event; |
| 103 | 103 | |
| 104 | 104 | $action = $transition[self::EXEC_ACTION]; |
| 105 | - if( $action ){ |
|
| 105 | + if ($action) { |
|
| 106 | 106 | ($action)($this); |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - if( $this->cancelTransition ){ |
|
| 109 | + if ($this->cancelTransition) { |
|
| 110 | 110 | $this->cancelTransition = false; |
| 111 | 111 | } else { |
| 112 | 112 | $this->currentState = $transition[self::NEXT_STATE]; |
@@ -114,30 +114,30 @@ discard block |
||
| 114 | 114 | |
| 115 | 115 | $this->transitionInProgress = false; |
| 116 | 116 | $event = array_shift($this->eventsQueue); |
| 117 | - if( $event != null ){ |
|
| 117 | + if ($event != null) { |
|
| 118 | 118 | $this->fireEvent($event); |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | return $this; |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | - public function cancelTransition(){ |
|
| 124 | + public function cancelTransition() { |
|
| 125 | 125 | $this->cancelTransition = true; |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | - public function getCurrentState(){ |
|
| 128 | + public function getCurrentState() { |
|
| 129 | 129 | return $this->currentState; |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | - public function getCurrentEvent(){ |
|
| 132 | + public function getCurrentEvent() { |
|
| 133 | 133 | return $this->currentEvent; |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | - public function getNextState(){ |
|
| 136 | + public function getNextState() { |
|
| 137 | 137 | return $this->nextState; |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | - public function getMachineToArray(){ |
|
| 140 | + public function getMachineToArray() { |
|
| 141 | 141 | return $this->sm; |
| 142 | 142 | } |
| 143 | 143 | |
@@ -149,40 +149,40 @@ discard block |
||
| 149 | 149 | |
| 150 | 150 | private function argumentIsNotNullOrFail($arg): void |
| 151 | 151 | { |
| 152 | - if( $arg === null ) |
|
| 152 | + if ($arg === null) |
|
| 153 | 153 | throw new \InvalidArgumentException("Null is not an valid argument"); |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | private function argumentIsNotBlankOrFail($arg): void |
| 157 | 157 | { |
| 158 | - if( trim($arg) === "" ) |
|
| 158 | + if (trim($arg) === "") |
|
| 159 | 159 | throw new \InvalidArgumentException("Blank is not an valid argument"); |
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | private function eventMustExistOrFail($event) |
| 163 | 163 | { |
| 164 | - if( !( isset($this->sm[$this->currentState][$event]) || isset($this->commonTransition[$event]) ) ) |
|
| 164 | + if ( ! (isset($this->sm[$this->currentState][$event]) || isset($this->commonTransition[$event]))) |
|
| 165 | 165 | throw new \InvalidArgumentException("Unexpected event {$event} on {$this->currentState} state"); |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | private function stateMustExistOrFail($state) |
| 169 | 169 | { |
| 170 | - if( ! isset($this->sm[$this->currentState]) ) |
|
| 170 | + if ( ! isset($this->sm[$this->currentState])) |
|
| 171 | 171 | throw new \InvalidArgumentException("Event {$this->currentEvent} fired an unexpected {$this->currentState} state"); |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | private function setCurrentStateIfThisIsInitialState($state): void |
| 175 | 175 | { |
| 176 | - if( $this->currentState == null){ |
|
| 176 | + if ($this->currentState == null) { |
|
| 177 | 177 | $this->currentState = $state; |
| 178 | 178 | } |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | - public function dumpStates(){ |
|
| 181 | + public function dumpStates() { |
|
| 182 | 182 | var_dump(array_keys($this->sm)); |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | - public function dumpEvents(){ |
|
| 185 | + public function dumpEvents() { |
|
| 186 | 186 | var_dump($this->sm); |
| 187 | 187 | } |
| 188 | 188 | } |
@@ -20,8 +20,9 @@ discard block |
||
| 20 | 20 | |
| 21 | 21 | public function __construct(StateMachineBuilder $smb = null) |
| 22 | 22 | { |
| 23 | - if( $smb != null ) |
|
| 24 | - $sm = $smb->__invoke(); |
|
| 23 | + if( $smb != null ) { |
|
| 24 | + $sm = $smb->__invoke(); |
|
| 25 | + } |
|
| 25 | 26 | |
| 26 | 27 | if( ! empty($sm) ){ |
| 27 | 28 | // StateEnum::CURRENT_STATE => [ EventEnum::ON_EVENT => [ StateEnum::NEXT_STATE_2, ActionClosureOrFunction ] ], |
@@ -149,26 +150,30 @@ discard block |
||
| 149 | 150 | |
| 150 | 151 | private function argumentIsNotNullOrFail($arg): void |
| 151 | 152 | { |
| 152 | - if( $arg === null ) |
|
| 153 | - throw new \InvalidArgumentException("Null is not an valid argument"); |
|
| 153 | + if( $arg === null ) { |
|
| 154 | + throw new \InvalidArgumentException("Null is not an valid argument"); |
|
| 155 | + } |
|
| 154 | 156 | } |
| 155 | 157 | |
| 156 | 158 | private function argumentIsNotBlankOrFail($arg): void |
| 157 | 159 | { |
| 158 | - if( trim($arg) === "" ) |
|
| 159 | - throw new \InvalidArgumentException("Blank is not an valid argument"); |
|
| 160 | + if( trim($arg) === "" ) { |
|
| 161 | + throw new \InvalidArgumentException("Blank is not an valid argument"); |
|
| 162 | + } |
|
| 160 | 163 | } |
| 161 | 164 | |
| 162 | 165 | private function eventMustExistOrFail($event) |
| 163 | 166 | { |
| 164 | - if( !( isset($this->sm[$this->currentState][$event]) || isset($this->commonTransition[$event]) ) ) |
|
| 165 | - throw new \InvalidArgumentException("Unexpected event {$event} on {$this->currentState} state"); |
|
| 167 | + if( !( isset($this->sm[$this->currentState][$event]) || isset($this->commonTransition[$event]) ) ) { |
|
| 168 | + throw new \InvalidArgumentException("Unexpected event {$event} on {$this->currentState} state"); |
|
| 169 | + } |
|
| 166 | 170 | } |
| 167 | 171 | |
| 168 | 172 | private function stateMustExistOrFail($state) |
| 169 | 173 | { |
| 170 | - if( ! isset($this->sm[$this->currentState]) ) |
|
| 171 | - throw new \InvalidArgumentException("Event {$this->currentEvent} fired an unexpected {$this->currentState} state"); |
|
| 174 | + if( ! isset($this->sm[$this->currentState]) ) { |
|
| 175 | + throw new \InvalidArgumentException("Event {$this->currentEvent} fired an unexpected {$this->currentState} state"); |
|
| 176 | + } |
|
| 172 | 177 | } |
| 173 | 178 | |
| 174 | 179 | private function setCurrentStateIfThisIsInitialState($state): void |