generated from boboko/starter
Feat: Updating Product Cards, Listing Pages and SearchController
This commit is contained in:
@@ -2,8 +2,6 @@
|
||||
|
||||
namespace App\Catalog;
|
||||
|
||||
use Lunar\Models\Product;
|
||||
|
||||
/**
|
||||
* Presentation shaping — how a storefront product listing/grid card is built:
|
||||
* name, price, image, href. Deliberately not in boboko-core: `href` depends on
|
||||
@@ -12,26 +10,13 @@
|
||||
* CategoryController / ProductController / SearchController don't each
|
||||
* hand-write the same name/price/image/href mapping.
|
||||
*
|
||||
* Two sources, because the storefront reads products both ways: a hydrated
|
||||
* Eloquent model (full-text search via ProductSearchService), or a localized
|
||||
* index array (ProductService::list()/getById()/random()). Model callers must
|
||||
* eager-load `variants.prices` and `media`.
|
||||
* One source: a localized index array — ProductService::list()/getById()/
|
||||
* random(), and now ProductSearchService::search() too, all return the exact
|
||||
* same document shape (see ProductListingResult), so this is the only mapping
|
||||
* every storefront listing page needs.
|
||||
*/
|
||||
final class ProductCard
|
||||
{
|
||||
/**
|
||||
* @return array{name: ?string, price: ?string, image: ?string, href: string}
|
||||
*/
|
||||
public static function fromModel(Product $product): array
|
||||
{
|
||||
return [
|
||||
'name' => $product->translateAttribute('name'), //@todo check this
|
||||
'price' => $product->variants->first()?->prices->first()?->price->decimal, //@todo check this
|
||||
'image' => $product->media->first()?->getUrl(),
|
||||
'href' => route('product.show', ['id' => $product->id]),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $product one item from ProductService's localized array shape
|
||||
* @return array{name: ?string, price: ?string, image: ?string, href: string}
|
||||
|
||||
Reference in New Issue
Block a user