1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Star\Component\State\Transitions; |
4
|
|
|
|
5
|
|
|
use Star\Component\State\RegistryBuilder; |
6
|
|
|
use Star\Component\State\StateRegistry; |
7
|
|
|
use Star\Component\State\StateTransition; |
8
|
|
|
use Star\Component\State\StateVisitor; |
9
|
|
|
use Star\Component\State\TransitionVisitor; |
10
|
|
|
use Webmozart\Assert\Assert; |
11
|
|
|
|
12
|
|
|
final class OneToOneTransition implements StateTransition |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $from; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $to; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $from |
26
|
|
|
* @param string $to |
27
|
|
|
*/ |
28
|
43 |
|
public function __construct($from, $to) |
29
|
|
|
{ |
30
|
43 |
|
Assert::string($from); |
31
|
43 |
|
Assert::string($to); |
32
|
43 |
|
$this->from = $from; |
33
|
43 |
|
$this->to = $to; |
34
|
43 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $from |
38
|
|
|
* |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
27 |
|
public function isAllowed($from) |
42
|
|
|
{ |
43
|
27 |
|
Assert::string($from); |
44
|
27 |
|
return $from === $this->from; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param RegistryBuilder $registry |
49
|
|
|
*/ |
50
|
42 |
|
public function onRegister(RegistryBuilder $registry) |
51
|
|
|
{ |
52
|
42 |
|
$registry->registerState($this->from, []); |
53
|
42 |
|
$registry->registerState($this->to, []); |
54
|
42 |
|
} |
55
|
|
|
|
56
|
26 |
|
public function getDestinationState() |
57
|
|
|
{ |
58
|
26 |
|
return $this->to; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param TransitionVisitor $visitor |
63
|
|
|
*/ |
64
|
2 |
|
public function acceptTransitionVisitor(TransitionVisitor $visitor) |
65
|
|
|
{ |
66
|
2 |
|
$visitor->visitFromState($this->from); |
67
|
2 |
|
$visitor->visitToState($this->to); |
68
|
2 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param StateVisitor $visitor |
72
|
|
|
* @param StateRegistry $registry |
73
|
|
|
*/ |
74
|
1 |
|
public function acceptStateVisitor(StateVisitor $visitor, StateRegistry $registry) |
75
|
|
|
{ |
76
|
1 |
|
$registry->getState($this->from)->acceptStateVisitor($visitor); |
77
|
1 |
|
$registry->getState($this->to)->acceptStateVisitor($visitor); |
78
|
1 |
|
} |
79
|
|
|
} |
80
|
|
|
|