AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SectionEditor.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace App\StorePlan;
  3. /**
  4. * Small changes to a plan's sections from Design tab boxes: find sections of one type, add one after
  5. * another, change its content or layout, or take it out. Every result still goes through PlanValidator.
  6. */
  7. final class SectionEditor
  8. {
  9. /**
  10. * Every section of one type, on any page.
  11. *
  12. * @param array<string, mixed> $plan
  13. * @return list<array{page_id: string, page_title: string, section: array<string, mixed>}>
  14. */
  15. public static function ofType(array $plan, string $type): array
  16. {
  17. $found = [];
  18. foreach ($plan['pages'] ?? [] as $page) {
  19. foreach ($page['sections'] ?? [] as $section) {
  20. if (($section['type'] ?? null) === $type) {
  21. $found[] = ['page_id' => (string) $page['id'], 'page_title' => (string) ($page['title'] ?? ''), 'section' => $section];
  22. }
  23. }
  24. }
  25. return $found;
  26. }
  27. /**
  28. * The type of a section, or null when the plan has none with that id.
  29. *
  30. * @param array<string, mixed> $plan
  31. */
  32. public static function typeOf(array $plan, string $sectionId): ?string
  33. {
  34. foreach ($plan['pages'] ?? [] as $page) {
  35. foreach ($page['sections'] ?? [] as $section) {
  36. if (($section['id'] ?? null) === $sectionId) {
  37. return (string) ($section['type'] ?? '');
  38. }
  39. }
  40. }
  41. return null;
  42. }
  43. /**
  44. * The home page's sections as id and a short label, for "put it after" choices.
  45. *
  46. * @param array<string, mixed> $plan
  47. * @return list<array{id: string, label: string}>
  48. */
  49. public static function homeSections(array $plan): array
  50. {
  51. $list = [];
  52. foreach ($plan['pages'][0]['sections'] ?? [] as $section) {
  53. $content = is_array($section['content'] ?? null) ? $section['content'] : [];
  54. $words = $content['heading'] ?? $content['headline'] ?? $content['name'] ?? null;
  55. $list[] = [
  56. 'id' => (string) $section['id'],
  57. 'label' => str_replace('_', ' ', (string) $section['type']).(is_string($words) && $words !== '' ? ' · '.$words : ''),
  58. ];
  59. }
  60. return $list;
  61. }
  62. /**
  63. * Add a section to the home page after another one (or at the end), with an id no other section has.
  64. * Returns the plan and the new section's id.
  65. *
  66. * @param array<string, mixed> $plan
  67. * @param array{type: string, variant: string, content: array<string, mixed>} $section
  68. * @return array{0: array<string, mixed>, 1: string}
  69. */
  70. public static function addToHome(array $plan, ?string $afterId, string $baseId, array $section): array
  71. {
  72. $id = self::freeId($plan, $baseId);
  73. $sections = $plan['pages'][0]['sections'] ?? [];
  74. $position = count($sections);
  75. foreach ($sections as $index => $existing) {
  76. if (($existing['id'] ?? null) === $afterId) {
  77. $position = $index + 1;
  78. }
  79. }
  80. array_splice($sections, $position, 0, [['id' => $id, ...$section]]);
  81. $plan['pages'][0]['sections'] = $sections;
  82. return [$plan, $id];
  83. }
  84. /**
  85. * Replace a section's content (and optionally its layout).
  86. *
  87. * @param array<string, mixed> $plan
  88. * @param array<string, mixed> $content
  89. * @return array<string, mixed>
  90. */
  91. public static function update(array $plan, string $sectionId, array $content, ?string $variant = null): array
  92. {
  93. foreach ($plan['pages'] ?? [] as $pageIndex => $page) {
  94. foreach ($page['sections'] ?? [] as $sectionIndex => $section) {
  95. if (($section['id'] ?? null) === $sectionId) {
  96. $plan['pages'][$pageIndex]['sections'][$sectionIndex]['content'] = $content;
  97. if ($variant !== null) {
  98. $plan['pages'][$pageIndex]['sections'][$sectionIndex]['variant'] = $variant;
  99. }
  100. }
  101. }
  102. }
  103. return $plan;
  104. }
  105. /**
  106. * Take a section out. A page never ends up empty: the last section of a page stays.
  107. *
  108. * @param array<string, mixed> $plan
  109. * @return array<string, mixed>
  110. */
  111. public static function remove(array $plan, string $sectionId): array
  112. {
  113. foreach ($plan['pages'] ?? [] as $pageIndex => $page) {
  114. $kept = array_values(array_filter($page['sections'] ?? [], fn (array $section) => ($section['id'] ?? null) !== $sectionId));
  115. if ($kept !== [] && count($kept) !== count($page['sections'] ?? [])) {
  116. $plan['pages'][$pageIndex]['sections'] = $kept;
  117. }
  118. }
  119. return $plan;
  120. }
  121. /**
  122. * "video", or "video-2" when "video" is taken, and so on.
  123. *
  124. * @param array<string, mixed> $plan
  125. */
  126. private static function freeId(array $plan, string $base): string
  127. {
  128. $taken = [];
  129. foreach ($plan['pages'] ?? [] as $page) {
  130. foreach ($page['sections'] ?? [] as $section) {
  131. $taken[(string) ($section['id'] ?? '')] = true;
  132. }
  133. }
  134. $id = $base;
  135. $suffix = 2;
  136. while (isset($taken[$id])) {
  137. $id = $base.'-'.$suffix++;
  138. }
  139. return $id;
  140. }
  141. }