|
| 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(); |
0 commit comments