Skip to content

Commit f5659e2

Browse files
committed
phpDoc + README update
1 parent 294235b commit f5659e2

5 files changed

Lines changed: 39 additions & 30 deletions

File tree

README.md

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
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

src/Assertion/CallbackAssertion.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public function setParams($params = []) {
3333
return $this;
3434
}
3535

36+
/**
37+
* @return bool
38+
* @throws AssertionException
39+
*/
3640
public function make(): bool
3741
{
3842
if ($this->callback) {

src/Assertion/CommonCallbackAssertion.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ public function setTransition(TransitionInterface $transition)
3535

3636
return $this;
3737
}
38-
38+
39+
/**
40+
* @return bool
41+
* @throws AssertionException
42+
*/
3943
public function make(): bool
4044
{
4145
if ($this->object instanceof MachineInterface && $this->transition instanceof TransitionInterface)

src/Assertion/DefaultCallbackAssertion.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ public function setTransition(TransitionInterface $transition)
3535

3636
return $this;
3737
}
38-
38+
39+
/**
40+
* @return bool
41+
* @throws AssertionException
42+
*/
3943
public function make(): bool
4044
{
4145
if ($this->object instanceof MachineInterface && $this->transition instanceof TransitionInterface)

src/Machine.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,14 @@ public function assertTransition(Transition $transition) : bool
141141
return true;
142142
}
143143

144+
/**
145+
* @return StateEnum
146+
* @throws TransitionException
147+
*/
144148
public function run() {
145149

146150
do {
151+
$result = false;
147152

148153
$allowedTrans = $this->getAllowedTransitions();
149154

@@ -179,6 +184,7 @@ public function run() {
179184
* @param $newState
180185
* @return $this
181186
* @throws TransitionException
187+
* @throws MachineException
182188
*/
183189
protected function setMachineState(StateEnum $newState) {
184190

0 commit comments

Comments
 (0)