-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathState.php
66 lines (55 loc) · 1.69 KB
/
State.php
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
<?php
namespace sssm;
use Exception;
final class State
{
/**
* @param bool $canLoop Sets if looping the state is allowed
* @throws LoopNotAllowedException
*/
public function __construct(private string $stateName = "Undefined state name", private bool $canLoop = true){
if($this->canLoop){
$this->allowedStatesAfter[] = $this;
}elseif(in_array($this, $this->allowedStatesAfter)){
throw new LoopNotAllowedException($this);
}
}
public function getStateName(): string {
return $this->stateName;
}
public function getCanLoop(): bool {
return $this->canLoop;
}
/**
* @var array An array of states that are allowed to be looped after this state.
*/
private array $allowedStatesAfter = [];
public function getAllowedStateTransitions(): array {
return $this->allowedStatesAfter;
}
/**
* @var array An array of functions called when the state is entered.
*/
public array $onStateEnter = [];
/**
* @var array An array of functions called when the state is looped.
*/
public array $onLoop = [];
/**
* @var array An array of functions called when the state is exited.
*/
public array $onStateLeave = [];
/**
* This function adds a new state to the allowed states after this state.
* @param State $state
* @return void
* @throws LoopNotAllowedException
*/
public function addAllowedStateTransition(State $state): void
{
if($state == $this->allowedStatesAfter && !$this->canLoop){
throw new LoopNotAllowedException($this);
}
$this->allowedStatesAfter[] = $state;
}
}