-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateHolder.php
94 lines (73 loc) · 1.71 KB
/
StateHolder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace FSMgasm;
use Iterator;
abstract class StateHolder extends State implements Iterator
{
protected int $current = 0;
/* @var State[] $states */
protected array $states;
public function __construct(array $states = [])
{
$this->states = $states;
}
public function current(): State
{
return $this->states[$this->current];
}
public function previous(): void
{
$oldState = $this->current();
$oldState->cleanup();
$this->current = max($this->key() - 1, 0);
$newState = $this->current();
$newState->cleanup();
$newState->start();
}
public function cleanup(): void
{
parent::cleanup();
$this->current = 0;
foreach ($this->states as $state) {
$state->cleanup();
}
}
public function next(): void
{
++$this->current;
}
public function key(): int
{
return $this->current;
}
public function valid(): bool
{
return isset($this->states[$this->current]);
}
public function rewind(): void
{
$this->current = 0;
}
public function add(State $state): void
{
$this->states[] = $state;
}
public function addAll(array $newStates): void
{
$this->states = array_merge($this->states, $newStates);
}
public function freeze(): void
{
$this->setFrozen(true);
}
public function setFrozen(bool $frozen): void
{
foreach ($this->states as $state) {
$state->setFrozen($frozen);
}
parent::setFrozen($frozen);
}
public function unfreeze(): void
{
$this->setFrozen(false);
}
}