From 38d5deb36ab34fcff3f745f65f71d642893a5fc0 Mon Sep 17 00:00:00 2001 From: Kavalar Date: Thu, 8 Aug 2024 17:08:46 +0300 Subject: [PATCH] formats and style --- examples/simple.json | 2 +- examples/simple.php | 11 +++++----- src/JasonTable.php | 42 +++++++++++++++++++++++++++++++++----- src/ListJsonTable.php | 4 +++- src/formats/HtmlFormat.php | 4 ++-- src/formats/TextFormat.php | 2 +- 6 files changed, 50 insertions(+), 15 deletions(-) diff --git a/examples/simple.json b/examples/simple.json index 82aa9a4..f883fe4 100644 --- a/examples/simple.json +++ b/examples/simple.json @@ -26,7 +26,7 @@ {"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": 4,"email":"fas4@mail.ru","description":"fafd dsfgsd vcbgdfs","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}, diff --git a/examples/simple.php b/examples/simple.php index bccc66b..7353dba 100644 --- a/examples/simple.php +++ b/examples/simple.php @@ -12,8 +12,12 @@ $table->columns([ "created_at" => [ "format" => "date:Y-m-d", ], - 'description2' => [ - "format" => "html:", + 'description' => [ + "format" => "html", + "style" => ["width" => "300px"], + "value" => function ($cell) { + return "$cell"; + } ], 'status' => function ($cell) { return getStatusLabel()[$cell]; @@ -21,9 +25,6 @@ $table->columns([ 'email' => function ($cell) { return "$cell"; }, - 'description' => function ($cell) { - return "$cell"; - } ]); //$table->setBeforePrintCell(function ($key, $data) { // return $key == "email" ? "$data" : $data; diff --git a/src/JasonTable.php b/src/JasonTable.php index 019c1cb..4764cd2 100644 --- a/src/JasonTable.php +++ b/src/JasonTable.php @@ -25,15 +25,16 @@ class JasonTable if (is_array($this->beforePrintCell)) { if (isset($this->beforePrintCell[$key])) { if (is_array($this->beforePrintCell[$key])){ + if (!isset($this->beforePrintCell[$key]['format'])){ + $this->beforePrintCell[$key]['format'] = "text"; + } + $format = explode(":", $this->beforePrintCell[$key]['format']); + $formatClass = FormatMapper::getFormat()[$format[0]]; + $cell = $formatClass::fetch($cell, $format[1] ?? ""); 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]; @@ -58,4 +59,35 @@ class JasonTable { $this->beforePrintHook = $closure; } + + protected function getParamFromCustomColumn(string $column, string $paramName) + { + if (is_array($this->beforePrintCell)) { + if (isset($this->beforePrintCell[$column])) { + if (is_array($this->beforePrintCell[$column])){ + if (isset($this->beforePrintCell[$column][$paramName])){ + return $this->beforePrintCell[$column][$paramName]; + } + } + } + } + + return null; + } + + protected function getStyleFromCustomColumn(string $column) + { + $styleStr = ''; + $style = $this->getParamFromCustomColumn($column, "style"); + if (is_array($style)){ + foreach ($style as $key => $value){ + $styleStr .= $key . ": " . $value . ";"; + } + } + else { + $styleStr = $style; + } + + return $styleStr; + } } \ No newline at end of file diff --git a/src/ListJsonTable.php b/src/ListJsonTable.php index b677a70..759b27e 100644 --- a/src/ListJsonTable.php +++ b/src/ListJsonTable.php @@ -91,7 +91,7 @@ class ListJsonTable extends JasonTable if($this->beforePrintCell) { $cell = $this->getCustomCell($key, $cell); } - $this->html .= "" . $cell . ""; + $this->html .= "" . $cell . ""; } } $this->getCustomColumns($row["id"] ?? null); @@ -163,6 +163,8 @@ class ListJsonTable extends JasonTable return $keys; } + + private function getColumnKeys(): array { $columnKeys = []; diff --git a/src/formats/HtmlFormat.php b/src/formats/HtmlFormat.php index fabef97..2bde005 100644 --- a/src/formats/HtmlFormat.php +++ b/src/formats/HtmlFormat.php @@ -7,8 +7,8 @@ use Itguild\Tables\formats\BaseFormat; class HtmlFormat extends BaseFormat { - static function fetch(?string $data, string $options = "") + static function fetch(?string $data, string $options = ""): string { - return; + return (string)$data; } } \ No newline at end of file diff --git a/src/formats/TextFormat.php b/src/formats/TextFormat.php index cac3d17..9e2537a 100644 --- a/src/formats/TextFormat.php +++ b/src/formats/TextFormat.php @@ -7,7 +7,7 @@ class TextFormat extends BaseFormat public static function fetch($data, string $options = ""): string { - return (string)$data; + return strip_tags((string)$data); } } \ No newline at end of file