PHP code example of utilitarian-dev / utilitarian-laravel-toolkit

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

    

utilitarian-dev / utilitarian-laravel-toolkit example snippets


class GetUserQuery extends Query
{
    public function __construct(
        private readonly int $userId,
    ) {}

    public function execute(): ?User
    {
        return User::query()->find($this->userId);
    }
}

// Execute
$user = GetUserQuery::make(userId: 1)->dispatch();
// or
$user = QueryBus::execute(GetUserQuery::make(userId: 1));

class CreateUserCommand extends Command
{
    public function __construct(
        private readonly string $email,
        private readonly string $name,
    ) {}

    public function execute(): User
    {
        return User::query()->create([
            'email' => $this->email,
            'name' => $this->name,
        ]);
    }
}

// Execute
$user = CreateUserCommand::make(email: '...', name: '...')->dispatch();

class RegisterUserAction extends Action
{
    public function __construct(
        private readonly string $email,
        private readonly string $name,
    ) {}

    public function execute(): User
    {
        $user = CreateUserCommand::make(
            email: $this->email,
            name: $this->name
        )->dispatch();

        SendWelcomeEmailCommand::make(userId: $user->id)->dispatch();

        return $user;
    }
}

// Execute
$user = RegisterUserAction::make(email: '...', name: '...')->dispatch();
// or
$user = ActionBus::execute(RegisterUserAction::make(email: '...', name: '...'));

class SyncProductToSearchIndexCommand extends Command
{
    private SearchIndexClient $search;

    public function __construct(
        private readonly Product $product,
    ) {}

    public function boot(SearchIndexClient $search): void
    {
        $this->search = $search;
    }

    public function execute(): void
    {
        $this->search->index('products', [
            'id' => $this->product->id,
            'name' => $this->product->name,
        ]);
    }
}

return [
    'cqrs' => [
        'query_middleware' => [
            \Utilitarian\Cqrs\Middleware\CachingMiddleware::class,
        ],
        'command_middleware' => [
            \Utilitarian\Cqrs\Middleware\TransactionMiddleware::class,
        ],
        'action_middleware' => [
            \Utilitarian\Cqrs\Middleware\AuthorizationMiddleware::class,
        ],
    ],
];

class MyQuery extends Query
{
    public function middleware(): array
    {
        return [CustomMiddleware::class];
    }

    public function excludeMiddleware(): array
    {
        return [UnwantedMiddleware::class];
    }
}

use Utilitarian\StateMachine\Traits\HasStateMachine;

class Order extends Model
{
    use HasStateMachine;
}

$order->state()->set('notes', 'Special handling');
$order->state()->get('notes');
$order->state()->has('notes');
$order->state()->forget('notes');

$order->state()->transitionTo(OrderState::Paid);
$order->state()->current(); // OrderState::Paid
$order->state()->is(OrderState::Paid); // true

enum OrderState: string implements FlowHandler
{
    case Pending = 'pending';
    case Paid = 'paid';
    case Shipped = 'shipped';

    public function canTransitionTo(UnitEnum $target): bool
    {
        return match ($this) {
            self::Pending => $target === self::Paid,
            self::Paid => $target === self::Shipped,
            self::Shipped => false,
        };
    }
}

$order->state()->transitionTo(OrderState::Paid); // OK
$order->state()->transitionTo(OrderState::Shipped); // throws InvalidTransitionException

use Utilitarian\Data\Data;

class UserData extends Data
{
    public function __construct(
        public readonly int $id,
        public readonly string $email,
        public readonly string $name,
    ) {}
}

// Create from array
$userData = UserData::from([
    'id' => 1,
    'email' => '[email protected]',
    'name' => 'John Doe',
]);

// Convert back to array
$array = $userData->toArray();

use Utilitarian\Data\Attributes\MapField;

class UserData extends Data
{
    public function __construct(
        public readonly int $id,
        #[MapField('email_address')]
        public readonly string $email,
    ) {}
}
bash
php artisan vendor:publish --tag=utilitarian-config
bash
php artisan migrate