Skip to content

Impossible to map to a nested collection of Target-Classes? #293

@FranzBruckner

Description

@FranzBruckner

I am struggling now since hours, maybe I am doing something wrong, maybe there is a bug or it's just not possible.

Basically I have a collection in the src and the target-object. The elements are all of the same type (typehinted, docblocks, everything there).
But I am no capable to map the elements from the source-objects collection to the type of the target-objects collection.
The elements of the collection are mapped but not to the target type :-(

Again, please apologize if I missed that somewhere in the documentation.

Call

assert($searchResult instanceof LegacyPaginatedData);
$response = $autoMapper->map($searchResult, UserSubusersResponse::class, [
      'groups' => ['user:subuser'],
  ]);

Result

UserSubusersResponse::$currentPage is fine.
UserSubusersResponse::$data is a collection of UserEntities (instead of a collection of UserSubuserResources)

Target-Objects

// Target (Root)
#[Groups(['user:subuser'])]
final class UserSubusersResponse
{
    #[MapFrom(LegacyPaginatedData::class)]
    public int $currentPage;

    /** @var iterable<UserSubuserResource> */
    #[MapFrom(LegacyPaginatedData::class)]
    public iterable $data = [];
}


#[Groups(['user:subuser'])]
final class UserSubuserResource
{
    #[MapFrom(User::class, property: 'uid')]
    public int $uid;

    #[MapFrom(User::class)]
    public string $email;
}

Source-Objects

/**
 * @template T
 */
#[Groups(['user:subuser'])]
final readonly class LegacyPaginatedData
{
    /**
     * @param iterable<T> $data
     */
    private function __construct(
        /** @var iterable<T> */
        public array $data,
        public int $currentPage,
        public int $currentPageSize,
        public int $pageCount,
        public int $totalCount,
        public string $orderBy = 'timeCreated',
        public string $orderDirection = 'ASC',
    ) {
    }
}


#[Groups(['user:subuser'])]
class UserEntity {
  // protected properties and public getters
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions