PHP code example of spotlibs / php-lib

1. Go to this page and download the library: Download spotlibs/php-lib 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/ */

    

spotlibs / php-lib example snippets




declare(strict_types=1);

use Spotlibs\PhpLib\Exceptions\StdException;
use Spotlibs\PhpLib\Exceptions\AccessException;

class ExceptionUsage
{
    public function ThrowingException()
    {
        throw new AccessException('You shall not pass!');
    }

    // you can also throw any exception by static function of StdException
    public function ThrowingStandardException()
    {
        throw StdException::create(
            StdException::HEADER_EXCEPTION,
            'Invalid headers to access this resource',
        );
    }
}



declare(strict_types=1);

use Spotlibs\PhpLib\Responses\StdResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Controllers\Controller;
use App\Models\Post;

class DailyNews extends Controller
{
    public function index(Request $request): Response
    {
        Post::create([
            'headline' => 'Top 10 Countries with Best Cyber Security, Finland Ranked First',
            'content' => 'Here are the top 10 countries with the best cyber securities based on the data published by Mix Mode and 2024 Global Security Index:'
        ]);
        return StdResponse::success('Daily news posted.');
    }
}



declare(strict_types=1);

use Spotlibs\PhpLib\Dtos\TraitDtos;

class Person
{
    use TraitDtos;

    public string $name;
    public int $age;
    public array $hobbies;
}

class Introduction
{
    protected Person $person;

    public function __construct()
    {
        // pass associative array to construct DTO
        $this->person = new Person([
            'name' => 'John',
            'age' => 30,
            'hobbies' => ['fishing', 'sleeping', 'coding']
        ]);
    }

    public function index(): void
    {
        echo "Hi, my name is " . $this->person->name . ". I am " . $this->person->age . " year(s) old" . PHP_EOL;
    }
}

$app->register(\Spotlibs\PhpLib\Providers\ContextServiceProvider::class);

$app->middleware([
    //...
    \Spotlibs\PhpLib\Middlewares\ActivityMonitor::class,
    //...
]);



declare(strict_types=1);

use Spotlibs\PhpLib\Logs\Log;

class HoldingBeer
{
    public function HoldMyBeer()
    {
        Log::activity()->info(['timestamp' => '2006-01-02 15:04:05', 'status' => 'accomplished']);
        Log::worker()->info(['timestamp' => '2006-01-02 15:04:05', 'job' => 'holding bro\'s beer']);
        Log::runtime()->info(['timestamp' => '2006-01-02 15:04:05', 'error' => 'the beer likely to spilled']);
        Log::runtime()->warning(['timestamp' => '2006-01-02 15:04:05', 'error' => 'the beer gonna be spilled']);
        Log::runtime()->error(['timestamp' => '2006-01-02 15:04:05', 'error' => 'the beer got spilled']);
    }
}

$app->register(\Spotlibs\PhpLib\Providers\QueueEventServiceProvider::class);



declare(strict_types=1);

use Spotlibs\PhpLib\Facades\Queue;
use App\Jobs\SendMoney;
use App\Models\Employee;

class Payday
{
    public function index(Request $request): void
    {
        $employees = Employee::get();
        foreach ($employees as $employee) {
            Queue::pushOn('payday', new SendMoney());
        }
    }
}