PHP code example of phpgears / cqrs

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

    

phpgears / cqrs example snippets




use Gears\CQRS\AbstractCommand;

class CreateUserCommand extends AbstractCommand
{
    public static function fromPersonalData(
        string $name,
        string lastname,
        \DateTimeImmutable $birthDate
    ): self {
        return new self([
            'name' => $name,
            'lastname' => $lastname,
            'birthDate' => $birthDate->format('U'),
        ]);
    }
}

use Gears\CQRS\AbstractEmptyCommand;

class CreateUserCommand extends AbstractEmptyCommand
{
    public static function instance(): self {
        return new self();
    }
}

use Gears\CQRS\AbstractQuery;

class FindUserQuery extends AbstractQuery
{
    public static function fromName(string $name): self 
    {
        return new self(['name' => $name]);
    }
}

use Gears\CQRS\AbstractEmptyQuery;

class FindAllUsersQuery extends AbstractEmptyQuery
{
    public static function instance(): self {
        return new self();
    }
}

class CreateUserCommandHandler extends AbstractCommandHandler
{
    protected function getSupportedCommandType(): string
    {
        return CreateUserCommand::class;
    }

    protected function handleCommand(Command $command): void
    {
        /* @var CreateUserCommand $command */

        $user = new User(
            $command->getName(),
            $command->getLastname(),
            $command->getBirthDate()
        );

        // ...
    }
}

class FindUserQueryHandler extends AbstractQueryHandler
{
    protected function getSupportedQueryType(): string
    {
        return FindUserQuery::class;
    }

    protected function handleCommand(Query $query): DTO
    {
        /* @var FindUserQuery $query */

        // Retrieve user from persistence by it's name $query->getName()

        return new UserDTO(/* parameters */);
    }
}