From 71b2fdfb6b31b542d758f498cb02140a28c5200c Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Tue, 6 Aug 2024 12:10:26 +0300 Subject: [PATCH] some fix --- examples/simple.php | 14 ++++----- examples/view.php | 10 +++---- src/JasonTable.php | 29 ++++++------------- src/ListJsonTable.php | 67 ++++++++++++++++++++++++------------------- src/ViewJsonTable.php | 14 ++++----- 5 files changed, 65 insertions(+), 69 deletions(-) diff --git a/examples/simple.php b/examples/simple.php index e45551c..67eb096 100644 --- a/examples/simple.php +++ b/examples/simple.php @@ -8,15 +8,15 @@ $json = file_get_contents('simple.json'); $table = new ListJsonTable($json); //$table->columns([ -$table->setBeforePrintCell([ - 'status' => function ($ceil) { - return getStatusLabel()[$ceil]; +$table->columns([ + 'status' => function ($cell) { + return getStatusLabel()[$cell]; }, - 'email' => function ($ceil) { - return "$ceil"; + 'email' => function ($cell) { + return "$cell"; }, - 'description' => function ($ceil) { - return "$ceil"; + 'description' => function ($cell) { + return "$cell"; } ]); //$table->setBeforePrintCell(function ($key, $data) { diff --git a/examples/view.php b/examples/view.php index d4ea137..51acd21 100644 --- a/examples/view.php +++ b/examples/view.php @@ -8,12 +8,12 @@ $json = file_get_contents('view.json'); $table = new ViewJsonTable($json); //$table->rows([ -$table->setBeforePrintCell([ - 'username' => function ($ceil) { - return "$ceil"; +$table->rows([ + 'username' => function ($cell) { + return "$cell"; }, - 'email' => function ($ceil) { - return "$ceil"; + 'email' => function ($cell) { + return "$cell"; } ]); $table->create(); diff --git a/src/JasonTable.php b/src/JasonTable.php index f06b8d2..276891b 100644 --- a/src/JasonTable.php +++ b/src/JasonTable.php @@ -2,7 +2,7 @@ namespace Itguild\Tables; -abstract class JasonTable +class JasonTable { protected string $html = ""; protected \Closure|array|false $beforePrintCell; @@ -15,37 +15,26 @@ abstract class JasonTable echo $this->html; } -// public function setBeforePrintCell(\Closure $closure): void -// { -// $this->beforePrintCell = $closure; -// } - - public function setBeforePrintCell(\Closure|array $data): void + public function setBeforePrintCell(\Closure $closure): void { - if(is_array($data)) { - foreach ($data as $key => $value) { - $this->beforePrintCell[$key] = $value; - } - } else { - $this->beforePrintCell = $data; - } + $this->beforePrintCell = $closure; } - public function getCustomCeil(string $key, string $ceil) + public function getCustomCell(string $key, string $cell) { if (is_array($this->beforePrintCell)) { - foreach ($this->beforePrintCell as $_key => $closure) { - if ($key == $_key) { + foreach ($this->beforePrintCell as $currentKey => $closure) { + if ($key == $currentKey) { $hook = $closure; - $ceil = $hook($ceil); + $cell = $hook($cell); } } } else { $hook = $this->beforePrintCell; - $ceil = $hook($key, $ceil); + $cell = $hook($key, $cell); } - return $ceil; + return $cell; } public function afterPrint(\Closure $closure): void diff --git a/src/ListJsonTable.php b/src/ListJsonTable.php index 3064308..62b1d89 100644 --- a/src/ListJsonTable.php +++ b/src/ListJsonTable.php @@ -2,9 +2,7 @@ namespace Itguild\Tables; -use Closure; use Exception; -use Itguild\Tables\ActionColumn\ActionColumn; use Itguild\Tables\ActionColumn\DeleteActionColumn; use Itguild\Tables\ActionColumn\EditActionColumn; use Itguild\Tables\ActionColumn\ViewActionColumn; @@ -32,7 +30,6 @@ class ListJsonTable extends JasonTable #[NoReturn] public function __construct(string $json) { $this->beforePrintCell = false; -// $this->beforePrintCellArr = []; $this->json = $json; $this->data = json_decode($this->json, true); $this->baseUrl = $this->data['meta']['baseUrl'] ?? ''; @@ -65,29 +62,14 @@ class ListJsonTable extends JasonTable $this->html .= "" . "ID" . ""; $columnKeys[] = "id"; } - foreach ($this->data['meta']['columns'] as $key => $column) { - if ($this->is_fillable($key)) { - $this->html .= "" . $column . ""; - $columnKeys[] = $key; - } - } + $columnKeys = $this->getColumnKeys($columnKeys); $columnKeys = array_merge($columnKeys, $this->getCustomColumnKeys()); $this->getCustomHeadColumn(); if ($this->showActionColumn) { $this->html .= "Действия"; } - if ($this->filters) { - $this->html .= "
"; - foreach ($columnKeys as $key){ - if ($this->issetFilter($key)){ - $this->html .= ""; - } - else { - $this->html .= ""; - } - } - $this->html .= "
"; + $this->getFilters($columnKeys); } } @@ -102,12 +84,12 @@ class ListJsonTable extends JasonTable if (!$this->issetColumn("id")) { $this->html .= '' . $id . ''; } - foreach ($row as $key => $ceil) { + foreach ($row as $key => $cell) { if ($this->issetColumn($key) and $this->is_fillable($key)) { - if($this->beforePrintCell and $ceil !== null) { - $ceil = $this->getCustomCeil($key, $ceil); + if($this->beforePrintCell and $cell !== null) { + $cell = $this->getCustomCell($key, $cell); } - $this->html .= "" . $ceil . ""; + $this->html .= "" . $cell . ""; } } $this->getCustomColumns($row["id"] ?? null); @@ -179,6 +161,17 @@ class ListJsonTable extends JasonTable return $keys; } + private function getColumnKeys(array $columnKeys): array + { + foreach ($this->data['meta']['columns'] as $key => $column) { + if ($this->is_fillable($key)) { + $this->html .= "" . $column . ""; + $columnKeys[] = $key; + } + } + return $columnKeys; + } + private function issetColumn($column): bool { if (isset($this->data['meta']['columns'])) { @@ -233,6 +226,20 @@ class ListJsonTable extends JasonTable return $actions; } + private function getFilters(array $columnKeys): void + { + $this->html .= "
"; + foreach ($columnKeys as $key){ + if ($this->issetFilter($key)){ + $this->html .= ""; + } + else { + $this->html .= ""; + } + } + $this->html .= "
"; + } + /** * @throws Exception */ @@ -280,10 +287,10 @@ 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) { + $this->beforePrintCell[$key] = $value; + } + } } \ No newline at end of file diff --git a/src/ViewJsonTable.php b/src/ViewJsonTable.php index 23231af..96cd7b7 100644 --- a/src/ViewJsonTable.php +++ b/src/ViewJsonTable.php @@ -35,7 +35,7 @@ class ViewJsonTable extends JasonTable foreach ($this->data['meta']['rows'] as $key => $row){ if ($this->issetRow($key)){ if ($this->beforePrintCell and $this->dataJson[$key] !== null) { - $this->dataJson[$key] = $this->getCustomCeil($key, $this->dataJson[$key]); + $this->dataJson[$key] = $this->getCustomCell($key, $this->dataJson[$key]); } $this->html .= "" . $row . "" . $this->dataJson[$key] . ""; } @@ -74,10 +74,10 @@ class ViewJsonTable extends JasonTable } -// public function rows(array $data): void -// { -// foreach ($data as $key => $value) { -// $this->beforePrintCell[$key] = $value; -// } -// } + public function rows(array $data): void + { + foreach ($data as $key => $value) { + $this->beforePrintCell[$key] = $value; + } + } } \ No newline at end of file