Feat: Updating COntrollers to remove business logic moved to boboko/core

This commit is contained in:
2026-09-03 11:47:38 +03:00
parent b87e22381f
commit b2207a622c
6 changed files with 56 additions and 50 deletions
+6 -6
View File
@@ -19,8 +19,8 @@ final class CategoryListing
{
private function __construct(
public readonly ?ProductSort $sort,
public readonly ?int $minPrice,
public readonly ?int $maxPrice,
public readonly ?float $minPrice,
public readonly ?float $maxPrice,
public readonly bool $inStockOnly,
public readonly int $page,
) {}
@@ -29,8 +29,8 @@ public static function fromRequest(Request $request): self
{
return new self(
sort: ProductSort::tryFrom((string) $request->query('sort')),
minPrice: self::intOrNull($request->query('price_min')),
maxPrice: self::intOrNull($request->query('price_max')),
minPrice: self::floatOrNull($request->query('price_min')),
maxPrice: self::floatOrNull($request->query('price_max')),
inStockOnly: $request->boolean('in_stock'),
page: max(1, (int) $request->query('page', 1)),
);
@@ -79,8 +79,8 @@ public function isRefined(): bool
|| $this->page > 1;
}
private static function intOrNull(mixed $value): ?int
private static function floatOrNull(mixed $value): ?float
{
return is_numeric($value) ? (int) $value : null;
return is_numeric($value) ? (float) $value : null;
}
}
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace App\Catalog;
/**
* Presentation shaping — how a product listing/grid card is built from
* ProductService's localized array shape (list()/getById()/random() all
* return it). Deliberately not in boboko-core: `href` depends on this
* storefront's own routes, and another app built on the same core package
* could want an entirely different card shape. Kept in one place so
* HomeController/CategoryController don't each hand-write the same
* name/price/image/href mapping.
*/
final class ProductCard
{
/**
* @param array $product One item from ProductService's localized array shape.
* @return array{name: ?string, price: ?float, image: ?string, href: string}
*/
public static function fromIndexed(array $product): array
{
return [
'name' => $product['name'],
'price' => $product['price'],
'image' => $product['media'][0]['url'] ?? null,
'href' => route('product.show', ['id' => $product['id']]),
];
}
}