Skip to content

Commit 2790196

Browse files
committed
fixes, simpler example given, cleanups
1 parent 0cb1cff commit 2790196

13 files changed

Lines changed: 136 additions & 117 deletions

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ composer.phar
55

66
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
77
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
8-
# composer.lock
8+
composer.lock
99

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ Maybe state machines are not what you usually do with PHP but when you do... jus
3333
public function init() {
3434
$this->setInitState(PetitionEnum::DRAFT());
3535

36-
// defines machine's allowed behavior, when no Assertion is given uses DefaultCallbackAssertion
36+
// defines machine's allowed behavior
3737
$this
38-
->allowTransition(PetitionEnum::DRAFT(), PetitionEnum::SENT())
39-
->allowTransition(PetitionEnum::DRAFT(), PetitionEnum::CANCELED())
38+
// prevents changing state upon assertion when AlwaysFalseAssertion is given
39+
->allowTransition(PetitionEnum::DRAFT(), PetitionEnum::SENT(), new AlwaysFalseAssertion())
40+
->allowTransition(PetitionEnum::DRAFT(), PetitionEnum::CANCELED(), new AlwaysFalseAssertion())
41+
42+
// when no Assertion is given uses DefaultCallbackAssertion which calls assertXToY methods
4043
->allowTransition(PetitionEnum::SENT(), PetitionEnum::VOTED())
4144
->allowTransition(PetitionEnum::VOTED(), PetitionEnum::ACCEPTED())
4245
->allowTransition(PetitionEnum::VOTED(), PetitionEnum::REJECTED())

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"autoload": {
1313
"psr-4": {
1414
"Coff\\SMF\\": "src/",
15-
"Coff\\SMF\\Example": "example/"
15+
"Coff\\SMF\\Example\\": "example/"
1616
}
1717
},
1818
"require": {

example/Boiler.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

example/BoilerPump.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

example/BoilerStateEnum.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

example/PumpStateEnum.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

example/example.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

example/example01.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace Coff\SMF\Example;
4+
5+
use Coff\SMF\Assertion\AlwaysFalseAssertion;
6+
use Coff\SMF\Machine;
7+
use Coff\SMF\StateEnum;
8+
use Coff\SMF\Transition\Transition;
9+
10+
include (__DIR__ . '/../vendor/autoload.php');
11+
12+
class PetitionEnum extends StateEnum
13+
{
14+
const __default = self::DRAFT,
15+
DRAFT = 'draft',
16+
SENT = 'sent',
17+
VOTED = 'voted',
18+
ACCEPTED = 'accepted',
19+
REJECTED = 'rejected',
20+
CANCELED = 'canceled';
21+
22+
}
23+
24+
class Petition extends Machine
25+
{
26+
27+
protected $votesYes, $votesNo;
28+
29+
public function init()
30+
{
31+
$this->setInitState(PetitionEnum::DRAFT());
32+
33+
// defines machine's allowed behavior, when no Assertion is given uses DefaultCallbackAssertion
34+
$this
35+
// prevents changing state upon assertion when AlwaysFalseAssertion is given
36+
->allowTransition(PetitionEnum::DRAFT(), PetitionEnum::SENT(), new AlwaysFalseAssertion())
37+
->allowTransition(PetitionEnum::DRAFT(), PetitionEnum::CANCELED(), new AlwaysFalseAssertion())
38+
39+
// when no Assertion is given uses DefaultCallbackAssertion which calls assertXToY methods
40+
->allowTransition(PetitionEnum::SENT(), PetitionEnum::VOTED())
41+
->allowTransition(PetitionEnum::VOTED(), PetitionEnum::ACCEPTED())
42+
->allowTransition(PetitionEnum::VOTED(), PetitionEnum::REJECTED());
43+
44+
}
45+
46+
public function send()
47+
{
48+
// shall throw an exception if current state is not DRAFT because it wasn't allowed transition
49+
$this->setMachineState(PetitionEnum::SENT());
50+
}
51+
52+
public function cancel()
53+
{
54+
// shall throw an exception if current state is not DRAFT because it wasn't allowed transition
55+
$this->setMachineState(PetitionEnum::CANCELED());
56+
}
57+
58+
59+
public function setVotes($ya, $nay)
60+
{
61+
$this->votesYes = $ya;
62+
$this->votesNo = $nay;
63+
}
64+
65+
public function assertSentToVoted()
66+
{
67+
// Method name used here is based upon DefaultCallbackAssertion. This can be changed though.
68+
return (null !== $this->votesYes && null !== $this->votesNo) ? true : false;
69+
}
70+
71+
72+
public function assertVotedToAccepted()
73+
{
74+
return $this->votesYes > $this->votesNo ? true : false;
75+
}
76+
77+
public function assertVotedToRejected()
78+
{
79+
return $this->votesYes <= $this->votesNo ? true : false;
80+
}
81+
82+
public function onTransition(Transition $transition)
83+
{
84+
/* for purpose of this example we only echo this transition but you can
85+
easily dispatch an event from here */
86+
echo 'State changed from ' . $transition->getFromState() . ' to ' . $transition->getToState() . PHP_EOL;
87+
}
88+
}
89+
90+
91+
$p = new Petition();
92+
$p->init();
93+
94+
echo 'Running...' . PHP_EOL;
95+
$p->run();
96+
97+
echo 'Sending...' . PHP_EOL;
98+
$p->send();
99+
100+
echo 'Running...' . PHP_EOL;
101+
$p->run();
102+
103+
echo 'Setting votes...' . PHP_EOL;
104+
$p->setVotes(5, 1);
105+
106+
echo 'Running...' . PHP_EOL;
107+
$p->run();

src/Assertion/CallbackAssertion.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class CallbackAssertion extends Assertion
1414
/** @var array */
1515
protected $params;
1616

17-
public function __construct(callable $callback = [], array $params = [])
17+
public function __construct(callable $callback = null, array $params = [])
1818
{
1919
$this->callback = $callback;
2020

0 commit comments

Comments
 (0)