|
1
|
|
|
<?php declare(strict_types=1); |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
namespace Star\Component\State\Builder; |
|
4
|
|
|
|
|
5
|
|
|
use Star\Component\State\EventRegistry; |
|
6
|
|
|
use Star\Component\State\Port\Symfony\EventDispatcherAdapter; |
|
7
|
|
|
use Star\Component\State\StateMachine; |
|
8
|
|
|
use Star\Component\State\StateTransition; |
|
9
|
|
|
use Star\Component\State\TransitionRegistry; |
|
10
|
|
|
use Star\Component\State\Transitions\ManyToOneTransition; |
|
11
|
|
|
use Star\Component\State\Transitions\OneToOneTransition; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Tool to build the StateMachine. |
|
15
|
|
|
*/ |
|
|
|
|
|
|
16
|
|
|
final class StateBuilder |
|
17
|
|
|
{ |
|
|
|
|
|
|
18
|
|
|
/** |
|
|
|
|
|
|
19
|
|
|
* @var TransitionRegistry |
|
20
|
|
|
*/ |
|
21
|
|
|
private $registry; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
|
|
|
|
|
24
|
|
|
* @var EventRegistry |
|
25
|
|
|
*/ |
|
26
|
|
|
private $listeners; |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
36 |
|
public function __construct() |
|
|
|
|
|
|
29
|
|
|
{ |
|
|
|
|
|
|
30
|
36 |
|
$this->registry = new TransitionRegistry(); |
|
|
|
|
|
|
31
|
36 |
|
$this->listeners = new EventDispatcherAdapter(); |
|
32
|
36 |
|
} |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
|
|
|
|
|
35
|
|
|
* @param string $name |
|
|
|
|
|
|
36
|
|
|
* @param string|string[] $from |
|
|
|
|
|
|
37
|
|
|
* @param string $to |
|
|
|
|
|
|
38
|
|
|
* |
|
39
|
|
|
* @return StateBuilder |
|
40
|
|
|
*/ |
|
41
|
36 |
|
public function allowTransition(string $name, $from, string $to): StateBuilder |
|
42
|
|
|
{ |
|
|
|
|
|
|
43
|
36 |
|
if (\is_array($from)) { |
|
44
|
16 |
|
$transition = new ManyToOneTransition($name, $to, ...$from); |
|
45
|
|
|
} else { |
|
46
|
36 |
|
$transition = new OneToOneTransition($name, $from, $to); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
36 |
|
$this->allowCustomTransition($transition); |
|
50
|
|
|
|
|
51
|
36 |
|
return $this; |
|
52
|
|
|
} |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
|
|
|
|
|
55
|
|
|
* @param StateTransition $transition |
|
|
|
|
|
|
56
|
|
|
*/ |
|
|
|
|
|
|
57
|
36 |
|
public function allowCustomTransition(StateTransition $transition): void |
|
58
|
|
|
{ |
|
|
|
|
|
|
59
|
36 |
|
$this->registry->addTransition($transition); |
|
60
|
36 |
|
} |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
|
|
|
|
|
63
|
|
|
* @param string $attribute The attribute |
|
|
|
|
|
|
64
|
|
|
* @param string|string[] $states The list of states that this attribute applies to |
|
|
|
|
|
|
65
|
|
|
* |
|
66
|
|
|
* @return StateBuilder |
|
67
|
|
|
*/ |
|
68
|
31 |
|
public function addAttribute(string $attribute, $states): StateBuilder |
|
69
|
|
|
{ |
|
|
|
|
|
|
70
|
31 |
|
$states = (array) $states; |
|
|
|
|
|
|
71
|
31 |
|
foreach ($states as $stateName) { |
|
72
|
31 |
|
$this->registry->addAttribute($stateName, $attribute); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
31 |
|
return $this; |
|
76
|
|
|
} |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
36 |
|
public function create(string $currentState): StateMachine |
|
|
|
|
|
|
79
|
|
|
{ |
|
|
|
|
|
|
80
|
36 |
|
return new StateMachine($currentState, $this->registry, $this->listeners); |
|
81
|
|
|
} |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
18 |
|
public static function build(): StateBuilder |
|
|
|
|
|
|
84
|
|
|
{ |
|
|
|
|
|
|
85
|
18 |
|
return new static(); |
|
86
|
|
|
} |
|
|
|
|
|
|
87
|
|
|
} |
|
|
|
|
|
|
88
|
|
|
|