AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

HeroImages.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace App\StorePlan;
  3. use App\Models\Product;
  4. use App\Models\ProductImage;
  5. use App\Models\Store;
  6. use App\Models\StoreImage;
  7. use Illuminate\Support\Collection;
  8. /**
  9. * Photos for hero sections.
  10. *
  11. * A hero's "image" points to a photo by a short name: "upload:12" for a photo the merchant uploaded
  12. * in the Design tab, or "product:sambal-hijau" for the main photo of one of their products.
  13. * With no image chosen (or when the chosen photo is gone), the hero uses the store's product photos,
  14. * so a store looks finished as soon as it has photos.
  15. */
  16. final class HeroImages
  17. {
  18. public const string PATTERN = '/^(upload:[1-9][0-9]*|product:[a-z0-9]+(?:-[a-z0-9]+)*)$/';
  19. /** How many product photos the storefront gets for heroes with no photo of their own. */
  20. private const int FALLBACK_COUNT = 4;
  21. /**
  22. * Every hero section, with the page it is on.
  23. *
  24. * @param array<string, mixed> $plan
  25. * @return list<array{page_id: string, page_title: string, section_id: string, headline: string, variant: string, image: string|null}>
  26. */
  27. public static function sections(array $plan): array
  28. {
  29. $found = [];
  30. foreach ($plan['pages'] ?? [] as $page) {
  31. foreach ($page['sections'] ?? [] as $section) {
  32. if (($section['type'] ?? null) === 'hero') {
  33. $found[] = [
  34. 'page_id' => (string) ($page['id'] ?? ''),
  35. 'page_title' => (string) ($page['title'] ?? ''),
  36. 'section_id' => (string) ($section['id'] ?? ''),
  37. 'headline' => (string) ($section['content']['headline'] ?? ''),
  38. 'variant' => (string) ($section['variant'] ?? ''),
  39. 'image' => is_string($section['content']['image'] ?? null) ? $section['content']['image'] : null,
  40. ];
  41. }
  42. }
  43. }
  44. return $found;
  45. }
  46. /**
  47. * Every photo name the plan uses: hero photos and bento tile photos.
  48. *
  49. * @param array<string, mixed> $plan
  50. * @return list<string>
  51. */
  52. public static function refs(array $plan): array
  53. {
  54. $refs = [];
  55. foreach ($plan['pages'] ?? [] as $page) {
  56. foreach ($page['sections'] ?? [] as $section) {
  57. $content = is_array($section['content'] ?? null) ? $section['content'] : [];
  58. $candidates = ($section['type'] ?? null) === 'bento'
  59. ? array_column(is_array($content['tiles'] ?? null) ? $content['tiles'] : [], 'image')
  60. : [$content['image'] ?? null];
  61. foreach ($candidates as $ref) {
  62. if (is_string($ref) && $ref !== '') {
  63. $refs[$ref] = $ref;
  64. }
  65. }
  66. }
  67. }
  68. return array_values($refs);
  69. }
  70. /**
  71. * The plan with one hero changed: its photo, its layout, or both. Null for $image means "automatic".
  72. *
  73. * @param array<string, mixed> $plan
  74. * @return array<string, mixed>
  75. */
  76. public static function update(array $plan, string $sectionId, ?string $image, ?string $variant = null): array
  77. {
  78. foreach ($plan['pages'] ?? [] as $pageIndex => $page) {
  79. foreach ($page['sections'] ?? [] as $sectionIndex => $section) {
  80. if (($section['id'] ?? null) === $sectionId && ($section['type'] ?? null) === 'hero') {
  81. $plan['pages'][$pageIndex]['sections'][$sectionIndex]['content']['image'] = $image;
  82. if ($variant !== null) {
  83. $plan['pages'][$pageIndex]['sections'][$sectionIndex]['variant'] = $variant;
  84. }
  85. }
  86. }
  87. }
  88. return $plan;
  89. }
  90. /**
  91. * Photos the merchant (or the AI) can choose from: uploads first, then one per product.
  92. *
  93. * @return list<array{ref: string, label: string, url: string}>
  94. */
  95. public static function choices(Store $store): array
  96. {
  97. $choices = $store->images()->latest('id')->limit(30)->get()
  98. ->map(fn (StoreImage $image) => ['ref' => $image->reference(), 'label' => 'Uploaded photo', 'url' => $image->url()])
  99. ->all();
  100. foreach (self::productsWithPhotos($store, 60) as $product) {
  101. $choices[] = ['ref' => 'product:'.$product->slug, 'label' => $product->name, 'url' => $product->images->first()->url()];
  102. }
  103. return array_values($choices);
  104. }
  105. /**
  106. * References that do not point to a photo of this store.
  107. *
  108. * @param list<string> $refs
  109. * @return list<string>
  110. */
  111. public static function unknown(Store $store, array $refs): array
  112. {
  113. $known = array_column(self::resolve($store, $refs), 'ref');
  114. return array_values(array_diff($refs, $known));
  115. }
  116. /**
  117. * What the storefront needs: the address of every photo the plan points to, and a few product photos
  118. * for heroes without a photo of their own.
  119. *
  120. * @param array<string, mixed> $plan
  121. * @return array{refs: object, fallback: list<string>}
  122. */
  123. public static function forStorefront(Store $store, array $plan): array
  124. {
  125. $refs = self::refs($plan);
  126. return [
  127. // An object even when empty, so the frontend always gets {} and never [].
  128. 'refs' => (object) collect(self::resolve($store, $refs))->mapWithKeys(fn (array $photo) => [$photo['ref'] => $photo['url']])->all(),
  129. 'fallback' => array_values(self::productsWithPhotos($store, self::FALLBACK_COUNT)
  130. ->map(fn (Product $product) => $product->images->first()->url())
  131. ->all()),
  132. ];
  133. }
  134. /**
  135. * @param list<string> $refs
  136. * @return list<array{ref: string, url: string}>
  137. */
  138. private static function resolve(Store $store, array $refs): array
  139. {
  140. $uploadIds = [];
  141. $slugs = [];
  142. foreach ($refs as $ref) {
  143. [$kind, $value] = explode(':', $ref, 2) + [1 => ''];
  144. if ($kind === 'upload') {
  145. $uploadIds[] = (int) $value;
  146. } elseif ($kind === 'product') {
  147. $slugs[] = $value;
  148. }
  149. }
  150. $found = [];
  151. if ($uploadIds !== []) {
  152. foreach ($store->images()->whereKey($uploadIds)->get() as $image) {
  153. $found[] = ['ref' => $image->reference(), 'url' => $image->url()];
  154. }
  155. }
  156. if ($slugs !== []) {
  157. $products = $store->products()->whereIn('slug', $slugs)->whereHas('images')->with('images')->get();
  158. foreach ($products as $product) {
  159. $found[] = ['ref' => 'product:'.$product->slug, 'url' => $product->images->first()->url()];
  160. }
  161. }
  162. return $found;
  163. }
  164. /**
  165. * Active products that have at least one photo, newest first.
  166. *
  167. * @return Collection<int, Product>
  168. */
  169. private static function productsWithPhotos(Store $store, int $limit): Collection
  170. {
  171. return $store->products()
  172. ->active()
  173. ->whereHas('images')
  174. ->with('images')
  175. ->latest('id')
  176. ->limit($limit)
  177. ->get()
  178. ->filter(fn (Product $product) => $product->images->first() instanceof ProductImage)
  179. ->values()
  180. ->toBase();
  181. }
  182. }