summaryrefslogtreecommitdiff
path: root/src/Columns/Column.php
blob: e4c50a9f94436cdfa1b9ce76ed505900f3ebe905 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php

namespace Lightscale\LaralightTables\Columns;

use Lightscale\LaralightTables\TableComponent;
use Lightscale\LaralightTables\Concerns\Makable;

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

use Closure;

class Column {
    use Makable;

    /**
     * @var TableComponent<Model>
     */
    private TableComponent $table;
    private bool $showInSelect;
    private bool $shouldEscape = true;

    /**
     * @var ?Closure(Model, Column): string
     */
    private ?Closure $slotFn = null;
    private ?Closure $sortFn = null;

    /**
     * @var ?Closure(Model): array<string, string>
     */
    private ?Closure $tdAttributesFn = null;

    private ?string $colClass = null;

    public function __construct(
        public string $name,
        private ?string $title = null
    ) {
        $this->showInSelect = $this->title !== null;
    }

    /**
     * @template TModel of Model
     * @param TableComponent<TModel> $table
     */
    public function setTable(TableComponent $table): void
    {
        $this->table = $table;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    private function defaultSlot(Model $row): string
    {
        return (string) $row->{$this->name};
    }

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

    public function sortable(?callable $fn): static
    {
        if ($fn !== null) {
            $this->sortFn = Closure::fromCallable($fn);
        }
        return $this;
    }

    public function isSortable(): bool
    {
        return $this->sortFn !== null;
    }

    /**
     * @template TModel of Model
     * @param Builder<TModel> $query
     */
    public function applySort(Builder $query, ?string $dir): void
    {
        if ($this->sortFn !== null) {
            ($this->sortFn)($query, $dir);
        }
    }

    public function colClass(string $v) : static
    {
        $this->colClass = $v;
        return $this;
    }

    public function getColClass() : ?string
    {
        return $this->colClass;
    }

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

    public function showInSelect(bool $show = true): static
    {
        $this->showInSelect = $show;
        return $this;
    }

    public function getShowInSelect(): bool
    {
        return $this->showInSelect;
    }

    public function shouldEscape(bool $v): static
    {
        $this->shouldEscape = $v;
        return $this;
    }

    public function escape(string $content): string
    {
        return $this->shouldEscape ? e($content) : $content;
    }

    protected function getContent(Model $row): string
    {
        return $this->escape(
            $this->slotFn?->call($this, $row, $this) ??
            $this->defaultSlot($row)
        );
    }

    public function view(Model $row): HtmlString
    {
        $attributes = $this->tdAttributesFn?->call($this, $row) ?? [];
        $attributes = (new ComponentAttributeBag($attributes))->toHtml();
        $content = $this->getContent($row);
        return new HtmlString("<td {$attributes}>{$content}</td>");
    }

}