Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.67% covered (success)
96.67%
29 / 30
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CollectionParent
96.67% covered (success)
96.67%
29 / 30
66.67% covered (warning)
66.67%
2 / 3
7
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%
17 / 17
100.00% covered (success)
100.00%
1 / 1
2
 status
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
7final class CollectionParent
8{
9    use NormalizesInput;
10
11    /** @param list<array{kind: string, label: string}> $status */
12    private function __construct(
13        public readonly string $uid,
14        public readonly ?string $title,
15        public readonly ?string $type,
16        public readonly array $status,
17        public readonly string $rootUrl,
18        public readonly string $editUrl,
19        public readonly string $treeUrl,
20    ) {}
21
22    /** @param iterable<mixed> $status */
23    public static function from(
24        CollectionUrls $urls,
25        ?string $title,
26        ?string $type,
27        iterable $status,
28    ): ?self {
29        $uid = $urls->query->parent;
30
31        if ($uid === null) {
32            return null;
33        }
34
35        return new self(
36            uid: $uid,
37            title: self::label($title),
38            type: self::label($type),
39            status: self::status($status),
40            rootUrl: $urls->collection([
41                'parent' => '',
42                'offset' => '',
43                'view' => '',
44                'open' => '',
45            ]),
46            editUrl: $urls->edit($uid),
47            treeUrl: $urls->showInTree($uid),
48        );
49    }
50
51    /**
52     * @param iterable<mixed> $status
53     * @return list<array{kind: string, label: string}>
54     */
55    private static function status(iterable $status): array
56    {
57        $badges = [];
58
59        foreach ($status as $badge) {
60            $badge = self::arrayFrom($badge);
61            $kind = trim((string) ($badge['kind'] ?? ''));
62            $label = trim((string) ($badge['label'] ?? ''));
63
64            if ($kind === '' || $label === '') {
65                continue;
66            }
67
68            $badges[] = [
69                'kind' => $kind,
70                'label' => $label,
71            ];
72        }
73
74        return $badges;
75    }
76}