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.

PlanChanges.php 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace App\StorePlan\Ai;
  3. use App\StorePlan\PlanSchema;
  4. /**
  5. * Applies the small "what to change" answer of a chat edit to an existing plan.
  6. *
  7. * The result is not trusted here. PlanWriter runs it through PlanValidator afterwards,
  8. * so a bad change is rejected instead of saved.
  9. */
  10. final class PlanChanges
  11. {
  12. /**
  13. * @param array<string, mixed> $plan
  14. * @param array<string, mixed> $changes
  15. * @return array<string, mixed>
  16. */
  17. public static function apply(array $plan, array $changes): array
  18. {
  19. // Theme: merge only the parts that were given.
  20. if (is_array($changes['theme'] ?? null) && is_array($plan['theme'] ?? null)) {
  21. $plan['theme'] = array_replace_recursive($plan['theme'], $changes['theme']);
  22. }
  23. // Header and footer: a new variant and/or some content fields.
  24. foreach (['header', 'footer'] as $part) {
  25. if (is_array($changes[$part] ?? null) && is_array($plan[$part] ?? null)) {
  26. $plan[$part] = self::merge($plan[$part], $changes[$part]);
  27. }
  28. }
  29. $pages = array_values(array_filter($plan['pages'] ?? [], 'is_array'));
  30. // Remove whole pages. The home page can never be removed.
  31. $removePages = array_filter(self::list($changes, 'remove_pages'), 'is_string');
  32. $pages = array_values(array_filter(
  33. $pages,
  34. fn (array $page) => ($page['id'] ?? null) === PlanSchema::HOME_PAGE || ! in_array($page['id'] ?? null, $removePages, true),
  35. ));
  36. // Rename pages.
  37. foreach (self::list($changes, 'update_pages') as $update) {
  38. foreach ($pages as $index => $page) {
  39. if (is_array($update) && ($page['id'] ?? null) === ($update['id'] ?? null) && is_string($update['title'] ?? null)) {
  40. $pages[$index]['title'] = $update['title'];
  41. }
  42. }
  43. }
  44. // Add whole new pages, at the end.
  45. $addedPages = [];
  46. foreach (self::list($changes, 'add_pages') as $page) {
  47. if (is_array($page)) {
  48. $pages[] = $page;
  49. $addedPages[] = $page;
  50. }
  51. }
  52. // We do not rely on the AI to keep the menus right: links to removed pages go,
  53. // and every new page gets a link so shoppers can reach it.
  54. $plan = self::dropLinksTo($plan, array_diff($removePages, [PlanSchema::HOME_PAGE]));
  55. foreach ($addedPages as $page) {
  56. $plan = self::ensureLinkTo($plan, $page);
  57. }
  58. $remove = array_filter(self::list($changes, 'remove'), 'is_string');
  59. $order = array_values(array_filter(self::list($changes, 'order'), 'is_string'));
  60. foreach ($pages as $pageIndex => $page) {
  61. $sections = array_values(array_filter($page['sections'] ?? [], 'is_array'));
  62. // Update: change the variant and/or some content fields of existing sections.
  63. foreach (self::list($changes, 'update') as $update) {
  64. foreach ($sections as $index => $section) {
  65. if (is_array($update) && ($section['id'] ?? null) === ($update['id'] ?? null)) {
  66. $sections[$index] = self::merge($section, $update);
  67. }
  68. }
  69. }
  70. // Remove.
  71. $sections = array_values(array_filter(
  72. $sections,
  73. fn (array $section) => ! in_array($section['id'] ?? null, $remove, true),
  74. ));
  75. // Add: after a named section, or at the end of the named page (the home page if none is named).
  76. foreach (self::list($changes, 'add') as $addition) {
  77. $section = is_array($addition) ? ($addition['section'] ?? null) : null;
  78. if (! is_array($section) || ($addition['page'] ?? PlanSchema::HOME_PAGE) !== ($page['id'] ?? null)) {
  79. continue;
  80. }
  81. $position = count($sections);
  82. foreach ($sections as $index => $existing) {
  83. if (($existing['id'] ?? null) === ($addition['after'] ?? null)) {
  84. $position = $index + 1;
  85. }
  86. }
  87. array_splice($sections, $position, 0, [$section]);
  88. }
  89. // Order: only used on the page whose sections it names exactly once each.
  90. $ids = array_column($sections, 'id');
  91. if ($order !== [] && count($order) === count($ids) && array_diff($ids, $order) === [] && array_diff($order, $ids) === []) {
  92. $byId = array_column($sections, null, 'id');
  93. $sections = array_map(fn (string $id) => $byId[$id], $order);
  94. }
  95. $pages[$pageIndex]['sections'] = $sections;
  96. }
  97. $plan['pages'] = $pages;
  98. return $plan;
  99. }
  100. /**
  101. * Remove header and footer links that point to the given pages.
  102. *
  103. * @param array<string, mixed> $plan
  104. * @param array<int, string> $pageIds
  105. * @return array<string, mixed>
  106. */
  107. private static function dropLinksTo(array $plan, array $pageIds): array
  108. {
  109. if ($pageIds === []) {
  110. return $plan;
  111. }
  112. foreach (['header', 'footer'] as $part) {
  113. $links = $plan[$part]['content']['links'] ?? null;
  114. if (is_array($links)) {
  115. $plan[$part]['content']['links'] = array_values(array_filter(
  116. $links,
  117. fn (mixed $link) => ! is_array($link) || ! in_array(self::pageOf($link['href'] ?? null), $pageIds, true),
  118. ));
  119. }
  120. }
  121. return $plan;
  122. }
  123. /**
  124. * Make sure the header or the footer links to a new page. The header is used while it has room.
  125. *
  126. * @param array<string, mixed> $plan
  127. * @param array<mixed> $page
  128. * @return array<string, mixed>
  129. */
  130. private static function ensureLinkTo(array $plan, array $page): array
  131. {
  132. $id = $page['id'] ?? null;
  133. $title = $page['title'] ?? null;
  134. if (! is_string($id) || ! is_string($title)) {
  135. return $plan;
  136. }
  137. foreach (['header', 'footer'] as $part) {
  138. foreach ($plan[$part]['content']['links'] ?? [] as $link) {
  139. if (is_array($link) && self::pageOf($link['href'] ?? null) === $id) {
  140. return $plan;
  141. }
  142. }
  143. }
  144. // The limits match PlanSchema: 6 header links, 8 footer links.
  145. foreach (['header' => 6, 'footer' => 8] as $part => $limit) {
  146. $links = $plan[$part]['content']['links'] ?? null;
  147. if (is_array($links) && count($links) < $limit) {
  148. $plan[$part]['content']['links'][] = ['label' => mb_substr($title, 0, 30), 'href' => '/'.$id];
  149. return $plan;
  150. }
  151. }
  152. return $plan;
  153. }
  154. /**
  155. * The page id a link such as "/about" or "/about#team" points to, or null for any other kind of link.
  156. */
  157. private static function pageOf(mixed $href): ?string
  158. {
  159. if (! is_string($href) || ! str_starts_with($href, '/')) {
  160. return null;
  161. }
  162. return explode('/', ltrim((string) preg_replace('/[?#].*$/', '', $href), '/'))[0];
  163. }
  164. /**
  165. * Take a new variant and replace only the content fields that were given.
  166. *
  167. * @param array<string, mixed> $section
  168. * @param array<string, mixed> $update
  169. * @return array<string, mixed>
  170. */
  171. private static function merge(array $section, array $update): array
  172. {
  173. if (is_string($update['variant'] ?? null)) {
  174. $section['variant'] = $update['variant'];
  175. }
  176. if (is_array($update['content'] ?? null)) {
  177. $section['content'] = array_replace($section['content'] ?? [], $update['content']);
  178. }
  179. return $section;
  180. }
  181. /**
  182. * @param array<string, mixed> $changes
  183. * @return array<mixed>
  184. */
  185. private static function list(array $changes, string $key): array
  186. {
  187. return is_array($changes[$key] ?? null) ? $changes[$key] : [];
  188. }
  189. }