| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
-
- namespace App\StorePlan;
-
- use App\Models\Product;
- use App\Models\ProductImage;
- use App\Models\Store;
- use App\Models\StoreImage;
- use Illuminate\Support\Collection;
-
- /**
- * Photos for hero sections.
- *
- * A hero's "image" points to a photo by a short name: "upload:12" for a photo the merchant uploaded
- * in the Design tab, or "product:sambal-hijau" for the main photo of one of their products.
- * With no image chosen (or when the chosen photo is gone), the hero uses the store's product photos,
- * so a store looks finished as soon as it has photos.
- */
- final class HeroImages
- {
- public const string PATTERN = '/^(upload:[1-9][0-9]*|product:[a-z0-9]+(?:-[a-z0-9]+)*)$/';
-
- /** How many product photos the storefront gets for heroes with no photo of their own. */
- private const int FALLBACK_COUNT = 4;
-
- /**
- * Every hero section, with the page it is on.
- *
- * @param array<string, mixed> $plan
- * @return list<array{page_id: string, page_title: string, section_id: string, headline: string, variant: string, image: string|null}>
- */
- public static function sections(array $plan): array
- {
- $found = [];
-
- foreach ($plan['pages'] ?? [] as $page) {
- foreach ($page['sections'] ?? [] as $section) {
- if (($section['type'] ?? null) === 'hero') {
- $found[] = [
- 'page_id' => (string) ($page['id'] ?? ''),
- 'page_title' => (string) ($page['title'] ?? ''),
- 'section_id' => (string) ($section['id'] ?? ''),
- 'headline' => (string) ($section['content']['headline'] ?? ''),
- 'variant' => (string) ($section['variant'] ?? ''),
- 'image' => is_string($section['content']['image'] ?? null) ? $section['content']['image'] : null,
- ];
- }
- }
- }
-
- return $found;
- }
-
- /**
- * The plan with one hero changed: its photo, its layout, or both. Null for $image means "automatic".
- *
- * @param array<string, mixed> $plan
- * @return array<string, mixed>
- */
- public static function update(array $plan, string $sectionId, ?string $image, ?string $variant = null): array
- {
- foreach ($plan['pages'] ?? [] as $pageIndex => $page) {
- foreach ($page['sections'] ?? [] as $sectionIndex => $section) {
- if (($section['id'] ?? null) === $sectionId && ($section['type'] ?? null) === 'hero') {
- $plan['pages'][$pageIndex]['sections'][$sectionIndex]['content']['image'] = $image;
-
- if ($variant !== null) {
- $plan['pages'][$pageIndex]['sections'][$sectionIndex]['variant'] = $variant;
- }
- }
- }
- }
-
- return $plan;
- }
-
- /**
- * Photos the merchant (or the AI) can choose from: uploads first, then one per product.
- *
- * @return list<array{ref: string, label: string, url: string}>
- */
- public static function choices(Store $store): array
- {
- $choices = $store->images()->latest('id')->limit(30)->get()
- ->map(fn (StoreImage $image) => ['ref' => $image->reference(), 'label' => 'Uploaded photo', 'url' => $image->url()])
- ->all();
-
- foreach (self::productsWithPhotos($store, 60) as $product) {
- $choices[] = ['ref' => 'product:'.$product->slug, 'label' => $product->name, 'url' => $product->images->first()->url()];
- }
-
- return array_values($choices);
- }
-
- /**
- * References that do not point to a photo of this store.
- *
- * @param list<string> $refs
- * @return list<string>
- */
- public static function unknown(Store $store, array $refs): array
- {
- $known = array_column(self::resolve($store, $refs), 'ref');
-
- return array_values(array_diff($refs, $known));
- }
-
- /**
- * What the storefront needs: the address of every photo the plan points to, and a few product photos
- * for heroes without a photo of their own.
- *
- * @param array<string, mixed> $plan
- * @return array{refs: object, fallback: list<string>}
- */
- public static function forStorefront(Store $store, array $plan): array
- {
- $refs = array_values(array_unique(array_filter(array_column(self::sections($plan), 'image'))));
-
- return [
- // An object even when empty, so the frontend always gets {} and never [].
- 'refs' => (object) collect(self::resolve($store, $refs))->mapWithKeys(fn (array $photo) => [$photo['ref'] => $photo['url']])->all(),
- 'fallback' => array_values(self::productsWithPhotos($store, self::FALLBACK_COUNT)
- ->map(fn (Product $product) => $product->images->first()->url())
- ->all()),
- ];
- }
-
- /**
- * @param list<string> $refs
- * @return list<array{ref: string, url: string}>
- */
- private static function resolve(Store $store, array $refs): array
- {
- $uploadIds = [];
- $slugs = [];
-
- foreach ($refs as $ref) {
- [$kind, $value] = explode(':', $ref, 2) + [1 => ''];
-
- if ($kind === 'upload') {
- $uploadIds[] = (int) $value;
- } elseif ($kind === 'product') {
- $slugs[] = $value;
- }
- }
-
- $found = [];
-
- if ($uploadIds !== []) {
- foreach ($store->images()->whereKey($uploadIds)->get() as $image) {
- $found[] = ['ref' => $image->reference(), 'url' => $image->url()];
- }
- }
-
- if ($slugs !== []) {
- $products = $store->products()->whereIn('slug', $slugs)->whereHas('images')->with('images')->get();
-
- foreach ($products as $product) {
- $found[] = ['ref' => 'product:'.$product->slug, 'url' => $product->images->first()->url()];
- }
- }
-
- return $found;
- }
-
- /**
- * Active products that have at least one photo, newest first.
- *
- * @return Collection<int, Product>
- */
- private static function productsWithPhotos(Store $store, int $limit): Collection
- {
- return $store->products()
- ->active()
- ->whereHas('images')
- ->with('images')
- ->latest('id')
- ->limit($limit)
- ->get()
- ->filter(fn (Product $product) => $product->images->first() instanceof ProductImage)
- ->values()
- ->toBase();
- }
- }
|