112 lines
2.5 KiB
PHP
112 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace Itguild\Tables;
|
|
|
|
use Itguild\Tables\traits\CreateParams;
|
|
|
|
class ViewJsonTable
|
|
{
|
|
use CreateParams;
|
|
|
|
private array $data;
|
|
|
|
private string $html = "";
|
|
private string $json;
|
|
|
|
private \Closure|false $beforePrintCell;
|
|
private \Closure|false $beforePrintTable;
|
|
|
|
private \Closure|false $afterPrintTable;
|
|
|
|
private array $dataJson;
|
|
public function __construct($json)
|
|
{
|
|
$this->json = $json;
|
|
$this->data = json_decode($this->json, true);
|
|
$this->dataJson = $this->data['data'];
|
|
}
|
|
|
|
|
|
|
|
public function beginTable(): void
|
|
{
|
|
$paramsStr = $this->createParams($this->data['meta']['params']);
|
|
|
|
//Хук перед выводом ячейки
|
|
if (isset($this->beforePrintTable)){
|
|
$hook = $this->beforePrintTable;
|
|
$this->html = $hook();
|
|
}
|
|
|
|
$this->html .= "<table $paramsStr>";
|
|
}
|
|
public function createRows(): string
|
|
{
|
|
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]);
|
|
}
|
|
|
|
$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->afterPrintTable)){
|
|
$hookAfter = $this->afterPrintTable;
|
|
$this->html .= $hookAfter();
|
|
}
|
|
}
|
|
|
|
public function create(): void
|
|
{
|
|
$this->beginTable();
|
|
$this->createRows();
|
|
$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;
|
|
}
|
|
} |