From 67a3f5770e17409435c00ce558d964867a1104be Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Fri, 2 Aug 2024 13:26:32 +0300 Subject: [PATCH] view custom ceil --- examples/view.php | 8 ++++++++ src/ListJsonTable.php | 2 +- src/ViewJsonTable.php | 41 ++++++++++++++++++++++++++++------------- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/examples/view.php b/examples/view.php index 916f1b4..219e34d 100644 --- a/examples/view.php +++ b/examples/view.php @@ -7,5 +7,13 @@ use Itguild\Tables\ViewJsonTable; $json = file_get_contents('view.json'); $table = new ViewJsonTable($json); +$table->rows([ + 'username' => function ($ceil) { + return "$ceil"; + }, + 'email' => function ($ceil) { + return "$ceil"; + } +]); $table->create(); $table->render(); \ No newline at end of file diff --git a/src/ListJsonTable.php b/src/ListJsonTable.php index 74e3218..1fec4fe 100644 --- a/src/ListJsonTable.php +++ b/src/ListJsonTable.php @@ -118,7 +118,7 @@ class ListJsonTable } foreach ($row as $key => $ceil) { if ($this->issetColumn($key) and $this->is_fillable($key)) { - if($this->beforePrintCell) { + if($this->beforePrintCell and $ceil) { $ceil = $this->getCustomCeil($key, $ceil); } $this->html .= "" . $ceil . ""; diff --git a/src/ViewJsonTable.php b/src/ViewJsonTable.php index 96a6ac6..b378f8f 100644 --- a/src/ViewJsonTable.php +++ b/src/ViewJsonTable.php @@ -13,7 +13,7 @@ class ViewJsonTable private string $html = ""; private string $json; - private \Closure|false $beforePrintCell; + private \Closure|array|false $beforePrintCell; private \Closure|false $beforePrintTable; private \Closure|false $afterPrintTable; @@ -26,8 +26,6 @@ class ViewJsonTable $this->dataJson = $this->data['data']; } - - public function beginTable(): void { $paramsStr = $this->createParams($this->data['meta']['params']); @@ -44,20 +42,14 @@ class ViewJsonTable { foreach ($this->data['meta']['rows'] as $key => $row){ if ($this->issetRow($key)){ - if (isset($this->beforePrintCell)){ - $hook = $this->beforePrintCell; - $this->dataJson[$key] = $hook($key, $this->dataJson[$key]); + if ($this->beforePrintCell and $this->dataJson[$key]) { + $this->dataJson[$key] = $this->getCustomCeil($key, $this->dataJson[$key]); } - - $this->html .= "" . $row . ": " . $this->dataJson[$key] . ""; - + $this->html .= "" . $row . "" . $this->dataJson[$key] . ""; } } - - return $this->html; } - private function issetRow($column): bool { @@ -89,7 +81,6 @@ class ViewJsonTable $this->endTable(); } - public function beforeTable(\Closure $closure): void { $this->beforePrintTable = $closure; @@ -109,4 +100,28 @@ class ViewJsonTable { $this->beforePrintCell = $closure; } + + public function rows(array $data): void + { + foreach ($data as $key => $value) { + $this->beforePrintCell[$key] = $value; + } + } + + public function getCustomCeil(string $key, string $ceil) + { + if (is_array($this->beforePrintCell)) { + foreach ($this->beforePrintCell as $row => $closure) { + if ($key == $row) { + $hook = $closure; + $ceil = $hook($ceil); + } + } + } else { + $hook = $this->beforePrintCell; + $ceil = $hook($key, $ceil); + } + + return $ceil; + } } \ No newline at end of file