-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStateMetadata.php
More file actions
57 lines (46 loc) · 1.35 KB
/
StateMetadata.php
File metadata and controls
57 lines (46 loc) · 1.35 KB
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
<?php declare(strict_types=1);
namespace Star\Component\State;
use Star\Component\State\Builder\StateBuilder;
use Star\Component\State\Callbacks\TransitionCallback;
abstract class StateMetadata
{
/**
* @param string $current The initial state name
*/
public function __construct(
protected string $current,
) {
}
/**
* Returns the state workflow configuration.
*
* @param StateBuilder $builder
*/
abstract protected function configure(StateBuilder $builder): void;
private function getMachine(): StateMachine
{
$this->configure($builder = new StateBuilder());
// todo implement caching for faster building
return $builder->create($this->current);
}
final public function transit(
string $name,
StateContext $context,
?TransitionCallback $callback = null
): StateMetadata {
$this->current = $this->getMachine()->transit($name, $context, $callback);
return $this;
}
final public function hasAttribute(string $attribute): bool
{
return $this->getMachine()->hasAttribute($attribute);
}
final public function isInState(string $state): bool
{
return $this->getMachine()->isInState($state);
}
final public function getCurrent(): string
{
return $this->current;
}
}