From afdde63d9f6264a025306ce72e83ac0eeb5b0b31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Fri, 14 Aug 2026 16:01:31 +0200 Subject: [PATCH] feat(IncoSheets): support short parameter in FindAll and Search usecases --- .../IncoSheets/FindAll/FindAllIncoSheets.php | 18 ++++++++++-------- .../Usecase/IncoSheets/FindAll/Input.php | 1 + .../Usecase/IncoSheets/Search/Input.php | 1 + .../Usecase/IncoSheets/Search/Output.php | 9 ++++++++- .../IncoSheets/Search/SearchIncoSheets.php | 18 ++++++++++-------- .../IncoSheets/IncoSheetsIndexController.php | 1 + .../IncoSheets/IncoSheetsSearchController.php | 1 + .../IncoSheets/IncoSheetsIndexResponse.php | 6 +++--- .../IncoSheets/IncoSheetsSearchResponse.php | 8 ++++---- .../IncoSheets/IncoSheetsSearchView.php | 5 ++++- 10 files changed, 43 insertions(+), 25 deletions(-) diff --git a/src/Application/Usecase/IncoSheets/FindAll/FindAllIncoSheets.php b/src/Application/Usecase/IncoSheets/FindAll/FindAllIncoSheets.php index d605046..ba92660 100644 --- a/src/Application/Usecase/IncoSheets/FindAll/FindAllIncoSheets.php +++ b/src/Application/Usecase/IncoSheets/FindAll/FindAllIncoSheets.php @@ -42,26 +42,28 @@ final readonly class FindAllIncoSheets implements Usecase filter: $filter ); - return $this->toOutput($page); + return $this->toOutput($page, $input->short); } /** * @param Paginated $resultPage */ - private function toOutput(Paginated $resultPage): Output + private function toOutput(Paginated $resultPage, bool $short): Output { $pagination = $resultPage->getPagination(); + $items = array_map( + static fn (IncoSheet $sheet): PaginatedIncoSheet | PaginatedShortIncoSheet => $short + ? PaginatedShortIncoSheet::fromDomain($sheet) + : PaginatedIncoSheet::fromDomain($sheet), + $resultPage->getItems() + ); + return new Output( - incoSheets: array_map([$this, 'toPaginatedIncoSheet'], $resultPage->getItems()), + incoSheets: $items, total: $resultPage->getTotal(), page: $pagination->pageNumber(), perPage: $pagination->pageSize(), ); } - - private function toPaginatedIncoSheet(IncoSheet $incoSheet): PaginatedIncoSheet - { - return PaginatedIncoSheet::fromDomain($incoSheet); - } } diff --git a/src/Application/Usecase/IncoSheets/FindAll/Input.php b/src/Application/Usecase/IncoSheets/FindAll/Input.php index dbfad82..fd67112 100644 --- a/src/Application/Usecase/IncoSheets/FindAll/Input.php +++ b/src/Application/Usecase/IncoSheets/FindAll/Input.php @@ -13,6 +13,7 @@ final readonly class Input implements UsecaseInput public Pagination $pagination, public Sorter $sorter, public ?Filter $filter = null, + public bool $short = false, ) { } } diff --git a/src/Application/Usecase/IncoSheets/Search/Input.php b/src/Application/Usecase/IncoSheets/Search/Input.php index a842f59..6165e57 100644 --- a/src/Application/Usecase/IncoSheets/Search/Input.php +++ b/src/Application/Usecase/IncoSheets/Search/Input.php @@ -14,6 +14,7 @@ final readonly class Input implements UsecaseInput public function __construct( public IncoIdQuery $query, + public bool $short = false, string ...$brands, ) { $this->brands = $brands; diff --git a/src/Application/Usecase/IncoSheets/Search/Output.php b/src/Application/Usecase/IncoSheets/Search/Output.php index 15624a1..cfae3bd 100644 --- a/src/Application/Usecase/IncoSheets/Search/Output.php +++ b/src/Application/Usecase/IncoSheets/Search/Output.php @@ -5,11 +5,12 @@ declare(strict_types=1); namespace Application\Usecase\IncoSheets\Search; use Shared\DDD\Usecase\Output as UsecaseOutput; +use Webmozart\Assert\Assert; final readonly class Output implements UsecaseOutput { /** - * @param array $incoSheets + * @param array $incoSheets */ public function __construct( public array $incoSheets = [], @@ -17,5 +18,11 @@ final readonly class Output implements UsecaseOutput public int $page = 1, public int $perPage = 10, ) { + foreach ($this->incoSheets as $incoSheet) { + Assert::isAnyOf($incoSheet, [ + PaginatedShortIncoSheet::class, + PaginatedIncoSheet::class, + ]); + } } } diff --git a/src/Application/Usecase/IncoSheets/Search/SearchIncoSheets.php b/src/Application/Usecase/IncoSheets/Search/SearchIncoSheets.php index 37369ee..03032b1 100644 --- a/src/Application/Usecase/IncoSheets/Search/SearchIncoSheets.php +++ b/src/Application/Usecase/IncoSheets/Search/SearchIncoSheets.php @@ -29,26 +29,28 @@ final readonly class SearchIncoSheets implements Usecase ...$input->brands ); - return $this->toOutput($page); + return $this->toOutput($page, $input->short); } /** * @param Paginated $resultPage */ - private function toOutput(Paginated $resultPage): Output + private function toOutput(Paginated $resultPage, bool $short): Output { $pagination = $resultPage->getPagination(); + $items = array_map( + static fn (IncoSheet $sheet): PaginatedIncoSheet | PaginatedShortIncoSheet => $short + ? PaginatedShortIncoSheet::fromDomain($sheet) + : PaginatedIncoSheet::fromDomain($sheet), + $resultPage->getItems() + ); + return new Output( - incoSheets: array_map([$this, 'toPaginatedIncoSheet'], $resultPage->getItems()), + incoSheets: $items, total: $resultPage->getTotal(), page: $pagination->pageNumber(), perPage: $pagination->pageSize(), ); } - - private function toPaginatedIncoSheet(IncoSheet $incoSheet): PaginatedIncoSheet - { - return PaginatedIncoSheet::fromDomain($incoSheet); - } } diff --git a/src/Presentation/Api/V1/Http/Controllers/IncoSheets/IncoSheetsIndexController.php b/src/Presentation/Api/V1/Http/Controllers/IncoSheets/IncoSheetsIndexController.php index b309a55..333a4ec 100644 --- a/src/Presentation/Api/V1/Http/Controllers/IncoSheets/IncoSheetsIndexController.php +++ b/src/Presentation/Api/V1/Http/Controllers/IncoSheets/IncoSheetsIndexController.php @@ -42,6 +42,7 @@ final readonly class IncoSheetsIndexController pagination: $request->pagination(), sorter: $request->sorter(), filter: $request->filter(), + short: $request->isShort(), ); } } diff --git a/src/Presentation/Api/V1/Http/Controllers/IncoSheets/IncoSheetsSearchController.php b/src/Presentation/Api/V1/Http/Controllers/IncoSheets/IncoSheetsSearchController.php index 8de2bfd..4cc0742 100644 --- a/src/Presentation/Api/V1/Http/Controllers/IncoSheets/IncoSheetsSearchController.php +++ b/src/Presentation/Api/V1/Http/Controllers/IncoSheets/IncoSheetsSearchController.php @@ -42,6 +42,7 @@ final readonly class IncoSheetsSearchController { return new SearchInput( $request->incoIdSearch(), + $request->isShort(), ...$request->brands() ); } diff --git a/src/Presentation/Api/V1/Http/Responses/IncoSheets/IncoSheetsIndexResponse.php b/src/Presentation/Api/V1/Http/Responses/IncoSheets/IncoSheetsIndexResponse.php index be90103..174761d 100644 --- a/src/Presentation/Api/V1/Http/Responses/IncoSheets/IncoSheetsIndexResponse.php +++ b/src/Presentation/Api/V1/Http/Responses/IncoSheets/IncoSheetsIndexResponse.php @@ -49,9 +49,9 @@ final readonly class IncoSheetsIndexResponse implements ApiResponse ); $items = array_map( - fn (PaginatedIncoSheet | PaginatedShortIncoSheet $sheet): array => match ($sheet::class) { - PaginatedShortIncoSheet::class => $this->fromPaginatedShortIncoSheet($sheet)->toArray(), - PaginatedIncoSheet::class => $this->fromPaginatedIncoSheet($sheet)->toArray(), + fn (PaginatedIncoSheet | PaginatedShortIncoSheet $sheet): array => match (true) { + $sheet instanceof PaginatedShortIncoSheet => $this->fromPaginatedShortIncoSheet($sheet)->toArray(), + $sheet instanceof PaginatedIncoSheet => $this->fromPaginatedIncoSheet($sheet)->toArray(), }, $this->view->incoSheets ); diff --git a/src/Presentation/Api/V1/Http/Responses/IncoSheets/IncoSheetsSearchResponse.php b/src/Presentation/Api/V1/Http/Responses/IncoSheets/IncoSheetsSearchResponse.php index fb8d579..32b3297 100644 --- a/src/Presentation/Api/V1/Http/Responses/IncoSheets/IncoSheetsSearchResponse.php +++ b/src/Presentation/Api/V1/Http/Responses/IncoSheets/IncoSheetsSearchResponse.php @@ -49,9 +49,9 @@ final readonly class IncoSheetsSearchResponse implements ApiResponse ); $items = array_map( - fn (PaginatedIncoSheet | PaginatedShortIncoSheet $sheet): array => match ($sheet::class) { - PaginatedShortIncoSheet::class => $this->fromPaginatedIncoSheetToShort($sheet)->toArray(), - PaginatedIncoSheet::class => $this->fromPaginatedIncoSheetToFull($sheet)->toArray(), + fn (PaginatedIncoSheet | PaginatedShortIncoSheet $sheet): array => match (true) { + $sheet instanceof PaginatedShortIncoSheet => $this->fromPaginatedIncoSheetToShort($sheet)->toArray(), + $sheet instanceof PaginatedIncoSheet => $this->fromPaginatedIncoSheetToFull($sheet)->toArray(), }, $this->view->incoSheets ); @@ -62,7 +62,7 @@ final readonly class IncoSheetsSearchResponse implements ApiResponse ]); } - private function fromPaginatedIncoSheetToShort(PaginatedIncoSheet $sheet): IncoShortSheetDto + private function fromPaginatedIncoSheetToShort(PaginatedShortIncoSheet $sheet): IncoShortSheetDto { return new IncoShortSheetDto( id: (string) $sheet->id, diff --git a/src/Presentation/Api/V1/Presenters/IncoSheets/IncoSheetsSearchView.php b/src/Presentation/Api/V1/Presenters/IncoSheets/IncoSheetsSearchView.php index a9201e4..89e8983 100644 --- a/src/Presentation/Api/V1/Presenters/IncoSheets/IncoSheetsSearchView.php +++ b/src/Presentation/Api/V1/Presenters/IncoSheets/IncoSheetsSearchView.php @@ -9,7 +9,10 @@ use Application\Usecase\IncoSheets\Search\Output; final readonly class IncoSheetsSearchView { /** - * @param \Application\Usecase\IncoSheets\Search\PaginatedIncoSheet[] $incoSheets + * @param array< + * \Application\Usecase\IncoSheets\Search\PaginatedIncoSheet + * |\Application\Usecase\IncoSheets\Search\PaginatedShortIncoSheet + * > $incoSheets */ public function __construct( public array $incoSheets,