PHP code example of jptagorda / laravel-package-starterkit

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

    

jptagorda / laravel-package-starterkit example snippets




declare(strict_types=1);

namespace Acme\MyPackage\Actions;

final readonly class CreateEntityAction
{
    public function __construct(
        private EntityRepositoryContract $repository,
    ) {}

    public function __invoke(EntityData $data): Entity
    {
        return $this->repository->create($data);
    }
}



declare(strict_types=1);

namespace Acme\MyPackage\Contracts;

interface EntityRepositoryContract
{
    public function find(int $id): ?Entity;
    public function create(EntityData $data): Entity;
}



declare(strict_types=1);

namespace Acme\MyPackage\ValueObjects;

final readonly class Email
{
    public function __construct(
        public string $value,
    ) {
        if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
            throw ValidationException::invalidField('email', 'Invalid email format');
        }
    }
}

use Acme\MyPackage\Exceptions\ValidationException;

throw ValidationException::invalidField('email', 'Must be a valid email');
throw ValidationException::



declare(strict_types=1);

it('creates entity with valid data', function (): void {
    $action = app(CreateEntityAction::class);

    $result = $action(new EntityData(name: 'Test'));

    expect($result)->toBeInstanceOf(Entity::class);
});

it('throws exception for invalid data', function (): void {
    $action = app(CreateEntityAction::class);

    expect(fn () => $action(new EntityData(name: '')))
        ->toThrow(ValidationException::class);
});