From 5c24746657ac23c7a65c4e4efc89cf6bfcb5a52c Mon Sep 17 00:00:00 2001
From: Sam Light <samlight1994@gmail.com>
Date: Sun, 5 Nov 2023 19:38:00 +0000
Subject: Built basic table

---
 src/Columns/Column.php | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 src/Columns/Column.php

(limited to 'src/Columns/Column.php')

diff --git a/src/Columns/Column.php b/src/Columns/Column.php
new file mode 100644
index 0000000..7b9f248
--- /dev/null
+++ b/src/Columns/Column.php
@@ -0,0 +1,68 @@
+<?php
+
+namespace Lightscale\LaralightTables\Columns;
+
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\View\ComponentAttributeBag;
+
+use Closure;
+
+class Column {
+
+    private TableComponent $table;
+
+    private Closure $displayFn;
+    private ?Closure $sortFn = null;
+    private ?Closure $searchFn = null;
+    private ?Closure $attributesFn = null;
+
+    public function __construct(
+        public string $name,
+        public string $title
+    ) {
+        $this->displayFn = Closure::fromCallable([$this, 'defaultDisplay']);
+    }
+
+    public static function make(string $name, $title) : static
+    {
+        return new static($name, $title);
+    }
+
+    private function defaultDisplay(Model $row, Column $column)
+    {
+        return $row->{$column->name};
+    }
+
+    public function display(callable $fn) : static
+    {
+        $this->displayFn = Closure::fromCallable($fn);
+        return $this;
+    }
+
+    public function sortable(callable $fn) : static
+    {
+        $this->sortFn = Closure::fromCallable($fn);
+        return $this;
+    }
+
+    public function searchable(callable $fn) : static
+    {
+        $this->searchFn = Closure::fromCallable($fn);
+        return $this;
+    }
+
+    public function attributes(callable $fn) : static
+    {
+        $this->attributesFn = Closure::fromCallable($fn);
+        return $this;
+    }
+
+    public function view(Model $row)
+    {
+        $attributes = $this->attributesFn?->call($this, $row) ?? [];
+        $attributes = new ComponentAttributeBag($attributes);
+        $content = $this->displayFn->call($this, $row, $this);
+        return view('laralight-tables::column', compact('attributes', 'content'));
+    }
+
+}
-- 
cgit v1.2.3