Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Cqrs Laravel Package

digital-craftsman/cqrs

View on GitHub
Deep Wiki
Context7

DTO constructor examples

Interface

interface DTOConstructor
{
    /**
    * [@return](https://github.com/return) Command|Query
    *
    * [@psalm-template](https://github.com/psalm-template) T of Command|Query
    * [@psalm-param](https://github.com/psalm-param) class-string<T> $dtoClass
    * [@psalm-return](https://github.com/psalm-return) T
    */
    public function constructDTO(array $requestData, string $dtoClass): object;
}

See position in process

Construction through serializer

⭐ This DTO constructor is supplied with the bundle.

A possible implementation of a constructor is one that uses the Symfony serializer like this that is already built-in:

final readonly class SerializerDTOConstructor implements DTOConstructor
{
    public function __construct(
        private DenormalizerInterface $serializer,
    ) {
    }

    /**
     * [@psalm-template](https://github.com/psalm-template) T of Command|Query
     * [@psalm-param](https://github.com/psalm-param) class-string<T> $dtoClass
     * [@psalm-return](https://github.com/psalm-return) T
     */
    public function constructDTO(array $requestData, string $dtoClass): Command|Query
    {
        /** [@psalm-var](https://github.com/psalm-var) T */
        return $this->serializer->denormalize($requestData, $dtoClass);
    }
}

It transforms an array like the following:

[
    'userId' => '5118d20f-0ca5-40d1-99a8-1d1c20d675d6',
    'emailAddress' => 'user@example.com',
    'name' => [
        'firstName' => 'John',
        'lastName' => 'Doe'
    ],
    'password' => 'V6nP2mKmxn42Km3JG3x@'
]

Into a DTO like this:

final readonly class CreateUserAccountCommand implements Command
{
    public function __construct(
        public UserId $userId,
        public EmailAddress $emailAddress,
        public Name $name,
        public PlainTextPassword $password,
    ) {
    }
}

Obviously it needs the custom denormalizers for the values objects to be able to do so.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle