| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- <?php
-
- namespace App\StorePlan;
-
- /**
- * Brings a plan saved in an older format up to the current one.
- *
- * Version 1: one flat list of sections, with the header and footer inside it.
- * Version 2: header and footer at the top of the plan, and a list of pages.
- */
- final class PlanUpgrader
- {
- /**
- * @param array<string, mixed> $plan
- * @return array<string, mixed>
- */
- public static function upgrade(array $plan): array
- {
- if (($plan['version'] ?? null) !== 1) {
- return $plan;
- }
-
- $header = null;
- $footer = null;
- $body = [];
-
- foreach ($plan['sections'] ?? [] as $section) {
- $type = $section['type'] ?? null;
- $shared = ['variant' => $section['variant'] ?? null, 'content' => $section['content'] ?? []];
-
- if ($type === 'header' && $header === null) {
- $header = $shared;
- } elseif ($type === 'footer') {
- $footer = $shared;
- } elseif ($type !== 'header') {
- $body[] = $section;
- }
- }
-
- return [
- 'version' => 2,
- 'theme' => $plan['theme'] ?? [],
- 'header' => $header ?? ['variant' => 'simple', 'content' => ['logoText' => 'Store', 'links' => []]],
- 'footer' => $footer ?? ['variant' => 'simple', 'content' => ['text' => '', 'links' => []]],
- 'pages' => [
- ['id' => PlanSchema::HOME_PAGE, 'title' => 'Home', 'sections' => $body],
- ],
- ];
- }
- }
|