Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.73% covered (success)
98.73%
78 / 79
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
CollectionPage
98.73% covered (success)
98.73%
78 / 79
80.00% covered (warning)
80.00%
4 / 5
12
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 from
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
1 / 1
2
 viewLinks
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
3
 createLinks
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 blueprints
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
4.01
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Panel;
6
7use Cosray\CollectionListMeta;
8use Cosray\Column;
9use DateTimeZone;
10
11final class CollectionPage
12{
13    use NormalizesInput;
14
15    /**
16     * @param list<array{label: string, url: string, active: bool}> $viewLinks
17     * @param list<array{slug: string, name: string, url: string}> $createLinks
18     * @param array{publishUrl: string, deleteUrl: string, duplicateUrl: string, showPublished: bool} $bulk
19     */
20    private function __construct(
21        public readonly string $name,
22        public readonly string $title,
23        public readonly ?CollectionParent $parent,
24        public readonly CollectionSearch $search,
25        public readonly array $viewLinks,
26        public readonly array $createLinks,
27        public readonly CollectionTable $table,
28        public readonly CollectionPager $pager,
29        public readonly array $bulk,
30    ) {}
31
32    /**
33     * @param iterable<Column> $columns
34     * @param iterable<mixed> $sortKeys
35     * @param iterable<mixed> $blueprints
36     * @param iterable<mixed> $nodes
37     * @param iterable<mixed>|null $createBlueprints
38     */
39    public static function from(
40        string $name,
41        CollectionUrls $urls,
42        iterable $columns,
43        iterable $sortKeys,
44        iterable $blueprints,
45        iterable $nodes,
46        int $total,
47        CollectionListMeta $meta,
48        string $locale,
49        DateTimeZone $timezone,
50        ?string $parentTitle = null,
51        ?string $parentType = null,
52        ?iterable $parentStatus = null,
53        ?iterable $createBlueprints = null,
54    ): self {
55        $query = $urls->query;
56        $nodes = self::items($nodes);
57        $blueprints = self::blueprints($blueprints);
58        $createBlueprints = $createBlueprints === null
59            ? $blueprints
60            : self::blueprints($createBlueprints);
61        $parent = CollectionParent::from(
62            urls: $urls,
63            title: $parentTitle,
64            type: $parentType,
65            status: $parentStatus ?? [],
66        );
67
68        return new self(
69            name: $name,
70            title: self::label($parentTitle) ?? $name,
71            parent: $parent,
72            search: CollectionSearch::from($urls),
73            viewLinks: self::viewLinks($meta, $query, $urls),
74            createLinks: self::createLinks($createBlueprints, $urls),
75            table: CollectionTable::from(
76                columns: $columns,
77                sortKeys: $sortKeys,
78                nodes: $nodes,
79                urls: $urls,
80                meta: $meta,
81                locale: $locale,
82                timezone: $timezone,
83            ),
84            pager: CollectionPager::from($total, count($nodes), $urls),
85            bulk: [
86                'publishUrl' => $urls->bulk('publish'),
87                'deleteUrl' => $urls->bulk('delete'),
88                'duplicateUrl' => $urls->bulk('duplicate'),
89                'showPublished' => $meta->showPublished,
90            ],
91        );
92    }
93
94    /** @return list<array{label: string, url: string, active: bool}> */
95    private static function viewLinks(
96        CollectionListMeta $meta,
97        CollectionQuery $query,
98        CollectionUrls $urls,
99    ): array {
100        if (!$meta->showChildren || $query->parent !== null) {
101            return [];
102        }
103
104        return [
105            [
106                'label' => __('collection:tree'),
107                'url' => $urls->collection([
108                    'view' => 'tree',
109                    'open' => '',
110                    'offset' => '',
111                ]),
112                'active' => $query->view === 'tree',
113            ],
114            [
115                'label' => __('collection:list'),
116                'url' => $urls->collection([
117                    'view' => 'list',
118                    'open' => '',
119                    'offset' => '',
120                ]),
121                'active' => $query->view === 'list',
122            ],
123        ];
124    }
125
126    /**
127     * @param list<array{slug: string, name: string}> $blueprints
128     * @return list<array{slug: string, name: string, url: string}>
129     */
130    private static function createLinks(array $blueprints, CollectionUrls $urls): array
131    {
132        $links = [];
133
134        foreach ($blueprints as $blueprint) {
135            $links[] = [
136                'slug' => $blueprint['slug'],
137                'name' => __($blueprint['name']),
138                'url' => $urls->create($blueprint['slug']),
139            ];
140        }
141
142        return $links;
143    }
144
145    /** @return list<array{slug: string, name: string}> */
146    private static function blueprints(iterable $blueprints): array
147    {
148        $result = [];
149
150        foreach ($blueprints as $blueprint) {
151            $blueprint = self::arrayFrom($blueprint);
152            $slug = trim((string) ($blueprint['slug'] ?? ''));
153            $name = trim((string) ($blueprint['name'] ?? ''));
154
155            if ($slug === '' || $name === '') {
156                continue;
157            }
158
159            $result[] = [
160                'slug' => $slug,
161                'name' => $name,
162            ];
163        }
164
165        return $result;
166    }
167}