2017 © Pedro Peláez
 

library eloquent-status-mutator

A status manager trait for Eloquent models

image

laravel-israel/eloquent-status-mutator

A status manager trait for Eloquent models

  • Thursday, October 5, 2017
  • by laravel Israel
  • Repository
  • 3 Watchers
  • 4 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Eloquent Status Mutator

Build Status Latest Stable Version Total Downloads License, (*1)

Handling status changes of a model is always a pain., (*2)

Eloquent Status Mutator provides is a simple trait which enforces correct status changes & some more cool features., (*3)

Usage

Define Statuses

Define the statuses of the model in the statuses property:, (*4)

class Order extends Model
{
    use HasStatus;

    protected $statuses = [
        'opened'    => [],
        'paid'      => ['from'     => 'opened'],
        'approved'  => ['from'     => 'paid'],
        'shipped'   => ['from'     => ['paid', 'approved']],
        'arrived'   => ['from'     => 'shipped'],
        'cancelled' => ['not-from' => ['arrived']],
    ];
}

Automatic Status Enforcement

The package makes sure that only listed statuses can be set:, (*5)

$order->status = 'opened'; // OK

$order->status = 'some other status'; // Throws Exception

Status Flow Validation

The package enforces that status can be set only after defined statuses in its 'from' key, (*6)

$order->status = 'opened';

$order->status = 'paid'; // OK

$order->status = 'arrived'; // Throws Exception

The package also enforces the 'not-from' status, (*7)

$order->status = 'arrived';

$order->status = 'cancelled'; // Throws Exception

Helpers

$order->status = 'paid';

if ($order->is('paid')) {
    echo 'The order is shipped';
}

if ($order->canBe('shipped')) {
    echo 'The order can be shipped';
}

Before and After callbacks

In some cases, we need to do something after a specific status is set - for example, send a mail after an order is cancelled. Our package invokes a method after status change by the convention of on + status name (camel cased), (*8)

class Order extends Model
{
    use HasStatus;

    protected $statuses = [
        'opened'    => [],
        'paid'      => ['from'     => 'opened'],
        'approved'  => ['from'     => 'paid'],
        'shipped'   => ['from'     => ['paid', 'approved']],
        'arrived'   => ['from'     => 'shipped'],
        'cancelled' => ['not-from' => ['arrived']],
    ];

    public function onCancelled()
    {
        // Send cancellation mail to the user
    }
}

Installation

  • Request the package via composer
composer require laravel-israel/eloquent-status-mutator
  • Use HasStatus trait in your model
class Order extends Model
{
    use HasStatus;
}
  • Define the available statuses in the model
protected $statuses = [
    'opened'    => [],
    'paid'      => ['from'     => 'opened'],
    'approved'  => ['from'     => 'paid'],
    'shipped'   => ['from'     => ['paid', 'approved']],
    'arrived'   => ['from'     => 'shipped'],
    'cancelled' => ['not-from' => ['arrived']],
];

The Versions

05/10 2017

dev-master

9999999-dev

A status manager trait for Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ido Bublil

05/10 2017

0.1

0.1.0.0

A status manager trait for Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ido Bublil