PHP code example of andydefer / laravel-utils

1. Go to this page and download the library: Download andydefer/laravel-utils 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/ */

    

andydefer / laravel-utils example snippets


use AndyDefer\LaravelUtils\Proxies\TransformableProxy;
use App\ValueObjects\SlugVO;

// Depuis une chaîne
$slug = TransformableProxy::make(SlugVO::class, 'mon-slug');

// Depuis un tableau
$user = TransformableProxy::make(UserRecord::class, [
    'name' => 'John Doe',
    'email' => '[email protected]',
]);

// Depuis du JSON
$product = TransformableProxy::make(ProductRecord::class, '{"name":"Laptop","price":999}');

// Avec nullable
$coordinates = TransformableProxy::make(CoordinatesVO::class, null, nullable: true);

use AndyDefer\LaravelUtils\Proxies\AttributeProxy;
use App\ValueObjects\SlugVO;
use App\ValueObjects\CoordinatesVO;
use App\Records\SettingsRecord;

class User extends Model
{
    // Attribut nullable (slug)
    protected function slug(): Attribute
    {
        return AttributeProxy::nullable(SlugVO::class, column: 'slug');
    }

    // Attribut ribut avec transformation personnalisée
    protected function customSlug(): Attribute
    {
        return AttributeProxy::nullable(
            SlugVO::class,
            column: 'slug',
            get: function ($value, $attributes) {
                return strtolower(trim($value));
            },
            set: function ($value) {
                return ['slug' => strtolower(trim($value))];
            }
        );
    }
}

// Utilisation
$user = User::find(1);
echo $user->slug->getValue();           // 'john-doe'
echo $user->coordinates->getLatitude()->getValue(); // 48.8566
echo $user->settings->theme;            // 'dark'

class Product extends Model
{
    protected function slug(): Attribute
    {
        // Le set est automatiquement géré
        return AttributeProxy::nullable(SlugVO::class, column: 'slug');
    }
}

// La valeur est automatiquement normalisée avant stockage
$product->slug = 'Mon Article';  // Stocké comme 'mon-article'

// config/utils.php
return [
    'repositories' => [
        'github' => '[email protected]:andydefer/laravel-utils.git',
        'o2switch' => 'ssh://[email protected]/home/user/git/repo.git',
    ],
];

// config/utils.php
return [
    // Extensions par défaut pour le diff
    'default_extensions' => ['php', 'js', 'ts', 'css', 'html', 'json', 'yaml', 'md'],
    
    // Recettes d'extensions
    'extension_recipes' => [
        'frontend' => ['js', 'ts', 'tsx', 'jsx', 'vue', 'css', 'scss', 'sass', 'less', 'html', 'xml'],
        'backend' => ['php', 'py', 'rb', 'go', 'rs', 'java', 'c', 'cpp', 'h', 'hpp'],
        'fullstack' => ['php', 'js', 'ts', 'tsx', 'jsx', 'vue', 'css', 'scss', 'html'],
    ],
];

// config/utils.php
return [
    // ... autres configurations ...
    
    'deployment' => [
        'ssh_key' => env('DEPLOY_SSH_KEY', 'o2switch'),
        'remote_path' => env('DEPLOY_REMOTE_PATH', '~/sites/laravel-utils.com'),
        'git_branch' => env('DEPLOY_GIT_BRANCH', 'master'),
    ],
];
bash
php artisan vendor:publish --tag=utils-config
bash
php artisan vendor:publish --tag=utils-config