| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
-
- namespace App\StorePlan;
-
- /**
- * Small changes to a plan's sections from Design tab boxes: find sections of one type, add one after
- * another, change its content or layout, or take it out. Every result still goes through PlanValidator.
- */
- final class SectionEditor
- {
- /**
- * Every section of one type, on any page.
- *
- * @param array<string, mixed> $plan
- * @return list<array{page_id: string, page_title: string, section: array<string, mixed>}>
- */
- 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<string, mixed> $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<string, mixed> $plan
- * @return list<array{id: string, label: string}>
- */
- 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<string, mixed> $plan
- * @param array{type: string, variant: string, content: array<string, mixed>} $section
- * @return array{0: array<string, mixed>, 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<string, mixed> $plan
- * @param array<string, mixed> $content
- * @return array<string, mixed>
- */
- 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<string, mixed> $plan
- * @return array<string, mixed>
- */
- 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<string, mixed> $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;
- }
- }
|