PHP code example of pektiyaz / laravel-event-engine
1. Go to this page and download the library: Download pektiyaz/laravel-event-engine 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/ */
pektiyaz / laravel-event-engine example snippets
return new EventCanValue(true)
// OR
return new EventCanValue(true, $data)
// OR
return new EventCanValue(false, "Reason..")
return new EventDoValue(true)
// OR
return new EventDoValue(true, $data)
// OR
return new EventDoValue(false, "Reason...")
//Listener in module Account
return new EventAskValue(true, ["name" => "John"])
//Listener in module Finance
return new EventAskValue(true, ["balance" => 500])
//Result
[
"name" => "John",
"balance" => 500
]
namespace App\Events;
class CanClientPay
{
public function __construct(
public int $clientId,
public float $amount
) {}
}
namespace App\Listeners;
use App\Events\CanClientPay;use Pektiyaz\LaravelEventEngine\CanValues\EventCanValue;
class CheckClientBalanceListener
{
public function handle(CanClientPay $event): EventCanValue
{
// Example logic: client can pay if they have more than $event->amount
$balance = 500; // You’d normally fetch this from DB
if ($balance >= $event->amount) {
return new EventCanValue(true);
}
return new EventCanValue(false, 'Insufficient balance');
}
}
namespace App\Listeners;
use App\Events\CanClientPay;use Pektiyaz\LaravelEventEngine\CanValues\EventCanValue;
class CanClientPayListener
{
public function handle(CanClientPay $event): EventCanValue
{
// Example logic: client can pay if they have more than $event->amount
$balance = 500; // You’d normally fetch this from DB
if ($balance >= $event->amount) {
return new EventCanValue(true);
}
return new EventCanValue(false, 'Insufficient balance', "CanClientPayListener");
}
}