$plan * @return list */ 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 $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 $plan * @return array */ 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; } }