-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
979e5ec
commit b616699
Showing
10 changed files
with
150 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,87 @@ | ||
# Machinery | ||
# HEMP Machinery 📠 | ||
|
||
Machinery is tiny state machine package for Laravel. It allows you | ||
to manage a state machine for an attribute on one of your Eloquent | ||
models using PHP enumerations. | ||
|
||
Machinery gives your backed enumeration superpowers by allowing you | ||
to specify the valid transitions between states. | ||
|
||
## Installation | ||
|
||
You can install the package via composer: | ||
|
||
```bash | ||
composer require hemp/machinery | ||
``` | ||
|
||
## Usage | ||
|
||
First, create an enumeration that represents the states for your Eloquent model and the valid transitions between them: | ||
|
||
```php | ||
use Hemp\Machinery\MachineState; | ||
use Hemp\Machinery\MachineStateTrait; | ||
|
||
enum OrderStatus: string implements MachineState | ||
{ | ||
use MachineStateTrait; | ||
|
||
case Processing : 'processing'; | ||
case Shipped : 'shipped'; | ||
case Delivered : 'delivered'; | ||
|
||
public static function transitions(): array | ||
{ | ||
return [ | ||
self::Processing->value => [ | ||
self::Shipped | ||
], | ||
self::Shipped->value => [ | ||
self::Delivered | ||
], | ||
self::Delivered->value => [ | ||
// This is the final state... | ||
] | ||
]; | ||
} | ||
} | ||
``` | ||
|
||
Next, add a column to your Eloquent model's `casts` to store the state: | ||
|
||
```php | ||
use Hemp\Machinery\Machinable; | ||
use Hemp\Machinery\Machinery; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Order extends Model implements Machinable | ||
{ | ||
use Machinery; | ||
|
||
protected $casts = [ | ||
'status' => OrderStatus::class | ||
]; | ||
} | ||
``` | ||
|
||
Now you can use the `OrderStatus` enumeration to manage the state of your `Order` model: | ||
|
||
```php | ||
$order = Order::create([ | ||
'status' => OrderStatus::Processing | ||
]); | ||
|
||
$order->status->is(OrderStatus::Processing); // true | ||
$order->status->canTransitionTo(OrderStatus::Shipped); // true | ||
|
||
$order->status->transitionTo('status', OrderStatus::Shipped, function () { | ||
// Perform any actions that need to be done when the state changes... | ||
}); | ||
|
||
$order->status->is(OrderStatus::Shipped); // true | ||
|
||
$order->status->canTransitionTo('status', OrderStatus::Processing); // false | ||
|
||
$order->status->transitionTo('status', OrderStatus::Delivered); // Throws an exception... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Hemp\Machinery\Tests; | ||
|
||
use Hemp\Machinery\InvalidStateTransition; | ||
use Hemp\Machinery\Tests\Fixtures\Status; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class MachineryUnitTest extends TestCase | ||
{ | ||
public function testModelStateCanBeTransitionedUsingTransitionTo() | ||
{ | ||
$this->assertEquals(Status::Inactive, Status::Active->transitionTo(Status::Inactive)); | ||
} | ||
|
||
public function testCannotTransitionToAnInvalidState() | ||
{ | ||
$this->assertFalse(Status::Banned->canTransitionTo(Status::Active)); | ||
|
||
$this->expectException(InvalidStateTransition::class); | ||
|
||
Status::Banned->transitionTo(Status::Active); | ||
} | ||
|
||
public function testSideEffectsAreExecuted() | ||
{ | ||
$affected = false; | ||
|
||
Status::Active->transitionTo(Status::Inactive, function () use (&$affected) { | ||
$affected = true; | ||
}); | ||
|
||
$this->assertTrue($affected); | ||
} | ||
} |