11
2+ * Remark: It's a work-in-progress project! Wanna contribute? Let me know.*
3+
24# State Machine Framework for PHP
35
4- State Machines are not what you usually do with PHP but when you do... just use this.
6+ Maybe state machines are not what you usually do with PHP but when you do... just use this.
7+
58
6-
7- It's a work-in-progress project! Wanna contribute? Let me know.
9+ ## Usage example
810
9- Usage example:
11+ ### State dictionary
1012
1113``` php
12- <?php
1314
1415 class PetitionEnum extends StateEnum {
1516 const __default = self::DRAFT,
@@ -21,14 +22,18 @@ Usage example:
2122 CANCELED = 'canceled';
2223
2324 }
25+ ```
26+ ### Machine class
2427
28+ ``` php
2529 class Petition extends Machine {
2630
2731 protected $votesYes, $votesNo;
2832
2933 public function init() {
3034 $this->setInitState(PetitionEnum::DRAFT());
3135
36+ // defines machine's allowed behavior, when no Assertion is given uses DefaultCallbackAssertion
3237 $this
3338 ->allowTransition(PetitionEnum::DRAFT(), PetitionEnum::SENT())
3439 ->allowTransition(PetitionEnum::DRAFT(), PetitionEnum::CANCELED())
@@ -60,33 +65,19 @@ Usage example:
6065
6166 public function assertSentToVoted()
6267 {
63- if (null !== $this->votesYes && null !== $this->votesNo)
64- {
65- return true;
66- }
67-
68- return false;
68+ // Method name used here is based upon DefaultCallbackAssertion. This can be changed though.
69+ return (null !== $this->votesYes && null !== $this->votesNo) ? true : false;
6970 }
7071
7172
7273 public function assertVotedToAccepted()
7374 {
74- if ($this->votesYes > $this->votesNo)
75- {
76- return true;
77- }
78-
79- return false;
75+ return $this->votesYes > $this->votesNo ? true : false;
8076 }
8177
8278 public function assertVotedToRejected()
8379 {
84- if ($this->votesYes <= $this->votesNo)
85- {
86- return true;
87- }
88-
89- return false;
80+ return $this->votesYes <= $this->votesNo ? true : false;
9081 }
9182
9283 public function onTransition(Transition $transition)
@@ -96,8 +87,12 @@ Usage example:
9687 echo 'State changed from ' . $transition->getFromState() . ' to ' . $transition->getToState() . PHP_EOL;
9788 }
9889 }
90+ ```
91+
92+ ### Machine in-use
9993
100-
94+ ``` php
95+
10196 $p = new Petition();
10297
10398 $p->run();
@@ -115,9 +110,5 @@ Usage example:
115110 // State changed from sent to voted
116111 // State changed from voted to accepted
117112
118-
119-
120-
121-
122113```
123114
0 commit comments