-
Notifications
You must be signed in to change notification settings - Fork 189
/
basic-graph.php
74 lines (62 loc) · 2.09 KB
/
basic-graph.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
<?php
require_once __DIR__ . '/../vendor/autoload.php';
// Implement your document class
class Document implements Finite\StatefulInterface
{
private $state;
public function getFiniteState()
{
return $this->state;
}
public function setFiniteState($state)
{
$this->state = $state;
}
}
// Configure your graph
$document = new Document;
$stateMachine = new Finite\StateMachine\StateMachine($document);
$loader = new Finite\Loader\ArrayLoader(array(
'class' => 'Document',
'states' => array(
'draft' => array(
'type' => Finite\State\StateInterface::TYPE_INITIAL,
'properties' => array('deletable' => true, 'editable' => true),
),
'proposed' => array(
'type' => Finite\State\StateInterface::TYPE_NORMAL,
'properties' => array(),
),
'accepted' => array(
'type' => Finite\State\StateInterface::TYPE_FINAL,
'properties' => array('printable' => true),
)
),
'transitions' => array(
'propose' => array('from' => array('draft'), 'to' => 'proposed'),
'accept' => array('from' => array('proposed'), 'to' => 'accepted'),
'reject' => array('from' => array('proposed'), 'to' => 'draft'),
),
));
$loader->load($stateMachine);
$stateMachine->initialize();
// Working with workflow
// Current state
var_dump($stateMachine->getCurrentState()->getName());
var_dump($stateMachine->getCurrentState()->getProperties());
var_dump($stateMachine->getCurrentState()->has('deletable'));
var_dump($stateMachine->getCurrentState()->has('printable'));
// Available transitions
var_dump($stateMachine->getCurrentState()->getTransitions());
var_dump($stateMachine->can('propose'));
var_dump($stateMachine->can('accept'));
// Apply transitions
try {
$stateMachine->apply('accept');
} catch (\Finite\Exception\StateException $e) {
echo $e->getMessage(), "\n";
}
// Applying a transition
$stateMachine->apply('propose');
var_dump($stateMachine->getCurrentState()->getName());
var_dump($document->getFiniteState());