diff --git a/examples/simple.json b/examples/simple.json index 4e1499b..82aa9a4 100644 --- a/examples/simple.json +++ b/examples/simple.json @@ -4,6 +4,7 @@ "columns": { "id": "ID", "email": "Email", + "created_at": "Дата создания", "description": "Описание 1", "description2": "Описание 2", "status": "Статус" @@ -22,15 +23,15 @@ "email", "description" ], "data": [ - {"id": 1,"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas", "status": 1}, - {"id": 2,"email":"fas2@mail.ru","description":"fafdgdfgsdfdfs","description2":"ffdghdas", "status": 1}, - {"id": 3,"email":"fas3@mail.ru","description":"fafdgdssdfgdfs","description2":"ffdghdas", "status": 2}, - {"id": 4,"email":"fas4@mail.ru","description":"fafddsfgsdvcbgdfs","description2":"ffdghdas", "status": 1}, - {"id": 5,"email":"fas5@mail.ru","description":"fafdgghjgfdfs","description2":"ffdghdas", "status": 1}, - {"id": 6,"email":"fas6@mail.ru","description":"fafddfgdhvgdfs","description2":"ffdghdas", "status": 1}, - {"id": 7,"email":"fas7@mail.ru","description":"fafdfgnfdgdfs","description2":"ffdghdas", "status": 0}, - {"id": 8,"email":"fas8@mail.ru","description":"fafdfghdfgdfs","description2":"ffdghdas", "status": 1}, - {"id": 12,"email":"dfdfd@mail.ru","description":"sdffhdfhggsdfg","description2":"ffdgdfgsdfghdas", "status": 99}, - {"id": 13,"email":"dfsdfsddfd@mail.ru","description":"sdffhdfsdfshggsdfg","description2":"ffdgdsdffgsdfghdas", "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} ] } \ No newline at end of file diff --git a/examples/simple.php b/examples/simple.php index 67eb096..e4b5568 100644 --- a/examples/simple.php +++ b/examples/simple.php @@ -9,6 +9,9 @@ $table = new ListJsonTable($json); //$table->columns([ $table->columns([ + "created_at" => [ + "format" => "date:Y-m-d", + ], 'status' => function ($cell) { return getStatusLabel()[$cell]; }, diff --git a/src/FormatMapper.php b/src/FormatMapper.php new file mode 100644 index 0000000..7e0ca29 --- /dev/null +++ b/src/FormatMapper.php @@ -0,0 +1,19 @@ + TextFormat::class, + 'date' => DateFormat::class, + ]; + } + +} \ No newline at end of file diff --git a/src/JasonTable.php b/src/JasonTable.php index bef4652..019c1cb 100644 --- a/src/JasonTable.php +++ b/src/JasonTable.php @@ -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; diff --git a/src/ListJsonTable.php b/src/ListJsonTable.php index 9f374b2..95c4cd6 100644 --- a/src/ListJsonTable.php +++ b/src/ListJsonTable.php @@ -62,7 +62,7 @@ class ListJsonTable extends JasonTable $this->html .= "" . "ID" . ""; $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 .= ""; $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 .= "" . $column . ""; @@ -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'])) { diff --git a/src/formats/BaseFormat.php b/src/formats/BaseFormat.php new file mode 100644 index 0000000..dbd08b1 --- /dev/null +++ b/src/formats/BaseFormat.php @@ -0,0 +1,10 @@ +format($options) ?? null; + } +} \ No newline at end of file diff --git a/src/formats/TextFormat.php b/src/formats/TextFormat.php new file mode 100644 index 0000000..cac3d17 --- /dev/null +++ b/src/formats/TextFormat.php @@ -0,0 +1,13 @@ +