Skip to content

Casting paginated responses to DTO #191

@boryn

Description

@boryn

I have a response with 'data' key, where there are several items. Normally (without automatic pagination), I'm able to get the collection of DTOs based on these suggestions: #186 using just $response->dto().

Now, I wanted to have the automatic pagination (I use OffsetPaginator), so instead of

$response = $this->connector()->send($this->request);

I used:

$paginator = $this->connector()->paginate($this->request);

everything is properly configured, as I can run

foreach($paginator as $response) {
    $pageData = $response->json('data');
}

as well I may create a collection of individual "raw" items in response: $paginator->collect('data').

I wanted to create DTOs based on the combined paginator results, and tried $paginator->dto(), but got: "Call to undefined method OffsetPaginator::dto()"

I know, I can easily build the DTOs manually:

$dtos = $paginator->collect('data')->map(
    fn ($post) =>  PostDto::fromItem($post),
);

but it would be nice to have a "helper/shortcut" method dto() as with single responses which would use the same createDtoFromResponse() method and we could similarly use $paginator->dto().

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions