PHP code example of 1tomany / rich-bundle

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

    

1tomany / rich-bundle example snippets




namespace App\Domain\Account\Contract\Repository;

use App\Entity\Account;

interface AccountRepositoryInterface
{
    public function findOneById(?int $accountId): ?Account;
}



namespace App\Repository;

use App\Entity\Account;
use App\Domain\Account\Contract\Repository\AccountRepositoryInterface;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @extends ServiceEntityRepository<Account>
 */
class AccountRepository extends ServiceEntityRepository implements AccountRepositoryInterface
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Account::class);
    }

    /**
     * @see App\Domain\Account\Contract\Repository\AccountRepositoryInterface
     */
    #[\Override]
    public function findOneById(?int $accountId): ?Account
    {
        return $accountId ? $this->find($accountId) : null;
    }
}



namespace App\Domain\Account\Contract\Exception;

interface ExceptionInterface extends \Throwable
{
}



namespace App\Domain\Account\Action\Command;

use OneToMany\RichBundle\Contract\Action\CommandInterface;

final readonly class CreateAccountCommand implements CommandInterface
{
    public function __construct(
        public ?int $userId,
        public string $name,
        public string $company,
        public string $email,
        public ?string $notes,
        public ?\DateTimeImmutable $founded = null,
        public ?string $ipAddress = null,
    ) {
    }
}



namespace App\Domain\Account\Action\Input;

use App\Entity\User;
use App\Domain\Account\Action\Command\CreateAccountCommand;
use OneToMany\RichBundle\Attribute\SourceIpAddress;
use OneToMany\RichBundle\Attribute\SourceRequest;
use OneToMany\RichBundle\Attribute\SourceUser;
use OneToMany\RichBundle\Contract\Action\CommandInterface;
use OneToMany\RichBundle\Contract\Action\InputInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @implements InputInterface<CreateAccountCommand>
 */
final readonly class CreateAccountInput implements InputInterface
{
    public function __construct(
        #[SourceUser]
        #[Assert\Email]
        #[Assert\Length(max: 128)]
        public ?UserInterface $user,

        #[SourceRequest]
        #[Assert\NotBlank]
        #[Assert\Length(min: 4, max: 128)]
        public ?string $name,

        #[SourceRequest]
        #[Assert\Length(min: 4, max: 48)]
        public ?string $company,

        #[SourceRequest]
        #[Assert\NotBlank]
        #[Assert\Email]
        #[Assert\Length(max: 128)]
        public ?string $email,

        #[SourceRequest(nullify: true)]
        #[Assert\Length(max: 1024)]
        public ?string $notes,

        #[SourceRequest(nullify: true)]
        #[Assert\Range(
            min: '1900-01-01',
            max: 'today',
        )]
        public ?\DateTimeImmutable $founded = null,

        #[SourceIpAddress(nullify: true)]
        #[Assert\Ip(version: 'all')]
        public ?string $ipAddress = null,
    ) {
    }

    /**
     * @see OneToMany\RichBundle\Contract\Action\CommandInterface
     */
    #[\Override]
    public function toCommand(): CommandInterface
    {
        return new CreateAccountCommand(
            $this->user?->getId(),
            (string) $this->name,
            (string) $this->company,
            (string) $this->email,
            $this->notes,
            $this->founded,
            $this->ipAddress,
        );
    }
}



namespace App\Domain\Account\Action\Input;

use App\Domain\Account\Action\Command\ReadAccountCommand;
use OneToMany\RichBundle\Attribute\SourceRequest;
use OneToMany\RichBundle\Contract\Action\CommandInterface;
use OneToMany\RichBundle\Contract\Action\InputInterface;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @implements InputInterface<ReadAccountCommand>
 */
final readonly class ReadAccountInput implements InputInterface
{
    public function __construct(
        #[SourceQuery('email')]
        #[SourceQuery('username')]
        #[Assert\Email]
        #[Assert\NotBlank]
        #[Assert\Length(max: 128)]
        public string $username,
    ) {
    }

    /**
     * @see OneToMany\RichBundle\Contract\Action\CommandInterface
     */
    public function toCommand(): CommandInterface
    {
        return new ReadAccountCommand($this->username);
    }
}



namespace App\Domain\Account\Action\Input;

use OneToMany\RichBundle\Attribute\SourceRequest;
use OneToMany\RichBundle\Contract\Action\InputInterface;

final readonly class UpdateAccountInput implements InputInterface
{
    public function __construct(
        #[SourceRequest(
            trim: true,
            nullify: true,
        )]
        public string $name,

        #[SourceRequest(
            trim: true,
            nullify: true,
        )]
        public ?string $email,

        #[SourceRequest(
            trim: false,
            nullify: false,
        )]
        public string $notes,

        #[SourceRequest(
            trim: true,
            nullify: false,
        )]
        public int $pin,

        #[SourceRequest(
            trim: true,
            nullify: true,
        )]
        public ?\DateTimeImmutable $birth,
    ) {
    }
}



namespace App\Domain\Account\Action\Handler;

use App\Entity\Account;
use App\Domain\Account\Action\Command\CreateAccountCommand;
use App\Domain\Account\Action\Input\CreateCustomerInput;
use App\Domain\Account\Action\Handler\Exception\UserNotFoundForCreatingAccountException;
use App\Domain\User\Contract\Repository\UserRepositoryInterface;
use Doctrine\ORM\EntityManagerInterface;
use OneToMany\RichBundle\Action\Result\HandlerResult;
use OneToMany\RichBundle\Contract\Action\CommandInterface;
use OneToMany\RichBundle\Contract\Action\HandlerInterface;
use OneToMany\RichBundle\Contract\Action\ResultInterface;

/**
 * @implements HandlerInterface<CreateAccountCommand, ResultInterface<Account>>
 */
final readonly class CreateAccountHandler implements HandlerInterface
{
    public function __construct(
        private UserRepositoryInterface $userRepository,
        private EntityManagerInterface $entityManager,
    ) {
    }

    /**
     * @see OneToMany\RichBundle\Contract\Action\HandlerInterface
     */
    #[\Override]
    public function handle(CommandInterface $command): ResultInterface
    {
        // Create the Account entity
        $account = Account::create(...[
            'name' => $command->name,
            'company' => $command->company,
            'email' => $command->email,
            'notes' => $command->notes,
            'founded' => $command->founded,
            'ipAddress' => $command->ipAddress,
        ]);

        // Attempt to find author
        if (null !== $command->author) {
            $author = $this->userRepository->findOneById(...[
                'userId' => $command->userId,
            ]);

            if (null === $author) {
                throw new UserNotFoundForCreatingAccountException($command->author);
            }

            $account->setAuthor($author);
        }

        $this->entityManager->persist($account);
        $this->entityManager->flush();

        return HandlerResult::created($account);
    }
}



namespace App\Domain\Account\Action\Handler\Exception;

use App\Domain\Account\Exception\DomainException;
use OneToMany\RichBundle\Attribute\HasUserMessage;

use function sprintf;

#[HasUserMessage]
final class UserNotFoundForCreatingAccountException extends DomainException
{
    public function __construct(
        ?string $username,
        ?\Throwable $previous = null,
    ) {
        parent::__construct(sprintf('The account could not be created because the user "%s" could not be found.', $username), 404, $previous);
    }
}



namespace App\Domain\Account\Framework\Controller\API;

use App\Entity\Account;
use App\Domain\Account\Action\Handler\CreateAccountHandler;
use App\Domain\Account\Action\Input\CreateAccountInput;
use OneToMany\RichBundle\Contract\Action\ResultInterface;
use Symfony\Component\HttpKernel\Attribute\AsController;

#[AsController]
final readonly class CreateAccountController
{
    public function __construct(
        private CreateAccountHandler $createAccountHandler,
    ) {
    }

    /**
     * @return ResultInterface<Account>
     */
    public function __invoke(CreateAccountInput $createAccountInput): ResultInterface
    {
        $result = $this->createAccountHandler->handle(...[
            'command' => $createAccountInput->toCommand(),
        ]);

        return $result->withGroups(['read']);
    }
}
shell
php bin/console make:rich-domain Account

src/
  Domain/
    Account/
      Action/
        Command/
          CreateAccountCommand.php
          ReadAccountCommand.php
        Event/
          AccountCreated.php
        Handler/
          Exception/
          CreateAccountHandler.php
          ReadAccountHandler.php
        Input/
          CreateAccountInput.php
          ReadAccountInput.php
      Contract/
        Enum/
        Exception/
          ExceptionInterface.php
        Repository/
          AccountRepositoryInterface.php
      Exception/
        DomainException.php
        RuntimeException.php
      Framework/
        Command/
          ReadAccountCommand.php
        Controller/
          API/
          Web/