Weave 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' => '[email protected]',
    '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.
oxygen/data
oxygen/core
jengo/base
xiloryx/log-collector
a21ns1g4ts/filament-stripe
la-php/laravel-admin
artisan-build/community-prompts
vleroy/smart
ayles-software/laravel-australian-public-holidays
cboxdk/laravel-id-spatie
cboxdk/laravel-id-billing
cboxdk/laravel-id-billing-bridge
simple-as-fuck/php-performance-log
hsluv/hsluv
seampact/codegen
seampact/hooks
seampact/ir
mehedimi/laravel-amp-mail
happones/laravel-vue-i18n-generator
jwerd/inertia-table