feat(IncoSheets): support short parameter in FindAll and Search usecases

This commit is contained in:
Jérémy Munsch 2026-08-14 16:01:31 +02:00
parent 7383c3e01f
commit afdde63d9f
Signed by: forge-joniko
GPG key ID: D7EFE2F5CB6CC0E4
10 changed files with 43 additions and 25 deletions

View file

@ -42,26 +42,28 @@ final readonly class FindAllIncoSheets implements Usecase
filter: $filter
);
return $this->toOutput($page);
return $this->toOutput($page, $input->short);
}
/**
* @param Paginated<IncoSheet> $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);
}
}

View file

@ -13,6 +13,7 @@ final readonly class Input implements UsecaseInput
public Pagination $pagination,
public Sorter $sorter,
public ?Filter $filter = null,
public bool $short = false,
) {
}
}

View file

@ -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;

View file

@ -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<PaginatedIncoSheet> $incoSheets
* @param array<PaginatedIncoSheet|PaginatedShortIncoSheet> $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,
]);
}
}
}

View file

@ -29,26 +29,28 @@ final readonly class SearchIncoSheets implements Usecase
...$input->brands
);
return $this->toOutput($page);
return $this->toOutput($page, $input->short);
}
/**
* @param Paginated<IncoSheet> $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);
}
}

View file

@ -42,6 +42,7 @@ final readonly class IncoSheetsIndexController
pagination: $request->pagination(),
sorter: $request->sorter(),
filter: $request->filter(),
short: $request->isShort(),
);
}
}

View file

@ -42,6 +42,7 @@ final readonly class IncoSheetsSearchController
{
return new SearchInput(
$request->incoIdSearch(),
$request->isShort(),
...$request->brands()
);
}

View file

@ -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
);

View file

@ -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,

View file

@ -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,