Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.67% |
29 / 30 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| CollectionParent | |
96.67% |
29 / 30 |
|
66.67% |
2 / 3 |
7 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| from | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
2 | |||
| status | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
4.01 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Panel; |
| 6 | |
| 7 | final 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 | public static function from( |
| 23 | CollectionUrls $urls, |
| 24 | ?string $title, |
| 25 | ?string $type, |
| 26 | iterable $status, |
| 27 | ): ?self { |
| 28 | $uid = $urls->query->parent; |
| 29 | |
| 30 | if ($uid === null) { |
| 31 | return null; |
| 32 | } |
| 33 | |
| 34 | return new self( |
| 35 | uid: $uid, |
| 36 | title: self::label($title), |
| 37 | type: self::label($type), |
| 38 | status: self::status($status), |
| 39 | rootUrl: $urls->collection([ |
| 40 | 'parent' => '', |
| 41 | 'offset' => '', |
| 42 | 'view' => '', |
| 43 | 'open' => '', |
| 44 | ]), |
| 45 | editUrl: $urls->edit($uid), |
| 46 | treeUrl: $urls->showInTree($uid), |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | /** @return list<array{kind: string, label: string}> */ |
| 51 | private static function status(iterable $status): array |
| 52 | { |
| 53 | $badges = []; |
| 54 | |
| 55 | foreach ($status as $badge) { |
| 56 | $badge = self::arrayFrom($badge); |
| 57 | $kind = trim((string) ($badge['kind'] ?? '')); |
| 58 | $label = trim((string) ($badge['label'] ?? '')); |
| 59 | |
| 60 | if ($kind === '' || $label === '') { |
| 61 | continue; |
| 62 | } |
| 63 | |
| 64 | $badges[] = [ |
| 65 | 'kind' => $kind, |
| 66 | 'label' => $label, |
| 67 | ]; |
| 68 | } |
| 69 | |
| 70 | return $badges; |
| 71 | } |
| 72 | } |