$plan * @return list}> */ public static function ofType(array $plan, string $type): array { $found = []; foreach ($plan['pages'] ?? [] as $page) { foreach ($page['sections'] ?? [] as $section) { if (($section['type'] ?? null) === $type) { $found[] = ['page_id' => (string) $page['id'], 'page_title' => (string) ($page['title'] ?? ''), 'section' => $section]; } } } return $found; } /** * The type of a section, or null when the plan has none with that id. * * @param array $plan */ public static function typeOf(array $plan, string $sectionId): ?string { foreach ($plan['pages'] ?? [] as $page) { foreach ($page['sections'] ?? [] as $section) { if (($section['id'] ?? null) === $sectionId) { return (string) ($section['type'] ?? ''); } } } return null; } /** * The home page's sections as id and a short label, for "put it after" choices. * * @param array $plan * @return list */ public static function homeSections(array $plan): array { $list = []; foreach ($plan['pages'][0]['sections'] ?? [] as $section) { $content = is_array($section['content'] ?? null) ? $section['content'] : []; $words = $content['heading'] ?? $content['headline'] ?? $content['name'] ?? null; $list[] = [ 'id' => (string) $section['id'], 'label' => str_replace('_', ' ', (string) $section['type']).(is_string($words) && $words !== '' ? ' ยท '.$words : ''), ]; } return $list; } /** * Add a section to the home page after another one (or at the end), with an id no other section has. * Returns the plan and the new section's id. * * @param array $plan * @param array{type: string, variant: string, content: array} $section * @return array{0: array, 1: string} */ public static function addToHome(array $plan, ?string $afterId, string $baseId, array $section): array { $id = self::freeId($plan, $baseId); $sections = $plan['pages'][0]['sections'] ?? []; $position = count($sections); foreach ($sections as $index => $existing) { if (($existing['id'] ?? null) === $afterId) { $position = $index + 1; } } array_splice($sections, $position, 0, [['id' => $id, ...$section]]); $plan['pages'][0]['sections'] = $sections; return [$plan, $id]; } /** * Replace a section's content (and optionally its layout). * * @param array $plan * @param array $content * @return array */ public static function update(array $plan, string $sectionId, array $content, ?string $variant = null): array { foreach ($plan['pages'] ?? [] as $pageIndex => $page) { foreach ($page['sections'] ?? [] as $sectionIndex => $section) { if (($section['id'] ?? null) === $sectionId) { $plan['pages'][$pageIndex]['sections'][$sectionIndex]['content'] = $content; if ($variant !== null) { $plan['pages'][$pageIndex]['sections'][$sectionIndex]['variant'] = $variant; } } } } return $plan; } /** * Take a section out. A page never ends up empty: the last section of a page stays. * * @param array $plan * @return array */ public static function remove(array $plan, string $sectionId): array { foreach ($plan['pages'] ?? [] as $pageIndex => $page) { $kept = array_values(array_filter($page['sections'] ?? [], fn (array $section) => ($section['id'] ?? null) !== $sectionId)); if ($kept !== [] && count($kept) !== count($page['sections'] ?? [])) { $plan['pages'][$pageIndex]['sections'] = $kept; } } return $plan; } /** * "video", or "video-2" when "video" is taken, and so on. * * @param array $plan */ private static function freeId(array $plan, string $base): string { $taken = []; foreach ($plan['pages'] ?? [] as $page) { foreach ($page['sections'] ?? [] as $section) { $taken[(string) ($section['id'] ?? '')] = true; } } $id = $base; $suffix = 2; while (isset($taken[$id])) { $id = $base.'-'.$suffix++; } return $id; } }