AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

FeaturedProducts.php 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\StorePlan;
  3. /**
  4. * Finds the "featured product" sections of a plan and the real products they point to.
  5. */
  6. final class FeaturedProducts
  7. {
  8. /**
  9. * Every featured product section, with the page it is on.
  10. *
  11. * @param array<string, mixed> $plan
  12. * @return list<array{page_id: string, page_title: string, section_id: string, name: string, product_slug: string|null}>
  13. */
  14. public static function sections(array $plan): array
  15. {
  16. $found = [];
  17. foreach ($plan['pages'] ?? [] as $page) {
  18. foreach ($page['sections'] ?? [] as $section) {
  19. if (($section['type'] ?? null) === 'featured_product') {
  20. $found[] = [
  21. 'page_id' => (string) ($page['id'] ?? ''),
  22. 'page_title' => (string) ($page['title'] ?? ''),
  23. 'section_id' => (string) ($section['id'] ?? ''),
  24. 'name' => (string) ($section['content']['name'] ?? ''),
  25. 'product_slug' => is_string($section['content']['productSlug'] ?? null) ? $section['content']['productSlug'] : null,
  26. ];
  27. }
  28. }
  29. }
  30. return $found;
  31. }
  32. /**
  33. * The product web-address names the plan points to.
  34. *
  35. * @param array<string, mixed> $plan
  36. * @return list<string>
  37. */
  38. public static function slugs(array $plan): array
  39. {
  40. return array_values(array_unique(array_filter(array_column(self::sections($plan), 'product_slug'))));
  41. }
  42. /**
  43. * The plan with one featured section pointed at a product (or at none).
  44. *
  45. * @param array<string, mixed> $plan
  46. * @return array<string, mixed>
  47. */
  48. public static function link(array $plan, string $sectionId, ?string $productSlug): array
  49. {
  50. foreach ($plan['pages'] ?? [] as $pageIndex => $page) {
  51. foreach ($page['sections'] ?? [] as $sectionIndex => $section) {
  52. if (($section['id'] ?? null) === $sectionId && ($section['type'] ?? null) === 'featured_product') {
  53. $plan['pages'][$pageIndex]['sections'][$sectionIndex]['content']['productSlug'] = $productSlug;
  54. }
  55. }
  56. }
  57. return $plan;
  58. }
  59. }