column format

This commit is contained in:
2024-08-08 14:21:47 +03:00
parent d98ddded07
commit 02fa193652
8 changed files with 104 additions and 15 deletions

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'])) {

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;
}
}