Compare commits

...

2 Commits

Author SHA1 Message Date
7dbff619f5 merge 2024-08-08 14:24:53 +03:00
02fa193652 column format 2024-08-08 14:21:47 +03:00
8 changed files with 105 additions and 31 deletions

View File

@ -4,8 +4,8 @@
"columns": {
"id": "ID",
"email": "Email",
"created_at": "Дата создания",
"description": "Описание 1",
"created_at": "Создан",
"status": "Статус"
},
"actions": ["view"],
@ -22,15 +22,15 @@
"email", "description"
],
"data": [
{"id": 1,"email":"fas1@mail.ru","description":"sdgsdfg","created_at":"2024-07-22 15:24:37", "status": 1},
{"id": 2,"email":"fas2@mail.ru","description":"fafdgdfgsdfdfs","created_at":"2024-04-22 15:56:37", "status": 1},
{"id": 3,"email":"fas3@mail.ru","description":"fafdgdssdfgdfs","created_at":"2024-07-27 15:24:37", "status": 2},
{"id": 4,"email":"fas4@mail.ru","description":"fafddsfgsdvcbgdfs","created_at":"2024-07-04 15:24:37", "status": 1},
{"id": 5,"email":"fas5@mail.ru","description":"fafdgghjgfdfs","created_at":"2024-07-13 15:24:37", "status": 1},
{"id": 6,"email":"fas6@mail.ru","description":"fafddfgdhvgdfs","created_at":"2024-07-20 15:24:37", "status": 1},
{"id": 7,"email":"fas7@mail.ru","description":"fafdfgnfdgdfs","created_at":"2024-06-22 15:24:37", "status": 0},
{"id": 8,"email":"fas8@mail.ru","description":"fafdfghdfgdfs","created_at":"2023-12-12 15:12:37", "status": 1},
{"id": 12,"email":"dfdfd@mail.ru","description":"sdffhdfhggsdfg","created_at":"2024-03-11 17:24:37", "status": 99},
{"id": 13,"email":"dfsdfsddfd@mail.ru","description":"sdffhdfsdfshggsdfg","created_at":"2023-11-19 14:24:37", "status": 1}
{"id": 1,"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas", "created_at": "17.06.2024", "status": 1},
{"id": 2,"email":"fas2@mail.ru","description":"fafdgdfgsdfdfs","description2":"ffdghdas", "created_at": "18.06.2024", "status": 1},
{"id": 3,"email":"fas3@mail.ru","description":"fafdgdssdfgdfs","description2":"ffdghdas", "created_at": "19.06.2024", "status": 2},
{"id": 4,"email":"fas4@mail.ru","description":"fafddsfgsdvcbgdfs","description2":"ffdghdas", "created_at": "20.06.2024", "status": 1},
{"id": 5,"email":"fas5@mail.ru","description":"fafdgghjgfdfs","description2":"ffdghdas", "created_at": "21.06.2024", "status": 1},
{"id": 6,"email":"fas6@mail.ru","description":"fafddfgdhvgdfs","description2":"ffdghdas", "created_at": "22.06.2024", "status": 1},
{"id": 7,"email":"fas7@mail.ru","description":"fafdfgnfdgdfs","description2":"ffdghdas", "created_at": "23.06.2024", "status": 0},
{"id": 8,"email":"fas8@mail.ru","description":"fafdfghdfgdfs","description2":"ffdghdas", "created_at": "24.06.2024", "status": 1},
{"id": 12,"email":"dfdfd@mail.ru","description":"sdffhdfhggsdfg","description2":"ffdgdfgsdfghdas", "created_at": "25.06.2024", "status": 99},
{"id": 13,"email":"dfsdfsddfd@mail.ru","description":"sdffhdfsdfshggsdfg","description2":"ffdgdsdffgsdfghdas", "created_at": "25.06.2024", "status": 1}
]
}

View File

@ -9,9 +9,8 @@ $table = new ListJsonTable($json);
//$table->columns([
$table->columns([
[
'field' => 'created_at',
'format' => 'date:d-m-Y',
"created_at" => [
"format" => "date:Y-m-d",
],
'status' => function ($cell) {
return getStatusLabel()[$cell];

19
src/FormatMapper.php Normal file
View File

@ -0,0 +1,19 @@
<?php
namespace Itguild\Tables;
use Itguild\Tables\formats\DateFormat;
use Itguild\Tables\formats\TextFormat;
class FormatMapper
{
public static function getFormat(): array
{
return [
'text' => TextFormat::class,
'date' => DateFormat::class,
];
}
}

View File

@ -23,11 +23,23 @@ class JasonTable
public function getCustomCell(string $key, string|null $cell)
{
if (is_array($this->beforePrintCell)) {
foreach ($this->beforePrintCell as $currentKey => $closure) {
if ($key == $currentKey) {
$hook = $closure;
if (isset($this->beforePrintCell[$key])) {
if (is_array($this->beforePrintCell[$key])){
if (isset($this->beforePrintCell[$key]['value'])){
$hook = $this->beforePrintCell[$key]['value'];
$cell = $hook($cell);
}
elseif (isset($this->beforePrintCell[$key]['format'])){
$format = explode(":", $this->beforePrintCell[$key]['format']);
$formatClass = FormatMapper::getFormat()[$format[0]];
$cell = $formatClass::fetch($cell, $format[1] ?? null);
}
}
else {
$hook = $this->beforePrintCell[$key];
$cell = $hook($cell);
}
}
} else {
$hook = $this->beforePrintCell;

View File

@ -62,7 +62,7 @@ class ListJsonTable extends JasonTable
$this->html .= "<th>" . "ID" . "</th>";
$columnKeys[] = "id";
}
$columnKeys = $this->getColumnKeys($columnKeys);
$columnKeys = $this->getColumnKeys();
$columnKeys = array_merge($columnKeys, $this->getCustomColumnKeys());
$this->getCustomHeadColumn();
if ($this->showActionColumn) {
@ -78,6 +78,8 @@ class ListJsonTable extends JasonTable
if ($this->data['data']) {
$this->count = $this->data["meta"]["perPage"] * ($this->data['meta']["currentPage"] - 1);
foreach ($this->data['data'] as $row) {
//сортируем колнки согласно массиву колонок из json
$row = array_merge(array_flip($this->getFillableColumns()), $row);
$this->html .= "<tr>";
$this->count += 1;
$id = $row["id"] ?? $this->count;
@ -161,8 +163,9 @@ class ListJsonTable extends JasonTable
return $keys;
}
private function getColumnKeys(array $columnKeys): array
private function getColumnKeys(): array
{
$columnKeys = [];
foreach ($this->data['meta']['columns'] as $key => $column) {
if ($this->is_fillable($key)) {
$this->html .= "<th>" . $column . "</th>";
@ -172,6 +175,17 @@ class ListJsonTable extends JasonTable
return $columnKeys;
}
private function getFillableColumns(): array
{
$columnKeys = [];
foreach ($this->data['meta']['columns'] as $key => $column) {
if ($this->is_fillable($key)) {
$columnKeys[] = $key;
}
}
return $columnKeys;
}
private function issetColumn($column): bool
{
if (isset($this->data['meta']['columns'])) {
@ -287,21 +301,11 @@ class ListJsonTable extends JasonTable
}
}
// public function columns(array $data): void
// {
// foreach ($data as $key => $value) {
// $this->beforePrintCell[$key] = $value;
// }
// }
public function columns(array $data): void
{
foreach ($data as $key => $value) {
foreach ($value as $currentKey => $currentValue) {
echo "<pre>";
print_r($currentValue);
// die();
}
$this->beforePrintCell[$key] = $value;
}
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace Itguild\Tables\formats;
abstract class BaseFormat
{
abstract static function fetch(string|null $data, string $options = "");
}

View File

@ -0,0 +1,17 @@
<?php
namespace Itguild\Tables\formats;
use DateTimeImmutable;
class DateFormat extends BaseFormat
{
/**
* @throws \Exception
*/
static function fetch(?string $data, string $options = ""): ?string
{
return (new DateTimeImmutable($data))->format($options) ?? null;
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace Itguild\Tables\formats;
class TextFormat extends BaseFormat
{
public static function fetch($data, string $options = ""): string
{
return (string)$data;
}
}