94 lines
2.7 KiB
PHP
94 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace Itguild\Tables;
|
|
|
|
class JasonTable
|
|
{
|
|
public string $html = "";
|
|
protected \Closure|array|false $beforePrintCell;
|
|
protected \Closure|false $afterPrintHook;
|
|
protected \Closure|false $beforePrintHook;
|
|
|
|
|
|
public function render(): void
|
|
{
|
|
echo $this->html;
|
|
}
|
|
|
|
public function setBeforePrintCell(\Closure $closure): void
|
|
{
|
|
$this->beforePrintCell = $closure;
|
|
}
|
|
|
|
public function getCustomCell(string $key, string|null $cell)
|
|
{
|
|
if (is_array($this->beforePrintCell)) {
|
|
if (isset($this->beforePrintCell[$key])) {
|
|
if (is_array($this->beforePrintCell[$key])){
|
|
if (!isset($this->beforePrintCell[$key]['format'])){
|
|
$this->beforePrintCell[$key]['format'] = "text";
|
|
}
|
|
$format = explode(":", $this->beforePrintCell[$key]['format']);
|
|
$formatClass = FormatMapper::getFormat()[$format[0]];
|
|
$cell = $formatClass::fetch($cell, $format[1] ?? "");
|
|
if (isset($this->beforePrintCell[$key]['value'])){
|
|
$hook = $this->beforePrintCell[$key]['value'];
|
|
$cell = $hook($cell);
|
|
}
|
|
}
|
|
else {
|
|
$hook = $this->beforePrintCell[$key];
|
|
$cell = $hook($cell);
|
|
}
|
|
|
|
}
|
|
} else {
|
|
$hook = $this->beforePrintCell;
|
|
$cell = $hook($key, $cell);
|
|
}
|
|
|
|
return $cell;
|
|
}
|
|
|
|
public function afterPrint(\Closure $closure): void
|
|
{
|
|
$this->afterPrintHook = $closure;
|
|
}
|
|
|
|
public function beforePrint(\Closure $closure): void
|
|
{
|
|
$this->beforePrintHook = $closure;
|
|
}
|
|
|
|
protected function getParamFromCustomColumn(string $column, string $paramName)
|
|
{
|
|
if (is_array($this->beforePrintCell)) {
|
|
if (isset($this->beforePrintCell[$column])) {
|
|
if (is_array($this->beforePrintCell[$column])){
|
|
if (isset($this->beforePrintCell[$column][$paramName])){
|
|
return $this->beforePrintCell[$column][$paramName];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
protected function getStyleFromCustomColumn(string $column)
|
|
{
|
|
$styleStr = '';
|
|
$style = $this->getParamFromCustomColumn($column, "style");
|
|
if (is_array($style)){
|
|
foreach ($style as $key => $value){
|
|
$styleStr .= $key . ": " . $value . ";";
|
|
}
|
|
}
|
|
else {
|
|
$styleStr = $style;
|
|
}
|
|
|
|
return $styleStr;
|
|
}
|
|
|
|
} |