diff --git a/examples/simple.php b/examples/simple.php index 66520f1..e45551c 100644 --- a/examples/simple.php +++ b/examples/simple.php @@ -7,7 +7,8 @@ use Itguild\Tables\ListJsonTable; $json = file_get_contents('simple.json'); $table = new ListJsonTable($json); -$table->columns([ +//$table->columns([ +$table->setBeforePrintCell([ 'status' => function ($ceil) { return getStatusLabel()[$ceil]; }, diff --git a/examples/view.php b/examples/view.php index 219e34d..d4ea137 100644 --- a/examples/view.php +++ b/examples/view.php @@ -7,7 +7,8 @@ use Itguild\Tables\ViewJsonTable; $json = file_get_contents('view.json'); $table = new ViewJsonTable($json); -$table->rows([ +//$table->rows([ +$table->setBeforePrintCell([ 'username' => function ($ceil) { return "$ceil"; }, diff --git a/src/JasonTable.php b/src/JasonTable.php new file mode 100644 index 0000000..f06b8d2 --- /dev/null +++ b/src/JasonTable.php @@ -0,0 +1,60 @@ +html; + } + +// public function setBeforePrintCell(\Closure $closure): void +// { +// $this->beforePrintCell = $closure; +// } + + public function setBeforePrintCell(\Closure|array $data): void + { + if(is_array($data)) { + foreach ($data as $key => $value) { + $this->beforePrintCell[$key] = $value; + } + } else { + $this->beforePrintCell = $data; + } + } + + public function getCustomCeil(string $key, string $ceil) + { + if (is_array($this->beforePrintCell)) { + foreach ($this->beforePrintCell as $_key => $closure) { + if ($key == $_key) { + $hook = $closure; + $ceil = $hook($ceil); + } + } + } else { + $hook = $this->beforePrintCell; + $ceil = $hook($key, $ceil); + } + + return $ceil; + } + + public function afterPrint(\Closure $closure): void + { + $this->afterPrintHook = $closure; + } + + public function beforePrint(\Closure $closure): void + { + $this->beforePrintHook = $closure; + } +} \ No newline at end of file diff --git a/src/ListJsonTable.php b/src/ListJsonTable.php index 1fec4fe..3064308 100644 --- a/src/ListJsonTable.php +++ b/src/ListJsonTable.php @@ -11,17 +11,13 @@ use Itguild\Tables\ActionColumn\ViewActionColumn; use Itguild\Tables\traits\CreateParams; use JetBrains\PhpStorm\NoReturn; -class ListJsonTable +class ListJsonTable extends JasonTable { use CreateParams; - private string $html = ''; private string $json; private int $count = 0; - private \Closure|array|false $beforePrintCell; - private \Closure|false $beforePrintHook; - private \Closure|false $afterPrintHook; private string $baseUrl; private array $data; @@ -57,16 +53,6 @@ class ListJsonTable $this->html .= ""; } - public function beforePrint(\Closure $closure): void - { - $this->beforePrintHook = $closure; - } - - public function afterPrint(\Closure $closure): void - { - $this->afterPrintHook = $closure; - } - public function createThead(): void { $columnKeys = []; @@ -118,7 +104,7 @@ class ListJsonTable } foreach ($row as $key => $ceil) { if ($this->issetColumn($key) and $this->is_fillable($key)) { - if($this->beforePrintCell and $ceil) { + if($this->beforePrintCell and $ceil !== null) { $ceil = $this->getCustomCeil($key, $ceil); } $this->html .= ""; @@ -294,37 +280,10 @@ class ListJsonTable } } - public function render(): void - { - echo $this->html; - } - - public function setBeforePrintCell(\Closure $closure): void - { - $this->beforePrintCell = $closure; - } - - public function columns(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 $column => $closure) { - if ($key == $column) { - $hook = $closure; - $ceil = $hook($ceil); - } - } - } else { - $hook = $this->beforePrintCell; - $ceil = $hook($key, $ceil); - } - - return $ceil; - } +// 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 b378f8f..23231af 100644 --- a/src/ViewJsonTable.php +++ b/src/ViewJsonTable.php @@ -4,20 +4,12 @@ namespace Itguild\Tables; use Itguild\Tables\traits\CreateParams; -class ViewJsonTable +class ViewJsonTable extends JasonTable { use CreateParams; private array $data; - - private string $html = ""; private string $json; - - private \Closure|array|false $beforePrintCell; - private \Closure|false $beforePrintTable; - - private \Closure|false $afterPrintTable; - private array $dataJson; public function __construct($json) { @@ -42,7 +34,7 @@ class ViewJsonTable { foreach ($this->data['meta']['rows'] as $key => $row){ if ($this->issetRow($key)){ - if ($this->beforePrintCell and $this->dataJson[$key]) { + if ($this->beforePrintCell and $this->dataJson[$key] !== null) { $this->dataJson[$key] = $this->getCustomCeil($key, $this->dataJson[$key]); } $this->html .= ""; @@ -81,47 +73,11 @@ class ViewJsonTable $this->endTable(); } - public function beforeTable(\Closure $closure): void - { - $this->beforePrintTable = $closure; - } - public function afterTable(\Closure $closure): void - { - $this->afterPrintTable = $closure; - } - - public function render(): void - { - echo $this->html; - } - - public function setBeforePrintCell(\Closure $closure): void - { - $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; - } +// public function rows(array $data): void +// { +// foreach ($data as $key => $value) { +// $this->beforePrintCell[$key] = $value; +// } +// } } \ No newline at end of file
" . $ceil . "
" . $row . "" . $this->dataJson[$key] . "