Skip to content

Mapping string into DateTimeImmutable #80

Open
@alvaro-octal

Description

@alvaro-octal

Hi,

I was taking a look at the issues, but haven't found any similar, is there a way to map a string into a DateTime/DateTimeImmutable by default? I mean, I have the following entity:

class UserFilter
{
    public ?string $email = null; // this is beeing auto mapped
    public ?array $roleIds = null; // this need to be handled with forMember, OK
    public ?DateTimeImmutable $createdAt = null; // Could this be mapped without a forMember?
}

And by using this mapper it results in a error, because the library is not able to determine a mapper for string to DateTimeImmutable of the createdAt property.

$this->config->registerMapping(DataType::ARRAY, UserFilter::class)
        ->forMember('roleIds', MapperHelper::ArrayOfInt('roleIds'));
return $this->mapper->map($params, UserFilter::class);
Message: Cannot assign string to property App\Application\Actions\User\Request\_Filter\UserFilter::$createdAt of type ?DateTimeImmutable

File: /var/www/vendor/mark-gerarts/auto-mapper-plus/src/PropertyAccessor/PropertyAccessor.php

Line: 50

Trace: #0 /var/www/vendor/mark-gerarts/auto-mapper-plus/src/MappingOperation/DefaultMappingOperation.php(106): AutoMapperPlus\PropertyAccessor\PropertyAccessor->setProperty(Object(App\Application\Actions\User\Request\_Filter\UserFilter), 'createdAt', '2021-11-02')

#1 /var/www/vendor/mark-gerarts/auto-mapper-plus/src/MappingOperation/DefaultMappingOperation.php(52): AutoMapperPlus\MappingOperation\DefaultMappingOperation->setDestinationValue(Object(App\Application\Actions\User\Request\_Filter\UserFilter), 'createdAt', '2021-11-02')

Of course I can add another forMember in order to map the string into a DateTimeImmutable but seems repetitive to me, is there a way to automate?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions