1. Go to this page and download the library: Download halaei/helpers 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/ */
halaei / helpers example snippets
use Halaei\Helpers\Supervisor\Supervisor;
app(Supervisor::class)->supervise(GetTelegramUpdates::class);
class GetTelegramUpdates
{
function handle(Api $api)
{
$updates = $api->getUpdates(...);
$this->queueUpdates($updates);
}
function queueUpdates($updates)
{
...
}
}
use Halaei\Helpers\Supervisor\QuitsOnSignals;
class SomeCommand
{
use QuitsOnSignals;
public function handle()
{
$this->listenToSignals();
try {
while(someConditionHolds()) {
$this->process(); // a process that must be atomic (it shouldn't abort in the middle).
$this->quitIfSignaled();
}
} finally {
//Optional. Required if this process has other stuff to do after handle().
$this->stopListeningToSignals();
}
}
}
use Halaei\Helpers\Objects\DataObject;
class Order extends DataObject
{
public static function relations()
{
return [
'items' => [Item::class], // items is a collection of objects of type Item.
'destination' => Location::class, //delivered_to is an object of type Location
'customer_mobile' => [Mobile::class, 'decode'], // customer mobile is a string that can be casted to a Mobile object via 'decode' static function.
];
}
}
/**
* @property string $item_code
* @property int $quantity
* @property float $unit_price
* @property float $total_price
*/
class Item extends DataObject
{
}
/**
* @property float $lat
* @property float $lon
*/
class Location extends DataObject
{
}
class Mobile extends DataObject
{
public static function decode($str)
{
if (preg_match('/^\+(\d+)-(\d+)$/', $str, $parts)) {
return new self(['code' => $parts[1], 'number' => $parts[2]]);
}
}
public function toRaw()
{
return '+'.$this->code.'-'.$this->number;
}
}
$array = [
'id' => 1234,
'items' => [
[
'item_code' => '#100',
'quantity' => 5,
'unit_price' => 24,
'total_price' => 120,
],
[
'item_code' => '#200',
'quantity' => 1,
'unit_price' => 80,
'total_price' => 80,
],
],
'final_price' => 200,
'delivered_to' => [
'lat' => 37.74123543,
'lon' => 49.43254355,
],
'customer_mobile' => '+98-9131231212',
];
$order = new Order($array);
echo get_class($order->delivered_to); // Location
echo $order->items[0]->quantity;// 5
var_dump($order->toArray()['mobile_number']); // ['code' => '+98', 'number' => '9131231212']
var_dump($order->toRaw()['mobile_number']); // +98-9131231212
use \Halaei\Helpers\Redis\Lock;
$lock = app(Lock::class);
...
// 1. Wait for lock named 'critical_section'
while (! $lock->lock('critical_section', 0.1) {}
// 2. Do some critical job
//...
// 3. Release the lock
$lock->unlock('critical_section', 0.1);