| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
-
- namespace App\StorePlan;
-
- /**
- * The layout (variant) of each section, for the Design tab's Layouts box.
- * The header and footer use the ids "header" and "footer". Heroes have their own box, so they are left out.
- */
- final class SectionLayouts
- {
- /**
- * @param array<string, mixed> $plan
- * @return list<array{page_id: string|null, page_title: string|null, section_id: string, type: string, variant: string, label: string}>
- */
- public static function sections(array $plan): array
- {
- $found = [[
- 'page_id' => null, 'page_title' => null, 'section_id' => 'header', 'type' => 'header',
- 'variant' => (string) ($plan['header']['variant'] ?? ''), 'label' => 'Header',
- ]];
-
- foreach ($plan['pages'] ?? [] as $page) {
- foreach ($page['sections'] ?? [] as $section) {
- $type = (string) ($section['type'] ?? '');
-
- if ($type === 'hero') {
- continue;
- }
-
- $content = is_array($section['content'] ?? null) ? $section['content'] : [];
-
- $found[] = [
- 'page_id' => (string) ($page['id'] ?? ''),
- 'page_title' => (string) ($page['title'] ?? ''),
- 'section_id' => (string) ($section['id'] ?? ''),
- 'type' => $type,
- 'variant' => (string) ($section['variant'] ?? ''),
- // The section's own heading, so the merchant recognises it.
- 'label' => (string) ($content['heading'] ?? $content['name'] ?? ''),
- ];
- }
- }
-
- $found[] = [
- 'page_id' => null, 'page_title' => null, 'section_id' => 'footer', 'type' => 'footer',
- 'variant' => (string) ($plan['footer']['variant'] ?? ''), 'label' => 'Footer',
- ];
-
- return $found;
- }
-
- /**
- * The section's type, or null when the plan has no such section (heroes excluded).
- *
- * @param array<string, mixed> $plan
- */
- public static function typeOf(array $plan, string $sectionId): ?string
- {
- foreach (self::sections($plan) as $section) {
- if ($section['section_id'] === $sectionId) {
- return $section['type'];
- }
- }
-
- return null;
- }
-
- /**
- * @param array<string, mixed> $plan
- * @return array<string, mixed>
- */
- public static function update(array $plan, string $sectionId, string $variant): array
- {
- if (in_array($sectionId, ['header', 'footer'], true)) {
- $plan[$sectionId]['variant'] = $variant;
-
- return $plan;
- }
-
- foreach ($plan['pages'] ?? [] as $pageIndex => $page) {
- foreach ($page['sections'] ?? [] as $sectionIndex => $section) {
- if (($section['id'] ?? null) === $sectionId) {
- $plan['pages'][$pageIndex]['sections'][$sectionIndex]['variant'] = $variant;
- }
- }
- }
-
- return $plan;
- }
- }
|