1. Go to this page and download the library: Download louishrg/state-flow 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/ */
louishrg / state-flow example snippets
new Stack(self::$status, Pending::class, 'key'),
namespace App\Models\States\User;
use Louishrg\StateFlow\StateAbstract;
class Active extends StateAbstract
{
public $key = 'active';
public $label = 'Active';
public $color = 'green';
// and everything you want !
//
public function computedField() {
return $this->$label.$this->color;
}
}
$user->status;
// It'll give you the state object with all your defined constants in it.
$user = new User;
// Simply pass the state class and that's it.
$user->status = Pending::class;
Select::make('Statut', 'status')
->options(GearRequest::getState('status')->pluck('label', 'key')->toArray())
->displayUsing(fn($item) => $item->label)
->resolveUsing(fn($item) => $item->key)
// Use the magic setter in the fillUsing method
->fillUsing(fn($request, $model) => ($model->_status = $request->status)),
$user->status->equal(Banned::class);
$user->status->is();
User::getState('status')
// Import the Flow class
use Louishrg\StateFlow\Flow;
...
protected static function registerStates(){
return [
// use a custom method in your model for better readability
'status' => self::myFlow(),
];
}
// You need to use the Flow class
protected static function myFlow(){
// We'll use the data from above
return (new Flow(self::$status))
// Add a transition, here your state can go from Pending to either Accepted or Refused.
->add(Pending::class, [
Accepted::class,
Refused::class
])
->add(Refused::class, [
Pending::class
])
->add(Accepted::class, [
Pending::class,
Canceled::class,
CanceledByAdmin::class
])
->default(Pending::class);
// You can specify a default class, when creating you don't need to provide value.
}
$user->status->canBe(Banned::class);
$user->status->allowedTo();
$users = User::where('status', (new Active)->key)->get();