Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
66.67% |
8 / 12 |
|
33.33% |
2 / 6 |
CRAP | |
0.00% |
0 / 1 |
| AbstractWidget | |
66.67% |
8 / 12 |
|
33.33% |
2 / 6 |
12.00 | |
0.00% |
0 / 1 |
| key | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| icon | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| description | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| fields | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| defaults | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| render | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
3.14 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Widgets; |
| 4 | |
| 5 | use Illuminate\Support\Str; |
| 6 | |
| 7 | abstract class AbstractWidget implements WidgetContract |
| 8 | { |
| 9 | /** Blade view under resources/views/widgets/ used by the default render(). */ |
| 10 | protected string $view = ''; |
| 11 | |
| 12 | public function key(): string |
| 13 | { |
| 14 | return Str::snake(class_basename(static::class)); |
| 15 | } |
| 16 | |
| 17 | public function icon(): string |
| 18 | { |
| 19 | return 'fas fa-cube'; |
| 20 | } |
| 21 | |
| 22 | public function description(): string |
| 23 | { |
| 24 | return ''; |
| 25 | } |
| 26 | |
| 27 | public function fields(): array |
| 28 | { |
| 29 | return []; |
| 30 | } |
| 31 | |
| 32 | public function defaults(): array |
| 33 | { |
| 34 | $defaults = []; |
| 35 | foreach ($this->fields() as $key => $def) { |
| 36 | $defaults[$key] = $def['default'] ?? null; |
| 37 | } |
| 38 | |
| 39 | return $defaults; |
| 40 | } |
| 41 | |
| 42 | public function render(array $settings): string |
| 43 | { |
| 44 | $view = $this->view ?: 'widgets.' . $this->key(); |
| 45 | |
| 46 | if (! view()->exists($view)) { |
| 47 | return ''; |
| 48 | } |
| 49 | |
| 50 | return view($view, ['s' => array_merge($this->defaults(), $settings), 'widget' => $this])->render(); |
| 51 | } |
| 52 | } |