mark-gerarts / mark-gerarts/automapper-plus

Mapping string into DateTimeImmutable

Open
#80 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
PHP
Stars
562
Forks
32
PR merge metrics
No merged PRs in 30d

Description

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?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the failed assignment through PropertyAccessor/PropertyAccessor.php and DefaultMappingOperation/DefaultMappingOperation.php, then inspect how existing type converters are registered and tested. Done means a string source value can map to DateTimeImmutable without a per-member mapper, while preserving the existing explicit forMember path.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.