| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
-
- namespace App\StorePlan;
-
- /**
- * Finds the "featured product" sections of a plan and the real products they point to.
- */
- final class FeaturedProducts
- {
- /**
- * Every featured product section, with the page it is on.
- *
- * @param array<string, mixed> $plan
- * @return list<array{page_id: string, page_title: string, section_id: string, name: string, product_slug: string|null}>
- */
- public static function sections(array $plan): array
- {
- $found = [];
-
- foreach ($plan['pages'] ?? [] as $page) {
- foreach ($page['sections'] ?? [] as $section) {
- if (($section['type'] ?? null) === 'featured_product') {
- $found[] = [
- 'page_id' => (string) ($page['id'] ?? ''),
- 'page_title' => (string) ($page['title'] ?? ''),
- 'section_id' => (string) ($section['id'] ?? ''),
- 'name' => (string) ($section['content']['name'] ?? ''),
- 'product_slug' => is_string($section['content']['productSlug'] ?? null) ? $section['content']['productSlug'] : null,
- ];
- }
- }
- }
-
- return $found;
- }
-
- /**
- * The product web-address names the plan points to.
- *
- * @param array<string, mixed> $plan
- * @return list<string>
- */
- public static function slugs(array $plan): array
- {
- return array_values(array_unique(array_filter(array_column(self::sections($plan), 'product_slug'))));
- }
-
- /**
- * The plan with one featured section pointed at a product (or at none).
- *
- * @param array<string, mixed> $plan
- * @return array<string, mixed>
- */
- public static function link(array $plan, string $sectionId, ?string $productSlug): array
- {
- foreach ($plan['pages'] ?? [] as $pageIndex => $page) {
- foreach ($page['sections'] ?? [] as $sectionIndex => $section) {
- if (($section['id'] ?? null) === $sectionId && ($section['type'] ?? null) === 'featured_product') {
- $plan['pages'][$pageIndex]['sections'][$sectionIndex]['content']['productSlug'] = $productSlug;
- }
- }
- }
-
- return $plan;
- }
- }
|