summaryrefslogtreecommitdiff
path: root/src/Columns/LinkColumn.php
blob: 7ba3a9545995e32816754078089e2769871b5696 (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
<?php

namespace Lightscale\LaralightTables\Columns;

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

use Closure;

class LinkColumn extends Column
{
    private Closure $urlFn;
    private ?Closure $linkAttributesFn = null;

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

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

    protected function getContent(Model $row)
    {
        $content = parent::getContent($row);
        $url = $this->urlFn->call($this, $row);
        $attributes = $this->linkAttributesFn?->call($this, $row) ?? [];
        $attributes = (new ComponentAttributeBag(['href' => $url] + $attributes))->toHtml();

        return new HtmlString("<a {$attributes}>{$content}</a>");
    }

}