Compare commits

...

46 Commits

Author SHA1 Message Date
5f46431d45 fix filter row 2024-10-11 16:31:11 +03:00
02a3e52b7d fix pagination 2024-09-24 15:42:55 +03:00
d747203d99 custom action 2024-09-02 22:34:25 +03:00
ad18d94489 table filter fix 2024-08-29 13:13:51 +03:00
453917ea1a table filter fix 2024-08-29 13:08:43 +03:00
1f2b93baf1 table filter fix 2024-08-29 12:41:17 +03:00
5cb5ac839d table params fix 2024-08-29 12:07:54 +03:00
cf2cada74e fix date format 2024-08-29 11:49:46 +03:00
c816a7f14b some fix 2024-08-29 11:36:44 +03:00
0960a7b1fd fix filter 2024-08-28 15:39:35 +03:00
f20f408f12 add value to filter 2024-08-28 15:09:34 +03:00
4a15305505 some fix 2024-08-28 14:24:25 +03:00
5064042929 fix filter 2024-08-28 14:21:00 +03:00
f03ec200ac some fix filters 2024-08-28 12:12:52 +03:00
b4dc2f6ab1 filter class 2024-08-23 14:52:22 +03:00
77a306a0f6 add custom columns filter 2024-08-22 13:08:25 +03:00
af1b996766 filter 2024-08-22 11:53:13 +03:00
96b249eb51 add integer format 2024-08-13 14:30:00 +03:00
19e994ef38 filetrs 2024-08-12 15:39:26 +03:00
38d5deb36a formats and style 2024-08-08 17:08:46 +03:00
d0f136e484 add formats 2024-08-08 16:31:26 +03:00
7dbff619f5 merge 2024-08-08 14:24:53 +03:00
02fa193652 column format 2024-08-08 14:21:47 +03:00
a2284e3bf2 columns update 2024-08-08 13:26:45 +03:00
9e7c83b680 fix View Json Table 2024-08-07 14:31:07 +03:00
b374744261 Merge branch 'master' of https://git.itguild.info/ItGuild/tables 2024-08-07 14:21:00 +03:00
c6918c85b3 fix View Jason Table 2024-08-07 14:10:26 +03:00
d98ddded07 fix view json table 2024-08-07 12:43:14 +03:00
1bd82eb40b some fix 2024-08-07 12:16:42 +03:00
4a12eeccfd some fix 2024-08-07 11:56:33 +03:00
71b2fdfb6b some fix 2024-08-06 12:10:26 +03:00
f53346610e add JasonTable class 2024-08-02 15:40:13 +03:00
67a3f5770e view custom ceil 2024-08-02 13:26:32 +03:00
6d5f6a8665 fix methods names 2024-08-02 12:06:57 +03:00
2c1db8573c custom handler 2024-08-02 11:44:34 +03:00
8d1dce0222 columns 2024-08-01 15:10:41 +03:00
433cc85f54 filters 2024-08-01 13:32:31 +03:00
39931b257b filter beta0.2 2024-08-01 12:50:16 +03:00
b7fe32b91d filter beta 2024-08-01 12:28:21 +03:00
320e1070ba custom column 2024-07-31 17:11:29 +03:00
8bc8a75a53 custom column beta 2024-07-31 16:32:44 +03:00
f5f6504545 pagination fix 2024-07-31 15:49:40 +03:00
647fe34d11 pagination 2024-07-31 14:51:39 +03:00
1714afcf3f pagination 2024-07-31 14:26:26 +03:00
d6d703aaa6 meta at hooks 2024-07-31 12:48:26 +03:00
b7fea4122c add hook afterPrint 2024-07-30 12:20:16 +03:00
20 changed files with 660 additions and 101 deletions

18
composer.lock generated Normal file
View File

@ -0,0 +1,18 @@
{
"_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,14 +2,34 @@
"meta": {
"title": "forma 1",
"columns": {
"id": "ID",
"email": "Email",
"created_at": "Дата создания",
"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"}
},
"filters": ["email", "status"],
"data": [
{"email":"fas@mail.ru","description":"fafdgdfs","description2":"ffdghdas"},
{"id": 1,"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas", "created_at": "17.06.2024", "status": "1"},
{"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,11 +1,86 @@
<?php
require_once "../vendor/autoload.php";
ini_set("display_errors", true);
error_reporting(-1);
require_once "../vendor/autoload.php";
use Itguild\Tables\Filter\InputDateFilter;
use Itguild\Tables\Filter\InputTextFilter;
use Itguild\Tables\Filter\SelectFilter;
use Itguild\Tables\ListJsonTable;
$json = file_get_contents('simple.json');
$table = new ListJsonTable($json);
$table->columns([
"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->render();
$table->render();
function getStatusLabel(): array
{
return [
0 => "На модерации",
1 => "Активный",
2 => "Модератор",
99 => "Удален",
];
}

View File

@ -7,5 +7,14 @@ 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();

19
src/Filter/Filter.php Normal file
View File

@ -0,0 +1,19 @@
<?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

@ -0,0 +1,12 @@
<?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

@ -0,0 +1,23 @@
<?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

@ -0,0 +1,11 @@
<?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

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

25
src/FormatMapper.php Normal file
View File

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

94
src/JasonTable.php Normal file
View File

@ -0,0 +1,94 @@
<?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,99 +2,130 @@
namespace Itguild\Tables;
use Itguild\Tables\ActionColumn\ActionColumn;
use Exception;
use Itguild\Tables\ActionColumn\DeleteActionColumn;
use Itguild\Tables\ActionColumn\EditActionColumn;
use Itguild\Tables\ActionColumn\ViewActionColumn;
use Itguild\Tables\Filter\InputTextFilter;
use Itguild\Tables\traits\CreateParams;
use JetBrains\PhpStorm\NoReturn;
class ListJsonTable
class ListJsonTable extends JasonTable
{
use CreateParams;
private string $html = '';
private string $json;
private int $count = 0;
private \Closure|false $beforePrintCell;
private \Closure|false $beforePrint;
private string $baseUrl;
private array $data;
private bool $pagination = true;
private bool $showActionColumn = true;
private bool $showFiltersRow = true;
private bool|array $filters = [];
private array $actionsArray = [];
private array $customActionsArray = [];
private array $customColumnsArray = [];
public function __construct(string $json)
#[NoReturn] public function __construct(string $json)
{
$this->beforePrintCell = false;
$this->beforePrint = function () {
};
$this->beforePrintCell = [];
$this->json = $json;
$this->data = json_decode($this->json, true);
$this->baseUrl = $this->data['meta']['baseUrl'] ?? '';
$this->pagination = $this->data['meta']['pagination'] ?? true;
$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
{
$paramsStr = $this->createParams($this->data['meta']['params']);
$hook = $this->beforePrint;
$this->html .= $hook();
$hookBefore = $this->beforePrintHook;
$this->html .= $hookBefore($this->data['meta']);
$this->html .= "<table $paramsStr>";
}
public function beforePrint(\Closure $closure): void
{
$this->beforePrint = $closure;
}
public function createThead(): void
{
$columnKeys = [];
if (!isset($this->data['meta']['columns'])) {
return;
}
$this->html .= "<thead><tr>";
$this->html .= "<th>" . "ID" . "</th>";
foreach ($this->data['meta']['columns'] as $key => $column) {
if ($this->is_fillable($key)) {
$this->html .= "<th>" . $column . "</th>";
}
if (!$this->issetColumn("id")) {
$this->html .= "<th>" . "ID" . "</th>";
$columnKeys[] = "id";
}
$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
{
if ($this->data['data']) {
$this->count = $this->data["meta"]["perPage"] * ($this->data['meta']["currentPage"] - 1);
foreach ($this->data['data'] as $col) {
foreach ($this->data['data'] as $row) {
//сортируем колнки согласно массиву колонок из json
$row = array_merge(array_flip($this->getFillableColumns()), $row);
$this->html .= "<tr>";
$this->count += 1;
$this->html .= '<td><a href=' . $this->baseUrl . "/" . $col["id"] . '>' . $this->count . '</a></td>';
foreach ($col as $key => $row) {
$id = $row["id"] ?? $this->count;
if (!$this->issetColumn("id")) {
$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->beforePrintCell) {
$hook = $this->beforePrintCell;
$row = $hook($key, $row);
if ($key === "id" and $cell === 0) {
$cell = $this->count;
}
$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) {
if (isset($row["id"])) {
$actions = $this->getActions($row);
$actions = $this->getActions($col["id"]);
$this->html .= "<td>$actions</td></tr>";
$this->html .= "<td>$actions</td></tr>";
} else {
$this->html .= "<td></td></tr>";
}
}
}
}
}
public function addAction(string $actionColumn): void
public function addAction(string|\Closure $actionColumn): void
{
$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
{
if (isset($this->data['meta']['actions'])) {
@ -115,6 +146,66 @@ class ListJsonTable
$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
{
if (isset($this->data['meta']['columns'])) {
@ -128,6 +219,27 @@ class ListJsonTable
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
{
if (isset($this->data['meta']['fillable'])) {
@ -141,17 +253,52 @@ class ListJsonTable
return false;
}
private function getActions(int $id): string
private function getActions(array $row): string
{
$actions = "";
foreach ($this->actionsArray as $item) {
$objItem = new $item($this->baseUrl, $id);
$actions .= $objItem->fetch();
if (is_string($item)) {
$objItem = new $item($this->baseUrl, $row['id']);
$actions .= $objItem->fetch();
} else {
$actions .= $item($row, $this->baseUrl);
}
}
return $actions;
}
private function getFilters(array $columnKeys): void
{
$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();
@ -163,7 +310,6 @@ class ListJsonTable
public function tableAction($json): void
{
$tableJson = json_decode($json, true);
foreach ($tableJson as $key => $value) {
@ -174,18 +320,34 @@ class ListJsonTable
}
}
/**
* @throws Exception
*/
public function endTable(): void
{
$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 render(): void
public function columns(array $data): void
{
echo $this->html;
foreach ($data as $key => $value) {
$this->beforePrintCell[$key] = $value;
}
}
public function setBeforePrintCell(\Closure $closure): void
{
$this->beforePrintCell = $closure;
}
}

View File

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

View File

@ -4,38 +4,33 @@ namespace Itguild\Tables;
use Itguild\Tables\traits\CreateParams;
class ViewJsonTable
class ViewJsonTable extends JasonTable
{
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->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->beforePrintTable)){
$hook = $this->beforePrintTable;
$this->html = $hook();
if (isset($this->beforePrintHook)){
$hook = $this->beforePrintHook;
$this->html .= $hook();
}
$this->html .= "<table $paramsStr>";
@ -44,20 +39,14 @@ class ViewJsonTable
{
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]);
if ($this->beforePrintCell) {
$this->dataJson[$key] = $this->getCustomCell($key, $this->dataJson[$key]);
}
$this->html .= "<tr><th>" . $row . ": </th><td>" . $this->dataJson[$key] . "</td></tr>";
$this->html .= "<tr><th>" . $row . "</th><td>" . $this->dataJson[$key] . "</td></tr>";
}
}
return $this->html;
}
private function issetRow($column): bool
{
@ -76,8 +65,8 @@ class ViewJsonTable
{
$this->html .= "</table>";
if(isset($this->afterPrintTable)){
$hookAfter = $this->afterPrintTable;
if(isset($this->afterPrintHook)){
$hookAfter = $this->afterPrintHook;
$this->html .= $hookAfter();
}
}
@ -90,23 +79,10 @@ class ViewJsonTable
}
public function beforeTable(\Closure $closure): void
public function rows(array $data): 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;
foreach ($data as $key => $value) {
$this->beforePrintCell[$key] = $value;
}
}
}

View File

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

View File

@ -0,0 +1,17 @@
<?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

@ -0,0 +1,14 @@
<?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

@ -0,0 +1,14 @@
<?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

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

View File

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