1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Finite\State; |
4
|
|
|
|
5
|
|
|
use Finite\Transition\TransitionInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* The base State class. |
9
|
|
|
* Feel free to extend it to fit to your needs. |
10
|
|
|
* |
11
|
|
|
* @author Yohan Giarelli <[email protected]> |
12
|
|
|
* @author Michal Dabrowski <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class State implements StateInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The state type. |
18
|
|
|
* |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
protected $type; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The transition name. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $transitions; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The state name. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $name; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $properties; |
41
|
|
|
|
42
|
235 |
|
public function __construct($name, $type = self::TYPE_NORMAL, array $transitions = array(), array $properties = array()) |
43
|
|
|
{ |
44
|
235 |
|
$this->name = $name; |
45
|
235 |
|
$this->type = $type; |
|
|
|
|
46
|
235 |
|
$this->transitions = $transitions; |
47
|
235 |
|
$this->properties = $properties; |
48
|
235 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function isInitial() |
54
|
|
|
{ |
55
|
|
|
return self::TYPE_INITIAL === $this->type; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function isFinal() |
62
|
|
|
{ |
63
|
|
|
return self::TYPE_FINAL === $this->type; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function isNormal() |
70
|
|
|
{ |
71
|
|
|
return self::TYPE_NORMAL === $this->type; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
20 |
|
public function getType() |
78
|
|
|
{ |
79
|
20 |
|
return $this->type; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $transition |
84
|
|
|
*/ |
85
|
195 |
|
public function addTransition($transition) |
86
|
|
|
{ |
87
|
195 |
|
if ($transition instanceof TransitionInterface) { |
88
|
185 |
|
$transition = $transition->getName(); |
89
|
111 |
|
} |
90
|
|
|
|
91
|
195 |
|
$this->transitions[] = $transition; |
92
|
195 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param array $transitions |
96
|
|
|
*/ |
97
|
|
|
public function setTransitions(array $transitions) |
98
|
|
|
{ |
99
|
|
|
foreach ($transitions as $transition) { |
100
|
|
|
$this->addTransition($transition); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
70 |
|
public function getTransitions() |
108
|
|
|
{ |
109
|
70 |
|
return $this->transitions; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
* |
115
|
|
|
* @deprecated Deprecated since version 1.0.0-BETA2. Use {@link StateMachine::can($transition)} instead. |
116
|
|
|
*/ |
117
|
10 |
|
public function can($transition) |
118
|
|
|
{ |
119
|
10 |
|
if ($transition instanceof TransitionInterface) { |
120
|
|
|
$transition = $transition->getName(); |
121
|
|
|
} |
122
|
|
|
|
123
|
10 |
|
return in_array($transition, $this->transitions); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
20 |
|
public function has($property) |
130
|
|
|
{ |
131
|
20 |
|
return array_key_exists($property, $this->properties); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
5 |
|
public function get($property, $default = null) |
138
|
|
|
{ |
139
|
5 |
|
return $this->has($property) ? $this->properties[$property] : $default; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
10 |
|
public function getProperties() |
146
|
|
|
{ |
147
|
10 |
|
return $this->properties; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
200 |
|
public function getName() |
154
|
|
|
{ |
155
|
200 |
|
return $this->name; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param array $properties |
160
|
|
|
*/ |
161
|
|
|
public function setProperties(array $properties) |
162
|
|
|
{ |
163
|
|
|
$this->properties = $properties; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
public function __toString() |
170
|
|
|
{ |
171
|
|
|
return $this->getName(); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.