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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 = array_filter(self::list($changes, 'remove'), 'is_string');
  31. $order = array_values(array_filter(self::list($changes, 'order'), 'is_string'));
  32. foreach ($pages as $pageIndex => $page) {
  33. $sections = array_values(array_filter($page['sections'] ?? [], 'is_array'));
  34. // Update: change the variant and/or some content fields of existing sections.
  35. foreach (self::list($changes, 'update') as $update) {
  36. foreach ($sections as $index => $section) {
  37. if (is_array($update) && ($section['id'] ?? null) === ($update['id'] ?? null)) {
  38. $sections[$index] = self::merge($section, $update);
  39. }
  40. }
  41. }
  42. // Remove.
  43. $sections = array_values(array_filter(
  44. $sections,
  45. fn (array $section) => ! in_array($section['id'] ?? null, $remove, true),
  46. ));
  47. // Add: after a named section, or at the end of the named page (the home page if none is named).
  48. foreach (self::list($changes, 'add') as $addition) {
  49. $section = is_array($addition) ? ($addition['section'] ?? null) : null;
  50. if (! is_array($section) || ($addition['page'] ?? PlanSchema::HOME_PAGE) !== ($page['id'] ?? null)) {
  51. continue;
  52. }
  53. $position = count($sections);
  54. foreach ($sections as $index => $existing) {
  55. if (($existing['id'] ?? null) === ($addition['after'] ?? null)) {
  56. $position = $index + 1;
  57. }
  58. }
  59. array_splice($sections, $position, 0, [$section]);
  60. }
  61. // Order: only used on the page whose sections it names exactly once each.
  62. $ids = array_column($sections, 'id');
  63. if ($order !== [] && count($order) === count($ids) && array_diff($ids, $order) === [] && array_diff($order, $ids) === []) {
  64. $byId = array_column($sections, null, 'id');
  65. $sections = array_map(fn (string $id) => $byId[$id], $order);
  66. }
  67. $pages[$pageIndex]['sections'] = $sections;
  68. }
  69. $plan['pages'] = $pages;
  70. return $plan;
  71. }
  72. /**
  73. * Take a new variant and replace only the content fields that were given.
  74. *
  75. * @param array<string, mixed> $section
  76. * @param array<string, mixed> $update
  77. * @return array<string, mixed>
  78. */
  79. private static function merge(array $section, array $update): array
  80. {
  81. if (is_string($update['variant'] ?? null)) {
  82. $section['variant'] = $update['variant'];
  83. }
  84. if (is_array($update['content'] ?? null)) {
  85. $section['content'] = array_replace($section['content'] ?? [], $update['content']);
  86. }
  87. return $section;
  88. }
  89. /**
  90. * @param array<string, mixed> $changes
  91. * @return array<mixed>
  92. */
  93. private static function list(array $changes, string $key): array
  94. {
  95. return is_array($changes[$key] ?? null) ? $changes[$key] : [];
  96. }
  97. }