1. Go to this page and download the library: Download aw-studio/laravel-states 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/ */
class BookingStateTransitions extends State
{
const PAYMENT_PAID = 'payment_paid';
const PAYMENT_FAILED = 'payment_failed';
}
class BookingState extends State
{
// ...
public static function config()
{
self::set(BookingStateTransition::PAYMENT_PAID)
->from(self::PENDING)
->to(self::SUCCESSFULL);
self::set(BookingStateTransition::PAYMENT_FAILED)
->from(self::PENDING)
->to(self::FAILED);
}
}
use AwStudio\States\Contracts\Stateful;
use AwStudio\States\HasStates;
class Booking extends Model implements Stateful
{
use HasStates;
protected $states = [
'state' => BookingState::class,
'payment_state' => ...,
];
}
$booking->states()->get() // Get all states.
$booking->states('payment_state')->get() // Get all payment states.
class BookingObserver
{
public function stateSuccessfull(Booking $booking)
{
// Gets fired when booking state changed to successfull.
}
public function paymentStatePaid(Booking $booking)
{
// Gets fired when booking payment_state changed to paid.
}
public function stateTransitionPaymentPaid(Booking $booking)
{
// Gets fired when state transition payment_paid gets fired.
}
}
BookingState::whereCan(BookingStateTransition::PAYMENT_PAID); // Gets states where from where the given transition can be executed.
BookingState::canTransitionFrom('pending', 'cancel'); // Determines if the transition can be executed for the given state.