Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.91% |
20 / 22 |
|
80.00% |
8 / 10 |
CRAP | |
0.00% |
0 / 1 |
| Collection | |
90.91% |
20 / 22 |
|
80.00% |
8 / 10 |
10.08 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| entries | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| blueprints | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| slug | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| children | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| columns | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| header | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| searchFields | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| sorts | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| defaultSort | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| defaultDir | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray; |
| 6 | |
| 7 | use Cosray\Collection\Schema; |
| 8 | use Cosray\Collection\Schemas; |
| 9 | use Cosray\Finder\Nodes; |
| 10 | use Override; |
| 11 | |
| 12 | /** |
| 13 | * Collections are configured through class attributes: #[Label], |
| 14 | * #[Handle], #[Icon], #[Badge], #[Permission], #[Hidden], #[Order], |
| 15 | * #[Listing], #[Blueprints]. Behavior stays on methods. |
| 16 | */ |
| 17 | abstract class Collection implements NavigationItem |
| 18 | { |
| 19 | public readonly NavMeta $meta; |
| 20 | |
| 21 | protected readonly Schema $schema; |
| 22 | |
| 23 | public function __construct( |
| 24 | public readonly ?Cms $cms = null, |
| 25 | ?Schemas $schemas = null, |
| 26 | ) { |
| 27 | $schemas ??= new Schemas(); |
| 28 | $this->schema = $schemas->of(static::class); |
| 29 | $this->meta = $schemas->nav(static::class); |
| 30 | } |
| 31 | |
| 32 | abstract public function entries(): Nodes; |
| 33 | |
| 34 | public CollectionListMeta $listMeta { |
| 35 | get => $this->schema->listing; |
| 36 | } |
| 37 | |
| 38 | /** @return list<class-string> */ |
| 39 | public function blueprints(): array |
| 40 | { |
| 41 | return $this->schema->blueprints; |
| 42 | } |
| 43 | |
| 44 | #[Override] |
| 45 | public function slug(): ?string |
| 46 | { |
| 47 | return $this->schema->handle; |
| 48 | } |
| 49 | |
| 50 | /** @return list<NavigationItem> */ |
| 51 | #[Override] |
| 52 | public function children(): array |
| 53 | { |
| 54 | return []; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns an array of columns with column definitions. |
| 59 | * |
| 60 | * Each column array must have the fields `title` and `field` |
| 61 | */ |
| 62 | public function columns(): array |
| 63 | { |
| 64 | return [ |
| 65 | Column::new(__('collection:column-title'), 'title')->bold(true)->sort('title'), |
| 66 | Column::new(__('collection:column-type'), 'meta.name')->sort('type'), |
| 67 | Column::new(__('collection:column-editor'), 'meta.editor')->sort('editor'), |
| 68 | Column::new(__('collection:column-changed'), 'meta.changed')->date(true)->sort('changed'), |
| 69 | Column::new(__('collection:column-created'), 'meta.created')->date(true)->sort('created'), |
| 70 | ]; |
| 71 | } |
| 72 | |
| 73 | public function header(): array |
| 74 | { |
| 75 | return array_map(static fn(Column $column) => $column->title, $this->columns()); |
| 76 | } |
| 77 | |
| 78 | public function searchFields(): array |
| 79 | { |
| 80 | return ['uid', 'title']; |
| 81 | } |
| 82 | |
| 83 | public function sorts(): array |
| 84 | { |
| 85 | return [ |
| 86 | 'changed' => 'changed', |
| 87 | 'created' => 'created', |
| 88 | 'uid' => 'uid', |
| 89 | ]; |
| 90 | } |
| 91 | |
| 92 | public function defaultSort(): string |
| 93 | { |
| 94 | return 'changed'; |
| 95 | } |
| 96 | |
| 97 | public function defaultDir(): string |
| 98 | { |
| 99 | return 'desc'; |
| 100 | } |
| 101 | } |