diff --git a/examples/view.php b/examples/view.php index 51acd21..dcba26a 100644 --- a/examples/view.php +++ b/examples/view.php @@ -7,6 +7,10 @@ use Itguild\Tables\ViewJsonTable; $json = file_get_contents('view.json'); $table = new ViewJsonTable($json); + +$table->addRow("Имя", function (){ + return "Kirill"; +}, ['after' => 'username']); //$table->rows([ $table->rows([ 'username' => function ($cell) { diff --git a/src/ViewJsonTable.php b/src/ViewJsonTable.php index 200c60a..ba9c6bb 100644 --- a/src/ViewJsonTable.php +++ b/src/ViewJsonTable.php @@ -11,6 +11,9 @@ class ViewJsonTable extends JasonTable private array $data; private string $json; private array $dataJson; + + private array $customRowsArray = []; + public function __construct($json) { $this->beforePrintCell = []; @@ -28,44 +31,40 @@ class ViewJsonTable extends JasonTable $paramsStr = $this->createParams($this->data['meta']['params']); //Хук перед выводом ячейки - if (isset($this->beforePrintHook)){ + if (isset($this->beforePrintHook)) { $hook = $this->beforePrintHook; $this->html .= $hook(); } $this->html .= ""; } + public function createRows(): string { - foreach ($this->data['meta']['rows'] as $key => $row){ - if ($this->issetRow($key)){ + foreach ($this->data['meta']['rows'] as $key => $row) { + if ($this->issetRow($key)) { + $rowsBefore = $this->getCustomRows($key, "before"); + $rowsAfter = $this->getCustomRows($key, "after"); if ($this->beforePrintCell) { $this->dataJson[$key] = $this->getCustomCell($key, $this->dataJson[$key]); } + foreach ($rowsBefore as $item){ + $this->html .= ""; + } $this->html .= ""; - } - } - return $this->html; - } - private function issetRow($column): bool - { - - if (isset($this->data['meta']['rows'])){ - foreach ($this->data['meta']['rows'] as $key => $currentColumn){ - if ($key === $column){ - return true; + foreach ($rowsAfter as $item){ + $this->html .= ""; } } } - - return false; + return $this->html; } public function endTable(): void { $this->html .= "
" . $item['label'] . "" . $this->getCustomRowHandler($item) . "
" . $row . "" . $this->dataJson[$key] . "
" . $item['label'] . "" . $this->getCustomRowHandler($item) . "
"; - if(isset($this->afterPrintHook)){ + if (isset($this->afterPrintHook)) { $hookAfter = $this->afterPrintHook; $this->html .= $hookAfter(); } @@ -85,4 +84,46 @@ class ViewJsonTable extends JasonTable $this->beforePrintCell[$key] = $value; } } + + public function addRow(string $label, \Closure $handler, array $position = []): void + { + $this->customRowsArray[] = ['label' => $label, 'handler' => $handler, 'position' => $position]; + } + + private function issetRow($column): bool + { + + if (isset($this->data['meta']['rows'])) { + foreach ($this->data['meta']['rows'] as $key => $currentColumn) { + if ($key === $column) { + return true; + } + } + } + + return false; + } + + private function getCustomRows(string $key, string $position): array + { + $customRows = []; + foreach ($this->customRowsArray as $item) { + if (!empty($item['position'])) { + if (isset($item['position'][$position]) && $item['position'][$position] === $key){ + $customRows[] = $item; + } + } + } + + return $customRows; + } + + private function getCustomRowHandler(array $row): mixed + { + if (isset($row['handler'])){ + return $row['handler'](); + } + + return false; + } } \ No newline at end of file