1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LightFsm; |
4
|
|
|
|
5
|
|
|
class StateConfiguration |
6
|
|
|
{ |
7
|
|
|
/** @var string|int */ |
8
|
|
|
private $state; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var TransitionConfiguration[] event => TransitionConfiguration |
12
|
|
|
*/ |
13
|
|
|
private $transitions = []; |
14
|
|
|
|
15
|
|
|
/** @var callable[] */ |
16
|
|
|
private $entryCallbacks = []; |
17
|
|
|
|
18
|
|
|
/** @var callable[] */ |
19
|
|
|
private $exitCallbacks = []; |
20
|
|
|
|
21
|
|
|
/** @var string|int|null */ |
22
|
|
|
private $parentState; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string|int $state |
26
|
|
|
*/ |
27
|
|
|
public function __construct($state) |
28
|
|
|
{ |
29
|
|
|
$this->state = $state; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return string|int |
34
|
|
|
*/ |
35
|
|
|
public function getState() |
36
|
|
|
{ |
37
|
|
|
return $this->state; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string|int $event |
42
|
|
|
* @param string|int $nextState |
43
|
|
|
* @param callable|null $guardCallback f($data) |
44
|
|
|
* @param string|null $guardName |
45
|
|
|
* |
46
|
|
|
* @return StateConfiguration |
47
|
|
|
*/ |
48
|
|
|
public function permit($event, $nextState, $guardCallback = null, $guardName = null) |
49
|
|
|
{ |
50
|
|
|
$this->transitions[$event] = new TransitionConfiguration($this->state, $event, $nextState, $guardCallback, $guardName); |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string|int $parentState |
57
|
|
|
* |
58
|
|
|
* @return StateConfiguration |
59
|
|
|
*/ |
60
|
|
|
public function setParentState($parentState) |
61
|
|
|
{ |
62
|
|
|
$this->parentState = $parentState; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return int|string|null |
69
|
|
|
*/ |
70
|
|
|
public function getParentState() |
71
|
|
|
{ |
72
|
|
|
return $this->parentState; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param callable $callback f($isSubState, $data, $state) |
77
|
|
|
* @param string|null $listenerName |
78
|
|
|
* |
79
|
|
|
* @return StateConfiguration |
80
|
|
|
*/ |
81
|
|
|
public function onEntry($callback, $listenerName = null) |
82
|
|
|
{ |
83
|
|
|
if ($listenerName) { |
|
|
|
|
84
|
|
|
$this->entryCallbacks[$listenerName] = $callback; |
85
|
|
|
} else { |
86
|
|
|
$this->entryCallbacks[] = $callback; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param callable $callback f($isSubState, $data, $state) |
94
|
|
|
* @param string|null $listenerName |
95
|
|
|
* |
96
|
|
|
* @return StateConfiguration |
97
|
|
|
*/ |
98
|
|
|
public function onExit($callback, $listenerName = null) |
99
|
|
|
{ |
100
|
|
|
if ($listenerName) { |
|
|
|
|
101
|
|
|
$this->exitCallbacks[$listenerName] = $callback; |
102
|
|
|
} else { |
103
|
|
|
$this->exitCallbacks[] = $callback; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string|int $event |
111
|
|
|
* |
112
|
|
|
* @return TransitionConfiguration|null |
113
|
|
|
*/ |
114
|
|
|
public function getTransition($event) |
115
|
|
|
{ |
116
|
|
|
return isset($this->transitions[$event]) ? $this->transitions[$event ] : null; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return TransitionConfiguration[] |
121
|
|
|
*/ |
122
|
|
|
public function getAllTransitions() |
123
|
|
|
{ |
124
|
|
|
return $this->transitions; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param bool $isSubState |
129
|
|
|
* @param mixed $data |
130
|
|
|
*/ |
131
|
|
|
public function triggerEntry($isSubState, $data) |
132
|
|
|
{ |
133
|
|
|
foreach ($this->entryCallbacks as $callback) { |
134
|
|
|
call_user_func($callback, $isSubState, $data, $this->state); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param bool $isSubState |
140
|
|
|
* @param mixed $data |
141
|
|
|
*/ |
142
|
|
|
public function triggerExit($isSubState, $data) |
143
|
|
|
{ |
144
|
|
|
foreach ($this->exitCallbacks as $callback) { |
145
|
|
|
call_user_func($callback, $isSubState, $data, $this->state); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return \callable[] |
151
|
|
|
*/ |
152
|
|
|
public function getAllEntryCallbacks() |
153
|
|
|
{ |
154
|
|
|
return $this->entryCallbacks; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return \callable[] |
159
|
|
|
*/ |
160
|
|
|
public function getAllExitCallbacks() |
161
|
|
|
{ |
162
|
|
|
return $this->exitCallbacks; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: