AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

PlanChanges.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\StorePlan\Ai;
  3. /**
  4. * Applies the small "what to change" answer of a chat edit to an existing plan.
  5. *
  6. * The result is not trusted here. PlanWriter runs it through PlanValidator afterwards,
  7. * so a bad change is rejected instead of saved.
  8. */
  9. final class PlanChanges
  10. {
  11. /**
  12. * @param array<string, mixed> $plan
  13. * @param array<string, mixed> $changes
  14. * @return array<string, mixed>
  15. */
  16. public static function apply(array $plan, array $changes): array
  17. {
  18. $sections = array_values(array_filter($plan['sections'] ?? [], 'is_array'));
  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. // Update: change the variant and/or some content fields of existing sections.
  24. foreach (self::list($changes, 'update') as $update) {
  25. if (! is_array($update)) {
  26. continue;
  27. }
  28. foreach ($sections as $index => $section) {
  29. if (($section['id'] ?? null) !== ($update['id'] ?? null)) {
  30. continue;
  31. }
  32. if (is_string($update['variant'] ?? null)) {
  33. $sections[$index]['variant'] = $update['variant'];
  34. }
  35. if (is_array($update['content'] ?? null)) {
  36. $sections[$index]['content'] = array_replace($section['content'] ?? [], $update['content']);
  37. }
  38. }
  39. }
  40. // Remove.
  41. $remove = array_filter(self::list($changes, 'remove'), 'is_string');
  42. if ($remove !== []) {
  43. $sections = array_values(array_filter(
  44. $sections,
  45. fn (array $section) => ! in_array($section['id'] ?? null, $remove, true),
  46. ));
  47. }
  48. // Add: after a named section, or at the end.
  49. foreach (self::list($changes, 'add') as $addition) {
  50. $section = is_array($addition) ? ($addition['section'] ?? null) : null;
  51. if (! is_array($section)) {
  52. continue;
  53. }
  54. $position = count($sections);
  55. foreach ($sections as $index => $existing) {
  56. if (($existing['id'] ?? null) === ($addition['after'] ?? null)) {
  57. $position = $index + 1;
  58. }
  59. }
  60. array_splice($sections, $position, 0, [$section]);
  61. }
  62. // Order: only used when it names every section exactly once.
  63. $order = array_values(array_filter(self::list($changes, 'order'), 'is_string'));
  64. $ids = array_column($sections, 'id');
  65. if ($order !== [] && count($order) === count($ids) && array_diff($ids, $order) === [] && array_diff($order, $ids) === []) {
  66. $byId = array_column($sections, null, 'id');
  67. $sections = array_map(fn (string $id) => $byId[$id], $order);
  68. }
  69. $plan['sections'] = $sections;
  70. return $plan;
  71. }
  72. /**
  73. * @param array<string, mixed> $changes
  74. * @return array<mixed>
  75. */
  76. private static function list(array $changes, string $key): array
  77. {
  78. return is_array($changes[$key] ?? null) ? $changes[$key] : [];
  79. }
  80. }