PHP code example of zerexei / php-toolkit

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

    

zerexei / php-toolkit example snippets


use Zerexei\PHPToolkit\Traits\Cacheable;

class Post extends Model
{
    use Cacheable;
}

// Check cache, retrieve, or store values
$post = Post::find(1);

if ($post->hasCache()) {
    $cachedData = $post->getCache();
}

$post->putCache('custom-serialized-data', 3600);
$post->forgetCache();

use Zerexei\PHPToolkit\Traits\HasActivityLogs;

class User extends Model
{
    use HasActivityLogs;
}

use Zerexei\PHPToolkit\Traits\Searchable;

class Product extends Model
{
    use Searchable;
}

// Query products matching "macbook pro" across title and description
$products = Product::search(['title', 'description'], 'macbook pro')->get();

use Zerexei\PHPToolkit\Traits\HasUuid;

class Invoice extends Model
{
    use HasUuid;
}

use Zerexei\PHPToolkit\Traits\HasSlug;

class Article extends Model
{
    use HasSlug;

    // Optional overrides:
    public function slugSource(): string
    {
        return 'title'; // Source column (Default: 'title')
    }

    public function slugDestination(): string
    {
        return 'slug'; // Destination column (Default: 'slug')
    }
}

use Zerexei\PHPToolkit\Traits\HasStatus;

class Project extends Model
{
    use HasStatus;

    public function getStatuses(): array
    {
        return ['draft', 'active', 'completed', 'archived'];
    }
}

// Usage
$project->setStatus('active'); // Throws InvalidArgumentException if invalid
$project->isStatus('completed'); // false

// Scope queries
$activeProjects = Project::ofStatus('active')->get();
$filteredProjects = Project::ofStatus(['active', 'completed'])->get();

use Zerexei\PHPToolkit\Traits\HasFilters;

class Order extends Model
{
    use HasFilters;
}

// 1. Using a closure
$orders = Order::filter(function ($query) {
    $query->where('amount', '>', 100);
})->get();

// 2. Using a dedicated Filter Class (must implement an `apply` method)
$orders = Order::filter(\App\Filters\OrderFilter::class)->get();

use Zerexei\PHPToolkit\Utilities\StrHelper;

// Get initials
StrHelper::initials('Taylor Otwell'); // "TO"

// Estimate reading time in minutes
StrHelper::readingTime($longText, $wordsPerMinute = 200); // 4

// Truncate to exact length while preserving word boundaries
StrHelper::truncateWords($text, $limit = 80);

use Zerexei\PHPToolkit\Utilities\ArrHelper;

// Group items by a key path (supports arrays & objects)
ArrHelper::groupByKey($users, 'role_id');

// Remove all null values recursively from nested arrays
ArrHelper::filterNullRecursive($dirtyArray);

// Safely rename associative array keys
ArrHelper::renameKeys($data, ['user_id' => 'id', 'user_email' => 'email']);
bash
composer