AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

SectionLayouts.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\StorePlan;
  3. /**
  4. * The layout (variant) of each section, for the Design tab's Layouts box.
  5. * The header and footer use the ids "header" and "footer". Heroes have their own box, so they are left out.
  6. */
  7. final class SectionLayouts
  8. {
  9. /**
  10. * @param array<string, mixed> $plan
  11. * @return list<array{page_id: string|null, page_title: string|null, section_id: string, type: string, variant: string, label: string}>
  12. */
  13. public static function sections(array $plan): array
  14. {
  15. $found = [[
  16. 'page_id' => null, 'page_title' => null, 'section_id' => 'header', 'type' => 'header',
  17. 'variant' => (string) ($plan['header']['variant'] ?? ''), 'label' => 'Header',
  18. ]];
  19. foreach ($plan['pages'] ?? [] as $page) {
  20. foreach ($page['sections'] ?? [] as $section) {
  21. $type = (string) ($section['type'] ?? '');
  22. if ($type === 'hero') {
  23. continue;
  24. }
  25. $content = is_array($section['content'] ?? null) ? $section['content'] : [];
  26. $found[] = [
  27. 'page_id' => (string) ($page['id'] ?? ''),
  28. 'page_title' => (string) ($page['title'] ?? ''),
  29. 'section_id' => (string) ($section['id'] ?? ''),
  30. 'type' => $type,
  31. 'variant' => (string) ($section['variant'] ?? ''),
  32. // The section's own heading, so the merchant recognises it.
  33. 'label' => (string) ($content['heading'] ?? $content['name'] ?? ''),
  34. ];
  35. }
  36. }
  37. $found[] = [
  38. 'page_id' => null, 'page_title' => null, 'section_id' => 'footer', 'type' => 'footer',
  39. 'variant' => (string) ($plan['footer']['variant'] ?? ''), 'label' => 'Footer',
  40. ];
  41. return $found;
  42. }
  43. /**
  44. * The section's type, or null when the plan has no such section (heroes excluded).
  45. *
  46. * @param array<string, mixed> $plan
  47. */
  48. public static function typeOf(array $plan, string $sectionId): ?string
  49. {
  50. foreach (self::sections($plan) as $section) {
  51. if ($section['section_id'] === $sectionId) {
  52. return $section['type'];
  53. }
  54. }
  55. return null;
  56. }
  57. /**
  58. * @param array<string, mixed> $plan
  59. * @return array<string, mixed>
  60. */
  61. public static function update(array $plan, string $sectionId, string $variant): array
  62. {
  63. if (in_array($sectionId, ['header', 'footer'], true)) {
  64. $plan[$sectionId]['variant'] = $variant;
  65. return $plan;
  66. }
  67. foreach ($plan['pages'] ?? [] as $pageIndex => $page) {
  68. foreach ($page['sections'] ?? [] as $sectionIndex => $section) {
  69. if (($section['id'] ?? null) === $sectionId) {
  70. $plan['pages'][$pageIndex]['sections'][$sectionIndex]['variant'] = $variant;
  71. }
  72. }
  73. }
  74. return $plan;
  75. }
  76. }