Files
tables/src/ViewJsonTable.php
2024-08-07 14:31:07 +03:00

88 lines
2.2 KiB
PHP

<?php
namespace Itguild\Tables;
use Itguild\Tables\traits\CreateParams;
class ViewJsonTable extends JasonTable
{
use CreateParams;
private array $data;
private string $json;
private array $dataJson;
public function __construct($json)
{
$this->beforePrintCell = [];
$this->json = $json;
$this->data = json_decode($this->json, true);
$this->dataJson = $this->data['data'];
$this->beforePrintHook = function () {
};
$this->afterPrintHook = function () {
};
}
public function beginTable(): void
{
$paramsStr = $this->createParams($this->data['meta']['params']);
//Хук перед выводом ячейки
if (isset($this->beforePrintHook)){
$hook = $this->beforePrintHook;
$this->html .= $hook();
}
$this->html .= "<table $paramsStr>";
}
public function createRows(): string
{
foreach ($this->data['meta']['rows'] as $key => $row){
if ($this->issetRow($key)){
if ($this->beforePrintCell) {
$this->dataJson[$key] = $this->getCustomCell($key, $this->dataJson[$key]);
}
$this->html .= "<tr><th>" . $row . "</th><td>" . $this->dataJson[$key] . "</td></tr>";
}
}
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;
}
}
}
return false;
}
public function endTable(): void
{
$this->html .= "</table>";
if(isset($this->afterPrintHook)){
$hookAfter = $this->afterPrintHook;
$this->html .= $hookAfter();
}
}
public function create(): void
{
$this->beginTable();
$this->createRows();
$this->endTable();
}
public function rows(array $data): void
{
foreach ($data as $key => $value) {
$this->beforePrintCell[$key] = $value;
}
}
}