1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Finite\Transition; |
4
|
|
|
|
5
|
|
|
use Finite\Exception\TransitionException; |
6
|
|
|
use Finite\StateMachine\StateMachineInterface; |
7
|
|
|
use Finite\State\StateInterface; |
8
|
|
|
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; |
9
|
|
|
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException; |
10
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The base Transition class. |
14
|
|
|
* Feel free to extend it to fit to your needs. |
15
|
|
|
* |
16
|
|
|
* @author Yohan Giarelli <[email protected]> |
17
|
|
|
* @author Michal Dabrowski <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class Transition implements PropertiesAwareTransitionInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $initialStates; |
25
|
|
|
|
26
|
|
|
/* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $state; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $name; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var callable |
38
|
|
|
*/ |
39
|
|
|
protected $guard; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var OptionsResolver |
43
|
|
|
*/ |
44
|
|
|
protected $propertiesOptionsResolver; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $name |
48
|
|
|
* @param string|array $initialStates |
49
|
|
|
* @param string $state |
50
|
|
|
* @param callable|null $guard |
51
|
|
|
* @param OptionsResolver $propertiesOptionsResolver |
52
|
|
|
*/ |
53
|
200 |
|
public function __construct( |
54
|
|
|
$name, |
55
|
|
|
$initialStates, |
56
|
|
|
$state, |
57
|
|
|
$guard = null, |
58
|
|
|
OptionsResolver $propertiesOptionsResolver = null |
59
|
|
|
) { |
60
|
200 |
|
if (null !== $guard && !is_callable($guard)) { |
61
|
|
|
throw new \InvalidArgumentException('Invalid callable guard argument passed to Transition::__construct().'); |
62
|
|
|
} |
63
|
|
|
|
64
|
200 |
|
$this->name = $name; |
65
|
200 |
|
$this->state = $state; |
66
|
200 |
|
$this->initialStates = (array) $initialStates; |
67
|
200 |
|
$this->guard = $guard; |
68
|
200 |
|
$this->propertiesOptionsResolver = $propertiesOptionsResolver ?: new OptionsResolver(); |
69
|
200 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string|StateInterface $state |
73
|
|
|
*/ |
74
|
|
|
public function addInitialState($state) |
75
|
|
|
{ |
76
|
|
|
if ($state instanceof StateInterface) { |
77
|
|
|
$state = $state->getName(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->initialStates[] = $state; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
155 |
|
public function getInitialStates() |
87
|
|
|
{ |
88
|
155 |
|
return $this->initialStates; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
170 |
|
public function getState() |
95
|
|
|
{ |
96
|
170 |
|
return $this->state; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
20 |
|
public function process(StateMachineInterface $stateMachine) |
103
|
|
|
{ |
104
|
20 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
170 |
|
public function getName() |
110
|
|
|
{ |
111
|
170 |
|
return $this->name; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return callable|null |
116
|
|
|
*/ |
117
|
45 |
|
public function getGuard() |
118
|
|
|
{ |
119
|
45 |
|
return $this->guard; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
65 |
|
public function resolveProperties(array $properties) |
126
|
|
|
{ |
127
|
|
|
try { |
128
|
65 |
|
return $this->propertiesOptionsResolver->resolve($properties); |
129
|
|
|
} catch (MissingOptionsException $e) { |
130
|
|
|
throw new TransitionException( |
131
|
|
|
'Testing or applying this transition need a parameter. Provide it or set it optional.', |
132
|
|
|
$e->getCode(), |
133
|
|
|
$e |
134
|
|
|
); |
135
|
|
|
} catch (UndefinedOptionsException $e) { |
136
|
|
|
throw new TransitionException( |
137
|
|
|
'You provided an unknown property to test() or apply(). Remove it or declare it in your graph.', |
138
|
|
|
$e->getCode(), |
139
|
|
|
$e |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritDoc} |
146
|
|
|
*/ |
147
|
7 |
|
public function has($property) |
148
|
|
|
{ |
149
|
7 |
|
return array_key_exists($property, $this->getProperties()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* {@inheritDoc} |
154
|
|
|
*/ |
155
|
7 |
|
public function get($property, $default = null) |
156
|
|
|
{ |
157
|
7 |
|
$properties = $this->getProperties(); |
158
|
|
|
|
159
|
7 |
|
return $this->has($property) ? $properties[$property] : $default; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritDoc} |
164
|
|
|
*/ |
165
|
10 |
|
public function getProperties() |
166
|
|
|
{ |
167
|
10 |
|
$missingOptions = $this->propertiesOptionsResolver->getMissingOptions(); |
168
|
|
|
|
169
|
10 |
|
return array_diff_key( |
170
|
10 |
|
$this->propertiesOptionsResolver->resolve( |
171
|
10 |
|
array_combine($missingOptions, array_fill(0, count($missingOptions), null)) |
172
|
5 |
|
), |
173
|
5 |
|
array_combine($missingOptions, $missingOptions) |
174
|
5 |
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
public function __toString() |
181
|
|
|
{ |
182
|
|
|
return $this->getName(); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|