1. Go to this page and download the library: Download norotaro/enumata library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
norotaro / enumata example snippets
$order->status; // 'pending', 'approved', 'declined' or 'processed'
namespace App\Models;
use Norotaro\Enumata\Contracts\Nullable;
use Norotaro\Enumata\Contracts\DefineStates;
enum OrderStatus implements DefineStates
{
case Pending;
case Approved;
case Declined;
case Processed;
public function transitions(): array
{
return match ($this) {
// when the order is Pending we can approve() or decline() it
self::Pending => [
'approve' => self::Approved,
'decline' => self::Delined,
],
// when the order is Approved we can apply the processOrder() transition
self::Approved => [
'processOrder' => self::Processed,
],
};
}
public static function default(): self
{
return self::Pending;
}
}
use Norotaro\Enumata\Contracts\HasStateMachine;
use Norotaro\Enumata\Traits\EloquentHasStateMachines;
class Order extends Model implements HasStateMachine
{
use EloquentHasStateMachines;
protected $casts = [
'status' => OrderStatus::class,
];
}
$model->approve(); // Change status to OrderStatus::Approved
$model->decline(); // Change status to OrderStatus::Declined
$model->processOrder(); // Change status to OrderStatus::Processed
use Norotaro\Enumata\Contracts\HasStateMachine;
use Norotaro\Enumata\Traits\EloquentHasStateMachines;
class Order extends Model implements HasStateMachine
{
use EloquentHasStateMachines;
// disable the creation of transition methods
public bool $defaultTransitionMethods = false;
protected $casts = [
'status' => OrderStatus::class,
];
// custom transition method
public function myApproveTransition(): void {
$this->status()->transitionTo(OrderStatus::Approved);
//...
}
}
$model->status; // App\Model\OrderStatus{name: "Pending"}
$model->processOrder(force: true); // this will apply the transition and will not throw the exception
$model->status; // App\Model\OrderStatus{name: "Processed"}
$model->status()->transitionTo(OrderStatus::Pending, force:true); // will apply the transition without errors
namespace App\Models;
use Norotaro\Enumata\Contracts\Nullable;
use Norotaro\Enumata\Contracts\DefineStates;
enum OrderFulfillment implements DefineStates, Nullable
{
case Pending;
case Completed;
public function transitions(): array
{
return match ($this) {
self::Pending => [
'completeFulfillment' => self::Completed,
],
};
}
public static function default(): ?self
{
return null;
}
public static function initialTransitions(): array
{
return [
'initFulfillment' => self::Pending,
];
}
}
use Norotaro\Enumata\Contracts\HasStateMachine;
use Norotaro\Enumata\Traits\EloquentHasStateMachines;
class Order extends Model implements HasStateMachine
{
use EloquentHasStateMachines;
protected $casts = [
'status' => OrderStatus::class,
'fulfillment' => OrderFulfillment::class,
];
}