Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapping string into DateTimeImmutable #80

Open
alvaro-octal opened this issue Nov 30, 2021 · 2 comments
Open

Mapping string into DateTimeImmutable #80

alvaro-octal opened this issue Nov 30, 2021 · 2 comments

Comments

@alvaro-octal
Copy link

alvaro-octal commented Nov 30, 2021

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?

@alvaro-octal alvaro-octal changed the title Mapping string into DatetimeInmutable Mapping string into DatetimeImmutable Nov 30, 2021
@alvaro-octal alvaro-octal changed the title Mapping string into DatetimeImmutable Mapping string into DateTimeImmutable Nov 30, 2021
@mark-gerarts
Copy link
Owner

No, this is not possible currently. If you have multiple recurring properties (createdAt, changedAt, deletedAt, ...), you could use copyMapping.

I can see the usefulness though, in a more generic sense: if a mapping exists from source type A to destination type B, automatically apply it. I am afraid that this is not a trivial thing to implement, especially for this case, since we're mapping from a scalar value to an object, which is something that still needs to be implemented as well.

I like the idea and will leave this open as food for thought.

@alvaro-octal
Copy link
Author

alvaro-octal commented Nov 30, 2021

Ok, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Release 2.0
  
To do
Development

No branches or pull requests

2 participants