Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
CategoryGrid
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
6 / 6
6
100.00% covered (success)
100.00%
1 / 1
 key
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 label
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 icon
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 description
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fields
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 categories
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Widgets\Types;
4
5use App\Models\Category;
6use App\Widgets\AbstractWidget;
7use Illuminate\Support\Collection;
8
9class CategoryGrid extends AbstractWidget
10{
11    protected string $view = 'widgets.category-grid';
12
13    public function key(): string
14    {
15        return 'category_grid';
16    }
17
18    public function label(): string
19    {
20        return 'Category slider / grid';
21    }
22
23    public function icon(): string
24    {
25        return 'fas fa-th';
26    }
27
28    public function description(): string
29    {
30        return 'Scrollable category cards or a fixed grid.';
31    }
32
33    public function fields(): array
34    {
35        return [
36            'title' => ['type' => 'text', 'label' => 'Heading', 'default' => 'Shop by category'],
37            'layout' => ['type' => 'select', 'label' => 'Layout', 'default' => 'slider', 'options' => ['slider'=>'Scrollable slider','grid'=>'Grid']],
38            'limit' => ['type' => 'number', 'label' => 'How many', 'default' => 6],
39            'parents_only' => ['type' => 'toggle', 'label' => 'Top-level categories only', 'default' => true],
40        ];
41    }
42
43    /** @return Collection<int,Category> */
44    public function categories(array $s)
45    {
46        $limit = max(1, min(24, (int) ($s['limit'] ?? 6)));
47
48        return Category::query()
49            ->when($s['parents_only'] ?? true, fn ($q) => $q->whereNull('parent_id'))
50            ->orderBy('name')
51            ->limit($limit)
52            ->get();
53    }
54}