Compare commits

..

No commits in common. "master" and "0.1.4" have entirely different histories.

21 changed files with 114 additions and 716 deletions

18
composer.lock generated
View File

@ -1,18 +0,0 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "76e2cbfc39ced89c5f5aa46d0b1474c7",
"packages": [],
"packages-dev": [],
"aliases": [],
"minimum-stability": "dev",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": [],
"plugin-api-version": "2.6.0"
}

View File

@ -2,34 +2,14 @@
"meta": { "meta": {
"title": "forma 1", "title": "forma 1",
"columns": { "columns": {
"id": "ID",
"email": "Email", "email": "Email",
"created_at": "Дата создания",
"description": "Описание 1", "description": "Описание 1",
"description2": "Описание 2", "description2": "Описание 2"
"status": "Статус"
}, },
"actions": ["view"],
"pagination": true,
"perPage": "5",
"currentPage": "1",
"showActionColumn": true,
"showFiltersRow": true,
"total": 10,
"paginationPrefix": "/page",
"params": {"class": "table table-bordered", "border": "1"} "params": {"class": "table table-bordered", "border": "1"}
}, },
"filters": ["email", "status"],
"data": [ "data": [
{"id": 1,"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas", "created_at": "17.06.2024", "status": "1"}, {"email":"fas@mail.ru","description":"fafdgdfs","description2":"ffdghdas"},
{"id": 2,"email":"fas2@mail.ru","description":"fafdgdfgsdfdfs","description2":"ffdghdas", "created_at": "18.06.2024", "status": "1"},
{"id": 3,"email":"fas3@mail.ru","description":"fafdgdssdfgdfs","description2":"ffdghdas", "created_at": "19.06.2024", "status": 2},
{"id": 4,"email":"fas4@mail.ru","description":"fafd <b>dsfgsd</b> vcbgdfs","description2":"ffdghdas", "created_at": "20.06.2024", "status": 1},
{"id": 5,"email":"fas5@mail.ru","description":"fafdgghjgfdfs","description2":"ffdghdas", "created_at": "21.06.2024", "status": 1},
{"id": 6,"email":"fas6@mail.ru","description":"fafddfgdhvgdfs","description2":"ffdghdas", "created_at": "22.06.2024", "status": 1},
{"id": 7,"email":"fas7@mail.ru","description":"fafdfgnfdgdfs","description2":"ffdghdas", "created_at": "23.06.2024", "status": 0},
{"id": 8,"email":"fas8@mail.ru","description":"fafdfghdfgdfs","description2":"ffdghdas", "created_at": "24.06.2024", "status": 1},
{"id": 12,"email":"dfdfd@mail.ru","description":"sdffhdfhggsdfg","description2":"ffdgdfgsdfghdas", "created_at": "25.06.2024", "status": 99},
{"id": 13,"email":"dfsdfsddfd@mail.ru","description":"sdffhdfsdfshggsdfg","description2":"ffdgdsdffgsdfghdas", "created_at": "25.06.2024", "status": 1}
] ]
} }

View File

@ -1,86 +1,11 @@
<?php <?php
ini_set("display_errors", true);
error_reporting(-1);
require_once "../vendor/autoload.php"; require_once "../vendor/autoload.php";
use Itguild\Tables\Filter\InputDateFilter;
use Itguild\Tables\Filter\InputTextFilter;
use Itguild\Tables\Filter\SelectFilter;
use Itguild\Tables\ListJsonTable; use Itguild\Tables\ListJsonTable;
$json = file_get_contents('simple.json'); $json = file_get_contents('simple.json');
$table = new ListJsonTable($json);
$table->columns([ $table = new ListJsonTable($json);
"created_at" => [
"format" => "date:Y-m-d",
'filter' => [
'class' => InputDateFilter::class,
]
],
'description' => [
"format" => "html",
"style" => ["width" => "300px"],
"filter" => [
'class' => InputTextFilter::class,
'value' => 'value'
],
"value" => function ($cell) {
return "<span style='color: sienna'>$cell</span>";
}
],
'description2' => [
"format" => "html",
"filter" => [
'class' => SelectFilter::class,
'param' => ['black', 'red', 'green', 'blue', 'yellow'],
'value' => 'red'
],
],
'status' => [
"filter" => [
'class' => SelectFilter::class,
'param' => getStatusLabel(),
'value' => 'Активный'
],
"value" => function ($cell) {
return getStatusLabel()[$cell];
}],
'k33' => [
"format" => "integer",
'filter' => [
'class' => \Itguild\Tables\Filter\InputRangeFilter::class,
'param' => ['min' => 0, 'max' => 10],
]
],
'email' => function ($cell) {
return "<span style='color: aqua'>$cell</span>";
},
]);
$table->afterPrint(function ($meta) {
return "<div>After Print</div>";
});
$table->addColumn("Колонка 33", "k33", function ($id) {
return "my ID: " . $id;
});
$table->addColumn("Колонка 34", "k34", function ($id) {
return "some34";
});
$table->addAction(function($row, $url){
return "<a href='mailto:". $row['email'] ."'>Написать</a>";
});
$table->create(); $table->create();
$table->render(); $table->render();
function getStatusLabel(): array
{
return [
0 => "На модерации",
1 => "Активный",
2 => "Модератор",
99 => "Удален",
];
}

View File

@ -1,27 +0,0 @@
{
"meta": {
"rows": {
"username": "Логин",
"email": "Email",
"created_at": "Создан",
"updated_at": "Обновлен"
},
"perPage": 10,
"currentPage": 1,
"baseUrl": "\/admin\/user",
"actions": [],
"params": {
"class": "table table-bordered",
"border": "2"
}
},
"data": {
"id": 1,
"username": "rrr",
"email": "rrr@mail.ru",
"password_hash": "sdgh46eyrtghsdret",
"role": 1,
"created_at": "15.07.2024",
"updated_at": null
}
}

View File

@ -1,20 +0,0 @@
<?php
require_once "../vendor/autoload.php";
use Itguild\Tables\ViewJsonTable;
$json = file_get_contents('view.json');
$table = new ViewJsonTable($json);
//$table->rows([
$table->rows([
'username' => function ($cell) {
return "<span style='color: aqua'>$cell</span>";
},
'email' => function ($cell) {
return "<span style='color: firebrick'>$cell</span>";
}
]);
$table->create();
$table->render();

View File

@ -1,19 +0,0 @@
<?php
namespace Itguild\Tables\Filter;
abstract class Filter
{
public string $html = '';
public string|array $param;
public string $name;
public string $value;
public function __construct(array $source)
{
$this->param = $source['param'] ?? '';
$this->name = $source['name'];
$this->value = $source['value'] ?? '';
}
abstract public function fetch();
}

View File

@ -1,12 +0,0 @@
<?php
namespace Itguild\Tables\Filter;
class InputDateFilter extends Filter
{
public function fetch()
{
return "<td><input type='date' name='$this->name' value ='$this->value'></td>";
}
}

View File

@ -1,23 +0,0 @@
<?php
namespace Itguild\Tables\Filter;
use Itguild\Tables\Filter\Filter;
class InputRangeFilter extends Filter
{
private int $min;
private int $max;
public function __construct(array $source)
{
parent::__construct($source);
$this->min = $this->param['min'] ?? 0;
$this->max = $this->param['max'] ?? 100;
}
public function fetch()
{
return "<td>" . $this->min . " <input type='range' name='$this->name' min= '$this->min' max='$this->max' value ='$this->value'>" . $this->max . "</td>";
}
}

View File

@ -1,11 +0,0 @@
<?php
namespace Itguild\Tables\Filter;
class InputTextFilter extends \Itguild\Tables\Filter\Filter
{
public function fetch()
{
return "<td><input type='text' name='$this->name' value ='$this->value'></td>";
}
}

View File

@ -1,23 +0,0 @@
<?php
namespace Itguild\Tables\Filter;
use Itguild\Tables\Filter\Filter;
class SelectFilter extends Filter
{
public function fetch()
{
$this->html = "<td><select name='$this->name'>";
foreach ($this->param as $value) {
if ($value === $this->value) {
$this->html .= "<option value='$value' selected>$value</option>";
} else {
$this->html .= "<option value='$value'>$value</option>";
}
}
$this->html .= "value='$this->value'</select></td>";
return $this->html;
}
}

View File

@ -1,25 +0,0 @@
<?php
namespace Itguild\Tables;
use Itguild\Tables\formats\DateFormat;
use Itguild\Tables\formats\HtmlFormat;
use Itguild\Tables\formats\IntegerFormat;
use Itguild\Tables\formats\PhoneNumberFormat;
use Itguild\Tables\formats\TextFormat;
class FormatMapper
{
public static function getFormat(): array
{
return [
'text' => TextFormat::class,
'date' => DateFormat::class,
'html' => HtmlFormat::class,
'phoneNumber' => PhoneNumberFormat::class,
'integer' => IntegerFormat::class,
];
}
}

View File

@ -1,94 +0,0 @@
<?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;
}
}

View File

@ -2,130 +2,100 @@
namespace Itguild\Tables; namespace Itguild\Tables;
use Exception; use Itguild\Tables\ActionColumn\ActionColumn;
use Itguild\Tables\ActionColumn\DeleteActionColumn; use Itguild\Tables\ActionColumn\DeleteActionColumn;
use Itguild\Tables\ActionColumn\EditActionColumn; use Itguild\Tables\ActionColumn\EditActionColumn;
use Itguild\Tables\ActionColumn\ViewActionColumn; use Itguild\Tables\ActionColumn\ViewActionColumn;
use Itguild\Tables\Filter\InputTextFilter;
use Itguild\Tables\traits\CreateParams; use Itguild\Tables\traits\CreateParams;
use JetBrains\PhpStorm\NoReturn;
class ListJsonTable extends JasonTable class ListJsonTable
{ {
use CreateParams; use CreateParams;
private string $html = '';
private string $json; private string $json;
private int $count = 0; private int $count = 0;
private \Closure|false $beforePrintCell;
private \Closure|false $beforePrint;
private string $baseUrl; private string $baseUrl;
private array $data; private array $data;
private bool $pagination = true;
private bool $showActionColumn = true;
private bool $showFiltersRow = true;
private bool|array $filters = [];
private array $actionsArray = []; private array $actionsArray = [];
private array $customActionsArray = []; private array $customActionsArray = [];
private array $customColumnsArray = [];
#[NoReturn] public function __construct(string $json) public function __construct(string $json)
{ {
$this->beforePrintCell = []; $this->beforePrintCell = false;
$this->beforePrint = function () {
};
$this->json = $json; $this->json = $json;
$this->data = json_decode($this->json, true); $this->data = json_decode($this->json, true);
$this->baseUrl = $this->data['meta']['baseUrl'] ?? ''; $this->baseUrl = $this->data['meta']['baseUrl'] ?? '';
$this->pagination = $this->data['meta']['pagination'] ?? true; $this->setActions();
$this->showActionColumn = $this->data['meta']['showActionColumn'] ?? true;
$this->showFiltersRow = $this->data['meta']['showFiltersRow'] ?? true;
$this->filters = $this->data['filters'] ?? [];
$this->beforePrintHook = function () {
};
$this->afterPrintHook = function () {
};
} }
public function beginTable(): void public function beginTable(): void
{ {
$paramsStr = $this->createParams($this->data['meta']['params']); $paramsStr = $this->createParams($this->data['meta']['params']);
$hookBefore = $this->beforePrintHook; $hook = $this->beforePrint;
$this->html .= $hookBefore($this->data['meta']); $this->html .= $hook();
$this->html .= "<table $paramsStr>"; $this->html .= "<table $paramsStr>";
} }
public function beforePrint(\Closure $closure): void
{
$this->beforePrint = $closure;
}
public function createThead(): void public function createThead(): void
{ {
$columnKeys = [];
if (!isset($this->data['meta']['columns'])) { if (!isset($this->data['meta']['columns'])) {
return; return;
} }
$this->html .= "<thead><tr>"; $this->html .= "<thead><tr>";
if (!$this->issetColumn("id")) {
$this->html .= "<th>" . "ID" . "</th>"; $this->html .= "<th>" . "ID" . "</th>";
$columnKeys[] = "id"; foreach ($this->data['meta']['columns'] as $key => $column) {
if ($this->is_fillable($key)) {
$this->html .= "<th>" . $column . "</th>";
} }
$columnKeys = array_merge($columnKeys, $this->getColumnKeys());
$columnKeys = array_merge($columnKeys, $this->getCustomColumnKeys());
$this->getCustomHeadColumn();
if ($this->showActionColumn) {
$this->html .= "<th>Действия</th></th></tr>";
}
if ($this->showFiltersRow) {
$this->getFilters($columnKeys);
$this->html .= "</thead>";
} }
$this->html .= "<th>Действия</th></th></tr></thead>";
} }
public function createTbody(): void public function createTbody(): void
{ {
if ($this->data['data']) { if ($this->data['data']) {
$this->count = $this->data["meta"]["perPage"] * ($this->data['meta']["currentPage"] - 1); $this->count = $this->data["meta"]["perPage"] * ($this->data['meta']["currentPage"] - 1);
foreach ($this->data['data'] as $row) { foreach ($this->data['data'] as $col) {
//сортируем колнки согласно массиву колонок из json
$row = array_merge(array_flip($this->getFillableColumns()), $row);
$this->html .= "<tr>"; $this->html .= "<tr>";
$this->count += 1; $this->count += 1;
$id = $row["id"] ?? $this->count; $this->html .= '<td><a href=' . $this->baseUrl . "/" . $col["id"] . '>' . $this->count . '</a></td>';
if (!$this->issetColumn("id")) { foreach ($col as $key => $row) {
$this->html .= '<td><a href=' . $this->baseUrl . "/" . $id . '>' . $id . '</a></td>';
}
foreach ($row as $key => $cell) {
if ($this->issetColumn($key) and $this->is_fillable($key)) { if ($this->issetColumn($key) and $this->is_fillable($key)) {
if ($this->beforePrintCell) { if ($this->beforePrintCell) {
if ($key === "id" and $cell === 0) { $hook = $this->beforePrintCell;
$cell = $this->count; $row = $hook($key, $row);
} }
$cell = $this->getCustomCell($key, $cell);
} $this->html .= "<td>" . $row . "</td>";
$this->html .= "<td style='" . $this->getStyleFromCustomColumn($key) . "'>" . $cell . "</td>";
} }
} }
$this->getCustomColumns($row["id"] ?? null);
if ($this->showActionColumn) { $actions = $this->getActions($col["id"]);
if (isset($row["id"])) {
$actions = $this->getActions($row);
$this->html .= "<td>$actions</td></tr>"; $this->html .= "<td>$actions</td></tr>";
} else {
$this->html .= "<td></td></tr>";
}
}
} }
} }
} }
public function addAction(string|\Closure $actionColumn): void public function addAction(string $actionColumn): void
{ {
$this->customActionsArray[] = $actionColumn; $this->customActionsArray[] = $actionColumn;
} }
public function addColumn(string $label, string $key, \Closure $closure): void
{
$this->customColumnsArray[] = ['label' => $label, "key" => $key, "handler" => $closure];
}
private function setActions(): void private function setActions(): void
{ {
if (isset($this->data['meta']['actions'])) { if (isset($this->data['meta']['actions'])) {
@ -146,66 +116,6 @@ class ListJsonTable extends JasonTable
$this->actionsArray = array_merge($this->actionsArray, $this->customActionsArray); $this->actionsArray = array_merge($this->actionsArray, $this->customActionsArray);
} }
private function getCurrentFilter(string $column)
{
if (isset($this->beforePrintCell[$column]) and is_array($this->beforePrintCell[$column])) {
if (isset($this->beforePrintCell[$column]['filter'])) {
if (isset($this->beforePrintCell[$column]['filter']) and is_array($this->beforePrintCell[$column]['filter'])) {
return $this->beforePrintCell[$column]['filter'];
}
}
}
return false;
}
public function getCustomColumns($id = null): void
{
foreach ($this->customColumnsArray as $item) {
$this->html .= "<td>" . $item['handler']($id) . "</td>";
}
}
public function getCustomHeadColumn(): void
{
foreach ($this->customColumnsArray as $item) {
$this->html .= "<th>" . $item['label'] . "</th>";
}
}
protected function getCustomColumnKeys(): array
{
$keys = [];
foreach ($this->customColumnsArray as $item) {
$keys[] = $item['key'];
}
return $keys;
}
private function getColumnKeys(): array
{
$columnKeys = [];
foreach ($this->data['meta']['columns'] as $key => $column) {
if ($this->is_fillable($key)) {
$this->html .= "<th>" . $column . "</th>";
$columnKeys[] = $key;
}
}
return $columnKeys;
}
private function getFillableColumns(): array
{
$columnKeys = [];
foreach ($this->data['meta']['columns'] as $key => $column) {
if ($this->is_fillable($key)) {
$columnKeys[] = $key;
}
}
return $columnKeys;
}
private function issetColumn($column): bool private function issetColumn($column): bool
{ {
if (isset($this->data['meta']['columns'])) { if (isset($this->data['meta']['columns'])) {
@ -219,27 +129,6 @@ class ListJsonTable extends JasonTable
return false; return false;
} }
private function issetFilter(string $filter): bool
{
if (isset($this->data['filters'])) {
foreach ($this->data['filters'] as $currentFilter) {
if (is_array($currentFilter)) {
return false;
} elseif (is_string($currentFilter)) {
if ($currentFilter === $filter) {
return true;
}
}
}
}
if (isset($this->beforePrintCell[$filter]) and is_array($this->beforePrintCell[$filter])) {
if (isset($this->beforePrintCell[$filter]['filter'])) {
return true;
}
}
return false;
}
private function is_fillable($column): bool private function is_fillable($column): bool
{ {
if (isset($this->data['meta']['fillable'])) { if (isset($this->data['meta']['fillable'])) {
@ -253,55 +142,19 @@ class ListJsonTable extends JasonTable
return false; return false;
} }
private function getActions(array $row): string private function getActions(int $id): string
{ {
$actions = ""; $actions = "";
foreach ($this->actionsArray as $item) { foreach ($this->actionsArray as $item) {
if (is_string($item)) { $objItem = new $item($this->baseUrl, $id);
$objItem = new $item($this->baseUrl, $row['id']);
$actions .= $objItem->fetch(); $actions .= $objItem->fetch();
} else {
$actions .= $item($row, $this->baseUrl);
}
} }
return $actions; return $actions;
} }
private function getFilters(array $columnKeys): void public function create()
{ {
$this->html .= "<tr><form action='$this->baseUrl/search'>";
foreach ($columnKeys as $key) {
if ($this->issetFilter($key)) {
$filter = $this->getCurrentFilter($key);
$params = [
'param' => $filter['param'] ?? '',
'name' => $key,
'value' => $filter['value'] ?? '',
];
if ($filter) {
$class = new $filter['class']($params);
$this->html .= $class->fetch();
} else {
$class = new InputTextFilter($params);
$this->html .= $class->fetch();
}
} else {
$this->html .= "<td></td>";
}
}
if ($this->showActionColumn) {
$this->html .= "<td></td>";
}
$this->html .= "</form></tr>";
}
/**
* @throws Exception
*/
public function create(): void
{
$this->setActions();
$this->beginTable(); $this->beginTable();
$this->createThead(); $this->createThead();
$this->createTbody(); $this->createTbody();
@ -310,6 +163,7 @@ class ListJsonTable extends JasonTable
public function tableAction($json): void public function tableAction($json): void
{ {
$tableJson = json_decode($json, true); $tableJson = json_decode($json, true);
foreach ($tableJson as $key => $value) { foreach ($tableJson as $key => $value) {
@ -320,34 +174,18 @@ class ListJsonTable extends JasonTable
} }
} }
/**
* @throws Exception
*/
public function endTable(): void public function endTable(): void
{ {
$this->html .= "</table>"; $this->html .= "</table>";
$hookAfter = $this->afterPrintHook;
$this->html .= $hookAfter($this->data['meta']);
if ($this->pagination && $this->data['data']) {
$options = [
'countItem' => $this->data['meta']['total'],
'perPage' => $this->data['meta']['perPage'] ?? 10,
'currentPage' => $this->data['meta']['currentPage'] ?? 1,
'baseUrl' => $this->baseUrl ?? '',
'prefix' => $this->data['meta']['paginationPrefix'] ?? '/page',
];
$pagination = new Pagination($options);
$pagination->create();
$this->html .= $pagination->fetch();
}
} }
public function columns(array $data): void public function render(): void
{ {
foreach ($data as $key => $value) { echo $this->html;
$this->beforePrintCell[$key] = $value;
}
} }
public function setBeforePrintCell(\Closure $closure): void
{
$this->beforePrintCell = $closure;
}
} }

View File

@ -3,9 +3,6 @@
namespace Itguild\Tables; namespace Itguild\Tables;
use Exception;
use http\Message;
class Pagination class Pagination
{ {
@ -17,24 +14,17 @@ class Pagination
private string $baseUrl; private string $baseUrl;
/** public function __construct($countItem, $perPage, $currentPage, $baseUrl)
* @throws Exception
*/
public function __construct(array $options)
{ {
if (!$options['countItem']) { $this->countItem = $countItem;
throw new Exception(message: "countItem is not valid"); $this->perPage = $perPage;
} $this->currentPage = $currentPage;
$this->countItem = $options['countItem']; $this->baseUrl = $baseUrl;
$this->perPage = $options['perPage'] ?? 10;
$this->currentPage = $options['currentPage'] ?? 1;
$this->baseUrl = $options['baseUrl'] ?? '';
$this->baseUrl .= $options['prefix'] ?? '/page';
$this->countPages = ceil($this->countItem / $this->perPage); $this->countPages = ceil($this->countItem / $perPage);
} }
public function create(): void public function create()
{ {
$prev = $this->currentPage - 1 >= 1 ? $this->currentPage - 1 : null; $prev = $this->currentPage - 1 >= 1 ? $this->currentPage - 1 : null;
$next = $this->currentPage + 1 <= $this->countPages ? $this->currentPage + 1 : null; $next = $this->currentPage + 1 <= $this->countPages ? $this->currentPage + 1 : null;
@ -52,12 +42,12 @@ class Pagination
echo $this->html; echo $this->html;
} }
public function fetch(): string public function fetch()
{ {
return $this->html; return $this->html;
} }
private function getTemplate(): string private function getTemplate()
{ {
return '<nav aria-label="Page navigation example"> return '<nav aria-label="Page navigation example">
<ul class="pagination"> <ul class="pagination">

View File

@ -4,54 +4,60 @@ namespace Itguild\Tables;
use Itguild\Tables\traits\CreateParams; use Itguild\Tables\traits\CreateParams;
class ViewJsonTable extends JasonTable class ViewJsonTable
{ {
use CreateParams; use CreateParams;
private array $data; private array $data;
private string $html = "";
private string $json; private string $json;
private \Closure|false $beforePrintCell;
private \Closure|false $beforePrintTable;
private \Closure|false $afterPrintTable;
private array $dataJson; private array $dataJson;
public function __construct($json) public function __construct($json)
{ {
$this->beforePrintCell = [];
$this->json = $json; $this->json = $json;
$this->data = json_decode($this->json, true); $this->data = json_decode($this->json, true);
$this->dataJson = $this->data['data']; $this->dataJson = json_decode($this->data['data']['data'], true);
$this->beforePrintHook = function () {
};
$this->afterPrintHook = function () {
};
} }
public function beginTable(): void public function beginTable(): void
{ {
$paramsStr = $this->createParams($this->data['meta']['params']); $paramsStr = $this->createParams($this->data['meta']['params']);
$hook = $this->beforePrintTable;
//Хук перед выводом ячейки $this->html = $hook();
if (isset($this->beforePrintHook)){
$hook = $this->beforePrintHook;
$this->html .= $hook();
}
$this->html .= "<table $paramsStr>"; $this->html .= "<table $paramsStr>";
} }
public function createRows(): string public function createColum(): string
{ {
foreach ($this->data['meta']['rows'] as $key => $row){ foreach ($this->data['meta']['columns'] as $key => $column){
if ($this->issetRow($key)){ if ($this->issetColumn($key)){
if ($this->beforePrintCell){ if ($this->beforePrintCell){
$this->dataJson[$key] = $this->getCustomCell($key, $this->dataJson[$key]); $hook = $this->beforePrintCell;
$this->dataJson[$key] = $hook($key, $this->dataJson[$key]);
} }
$this->html .= "<tr><th>" . $row . "</th><td>" . $this->dataJson[$key] . "</td></tr>";
$this->html .= "<tr><th>" . $column . ": </th><td>" . $this->dataJson[$key] . "</td></tr>";
} }
} }
return $this->html; return $this->html;
} }
private function issetRow($column): bool
private function issetColumn($column)
{ {
if (isset($this->data['meta']['rows'])){ if (isset($this->data['meta']['columns'])){
foreach ($this->data['meta']['rows'] as $key => $currentColumn){ foreach ($this->data['meta']['columns'] as $key => $currentColumn){
if ($key === $column){ if ($key === $column){
return true; return true;
} }
@ -61,28 +67,41 @@ class ViewJsonTable extends JasonTable
return false; return false;
} }
public function endTable(): void public function endTable()
{ {
$this->html .= "</table>"; $this->html .= "</table>";
$hookAfter = $this->afterPrintTable;
if(isset($this->afterPrintHook)){
$hookAfter = $this->afterPrintHook;
$this->html .= $hookAfter(); $this->html .= $hookAfter();
}
} }
public function create(): void public function create()
{ {
$this->beginTable(); $this->beginTable();
$this->createRows(); $this->createColum();
$this->endTable(); $this->endTable();
} }
public function rows(array $data): void public function beforeTable(\Closure $closure): void
{ {
foreach ($data as $key => $value) { $this->beforePrintTable = $closure;
$this->beforePrintCell[$key] = $value; }
}
public function afterTable(\Closure $closure): void
{
$this->afterPrintTable = $closure;
}
public function render()
{
echo $this->html;
}
public function setBeforePrintCell(\Closure $closure): void
{
$this->beforePrintCell = $closure;
} }
} }

View File

@ -1,10 +0,0 @@
<?php
namespace Itguild\Tables\formats;
abstract class BaseFormat
{
abstract static function fetch(string|null $data, string $options = "");
}

View File

@ -1,17 +0,0 @@
<?php
namespace Itguild\Tables\formats;
use DateTimeImmutable;
class DateFormat extends BaseFormat
{
/**
* @throws \Exception
*/
static function fetch(?string $data, string $options = ""): ?string
{
return (new DateTimeImmutable($data ?? ""))->format($options) ?? null;
}
}

View File

@ -1,14 +0,0 @@
<?php
namespace Itguild\Tables\formats;
use Itguild\Tables\formats\BaseFormat;
class HtmlFormat extends BaseFormat
{
static function fetch(?string $data, string $options = ""): string
{
return (string)$data;
}
}

View File

@ -1,14 +0,0 @@
<?php
namespace Itguild\Tables\formats;
use Itguild\Tables\formats\BaseFormat;
class IntegerFormat extends BaseFormat
{
static function fetch(?string $data, string $options = "")
{
return intval($data);
}
}

View File

@ -1,14 +0,0 @@
<?php
namespace Itguild\Tables\formats;
use Itguild\Tables\formats\BaseFormat;
class PhoneNumberFormat extends BaseFormat
{
static function fetch(?string $data, string $options = "")
{
return;
}
}

View File

@ -1,13 +0,0 @@
<?php
namespace Itguild\Tables\formats;
class TextFormat extends BaseFormat
{
public static function fetch($data, string $options = ""): string
{
return strip_tags((string)$data);
}
}