AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

PlanChanges.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. /**
  18. * Where a section is: its type and page. Header and footer count as sections with no page.
  19. *
  20. * @param array<string, mixed>|null $plan
  21. * @return array{type: string, page: string|null}|null Null when the plan has no such section.
  22. */
  23. public static function findSection(?array $plan, string $sectionId): ?array
  24. {
  25. if ($plan === null) {
  26. return null;
  27. }
  28. if (in_array($sectionId, ['header', 'footer'], true)) {
  29. return is_array($plan[$sectionId] ?? null) ? ['type' => $sectionId, 'page' => null] : null;
  30. }
  31. foreach ($plan['pages'] ?? [] as $page) {
  32. foreach ($page['sections'] ?? [] as $section) {
  33. if (is_array($section) && ($section['id'] ?? null) === $sectionId) {
  34. return ['type' => (string) ($section['type'] ?? ''), 'page' => (string) ($page['id'] ?? '')];
  35. }
  36. }
  37. }
  38. return null;
  39. }
  40. /**
  41. * Keep only the changes to one selected part of the store, so a request about "this" can never
  42. * touch the rest: updating the section, removing it (with the menu links that pointed to it),
  43. * or adding a new section right after it. The reply and store actions are kept as they are.
  44. *
  45. * @param array<string, mixed> $changes
  46. * @return array<string, mixed>
  47. */
  48. public static function limitTo(array $changes, string $sectionId): array
  49. {
  50. $kept = array_intersect_key($changes, array_flip(['reply', 'actions']));
  51. if (in_array($sectionId, ['header', 'footer'], true)) {
  52. if (is_array($changes[$sectionId] ?? null)) {
  53. $kept[$sectionId] = $changes[$sectionId];
  54. }
  55. return $kept;
  56. }
  57. $updates = array_values(array_filter(
  58. self::list($changes, 'update'),
  59. fn ($update) => is_array($update) && ($update['id'] ?? null) === $sectionId,
  60. ));
  61. $additions = array_values(array_filter(
  62. self::list($changes, 'add'),
  63. fn ($addition) => is_array($addition) && ($addition['after'] ?? null) === $sectionId,
  64. ));
  65. if ($updates !== []) {
  66. $kept['update'] = $updates;
  67. }
  68. if ($additions !== []) {
  69. $kept['add'] = $additions;
  70. }
  71. if (in_array($sectionId, self::list($changes, 'remove'), true)) {
  72. $kept['remove'] = [$sectionId];
  73. // Links to the removed section may need fixing in the menus.
  74. foreach (['header', 'footer'] as $part) {
  75. if (is_array($changes[$part] ?? null)) {
  76. $kept[$part] = $changes[$part];
  77. }
  78. }
  79. }
  80. return $kept;
  81. }
  82. public static function apply(array $plan, array $changes): array
  83. {
  84. // Theme: merge only the parts that were given.
  85. if (is_array($changes['theme'] ?? null) && is_array($plan['theme'] ?? null)) {
  86. $plan['theme'] = array_replace_recursive($plan['theme'], $changes['theme']);
  87. }
  88. // Header and footer: a new variant and/or some content fields.
  89. foreach (['header', 'footer'] as $part) {
  90. if (is_array($changes[$part] ?? null) && is_array($plan[$part] ?? null)) {
  91. $plan[$part] = self::merge($plan[$part], $changes[$part]);
  92. }
  93. }
  94. $pages = array_values(array_filter($plan['pages'] ?? [], 'is_array'));
  95. // Remove whole pages. The home page can never be removed.
  96. $removePages = array_filter(self::list($changes, 'remove_pages'), 'is_string');
  97. $pages = array_values(array_filter(
  98. $pages,
  99. fn (array $page) => ($page['id'] ?? null) === PlanSchema::HOME_PAGE || ! in_array($page['id'] ?? null, $removePages, true),
  100. ));
  101. // Rename pages.
  102. foreach (self::list($changes, 'update_pages') as $update) {
  103. foreach ($pages as $index => $page) {
  104. if (is_array($update) && ($page['id'] ?? null) === ($update['id'] ?? null) && is_string($update['title'] ?? null)) {
  105. $pages[$index]['title'] = $update['title'];
  106. }
  107. }
  108. }
  109. // Add whole new pages, at the end.
  110. $addedPages = [];
  111. foreach (self::list($changes, 'add_pages') as $page) {
  112. if (is_array($page)) {
  113. $pages[] = $page;
  114. $addedPages[] = $page;
  115. }
  116. }
  117. // We do not rely on the AI to keep the menus right: links to removed pages go,
  118. // and every new page gets a link so shoppers can reach it.
  119. $plan = self::dropLinksTo($plan, array_diff($removePages, [PlanSchema::HOME_PAGE]));
  120. foreach ($addedPages as $page) {
  121. $plan = self::ensureLinkTo($plan, $page);
  122. }
  123. $remove = array_filter(self::list($changes, 'remove'), 'is_string');
  124. $order = array_values(array_filter(self::list($changes, 'order'), 'is_string'));
  125. foreach ($pages as $pageIndex => $page) {
  126. $sections = array_values(array_filter($page['sections'] ?? [], 'is_array'));
  127. // Update: change the variant and/or some content fields of existing sections.
  128. foreach (self::list($changes, 'update') as $update) {
  129. foreach ($sections as $index => $section) {
  130. if (is_array($update) && ($section['id'] ?? null) === ($update['id'] ?? null)) {
  131. $sections[$index] = self::merge($section, $update);
  132. }
  133. }
  134. }
  135. // Remove.
  136. $sections = array_values(array_filter(
  137. $sections,
  138. fn (array $section) => ! in_array($section['id'] ?? null, $remove, true),
  139. ));
  140. // Add: after a named section, or at the end of the named page (the home page if none is named).
  141. foreach (self::list($changes, 'add') as $addition) {
  142. $section = is_array($addition) ? ($addition['section'] ?? null) : null;
  143. if (! is_array($section) || ($addition['page'] ?? PlanSchema::HOME_PAGE) !== ($page['id'] ?? null)) {
  144. continue;
  145. }
  146. $position = count($sections);
  147. foreach ($sections as $index => $existing) {
  148. if (($existing['id'] ?? null) === ($addition['after'] ?? null)) {
  149. $position = $index + 1;
  150. }
  151. }
  152. array_splice($sections, $position, 0, [$section]);
  153. }
  154. // Order: only used on the page whose sections it names exactly once each.
  155. $ids = array_column($sections, 'id');
  156. if ($order !== [] && count($order) === count($ids) && array_diff($ids, $order) === [] && array_diff($order, $ids) === []) {
  157. $byId = array_column($sections, null, 'id');
  158. $sections = array_map(fn (string $id) => $byId[$id], $order);
  159. }
  160. $pages[$pageIndex]['sections'] = $sections;
  161. }
  162. $plan['pages'] = $pages;
  163. return $plan;
  164. }
  165. /**
  166. * Remove header and footer links that point to the given pages.
  167. *
  168. * @param array<string, mixed> $plan
  169. * @param array<int, string> $pageIds
  170. * @return array<string, mixed>
  171. */
  172. private static function dropLinksTo(array $plan, array $pageIds): array
  173. {
  174. if ($pageIds === []) {
  175. return $plan;
  176. }
  177. foreach (['header', 'footer'] as $part) {
  178. $links = $plan[$part]['content']['links'] ?? null;
  179. if (is_array($links)) {
  180. $plan[$part]['content']['links'] = array_values(array_filter(
  181. $links,
  182. fn (mixed $link) => ! is_array($link) || ! in_array(self::pageOf($link['href'] ?? null), $pageIds, true),
  183. ));
  184. }
  185. }
  186. return $plan;
  187. }
  188. /**
  189. * Make sure the header or the footer links to a new page. The header is used while it has room.
  190. *
  191. * @param array<string, mixed> $plan
  192. * @param array<mixed> $page
  193. * @return array<string, mixed>
  194. */
  195. private static function ensureLinkTo(array $plan, array $page): array
  196. {
  197. $id = $page['id'] ?? null;
  198. $title = $page['title'] ?? null;
  199. if (! is_string($id) || ! is_string($title)) {
  200. return $plan;
  201. }
  202. foreach (['header', 'footer'] as $part) {
  203. foreach ($plan[$part]['content']['links'] ?? [] as $link) {
  204. if (is_array($link) && self::pageOf($link['href'] ?? null) === $id) {
  205. return $plan;
  206. }
  207. }
  208. }
  209. // The limits match PlanSchema: 6 header links, 8 footer links.
  210. foreach (['header' => 6, 'footer' => 8] as $part => $limit) {
  211. $links = $plan[$part]['content']['links'] ?? null;
  212. if (is_array($links) && count($links) < $limit) {
  213. $plan[$part]['content']['links'][] = ['label' => mb_substr($title, 0, 30), 'href' => '/'.$id];
  214. return $plan;
  215. }
  216. }
  217. return $plan;
  218. }
  219. /**
  220. * The page id a link such as "/about" or "/about#team" points to, or null for any other kind of link.
  221. */
  222. private static function pageOf(mixed $href): ?string
  223. {
  224. if (! is_string($href) || ! str_starts_with($href, '/')) {
  225. return null;
  226. }
  227. return explode('/', ltrim((string) preg_replace('/[?#].*$/', '', $href), '/'))[0];
  228. }
  229. /**
  230. * Take a new variant and replace only the content fields that were given.
  231. *
  232. * @param array<string, mixed> $section
  233. * @param array<string, mixed> $update
  234. * @return array<string, mixed>
  235. */
  236. private static function merge(array $section, array $update): array
  237. {
  238. if (is_string($update['variant'] ?? null)) {
  239. $section['variant'] = $update['variant'];
  240. }
  241. if (is_array($update['content'] ?? null)) {
  242. $section['content'] = array_replace($section['content'] ?? [], $update['content']);
  243. }
  244. return $section;
  245. }
  246. /**
  247. * @param array<string, mixed> $changes
  248. * @return array<mixed>
  249. */
  250. private static function list(array $changes, string $key): array
  251. {
  252. return is_array($changes[$key] ?? null) ? $changes[$key] : [];
  253. }
  254. }