summaryrefslogtreecommitdiff
path: root/src/Columns/ElementColumn.php
blob: 71f0750971ba357143779d483337b8fcdb05b1f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php

namespace Lightscale\LaralightTables\Columns;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\HtmlString;
use Illuminate\View\ComponentAttributeBag;

use Closure;

abstract class ElementColumn extends Column
{

    protected string $element;
    private ?Closure $elemAttributesFn = null;

    public function attributes(callable $fn) : static
    {
        $this->elemAttributesFn = Closure::fromCallable($fn);
        return $this;
    }

    protected function getElemAttributes(Model $row) : array
    {
        return $this->elemAttributesFn?->call($this, $row) ?? [];
    }

    protected function getContent(Model $row): string
    {
        $content = parent::getContent($row);
        $attributes = new ComponentAttributeBag($this->getElemAttributes($row));
        $attributes = $attributes->toHtml();

        return new HtmlString(
            "<{$this->element} {$attributes}>{$content}</{$this->element}>"
        );
    }

}