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