add JasonTable class
This commit is contained in:
		@@ -7,7 +7,8 @@ use Itguild\Tables\ListJsonTable;
 | 
				
			|||||||
$json = file_get_contents('simple.json');
 | 
					$json = file_get_contents('simple.json');
 | 
				
			||||||
$table = new ListJsonTable($json);
 | 
					$table = new ListJsonTable($json);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
$table->columns([
 | 
					//$table->columns([
 | 
				
			||||||
 | 
					$table->setBeforePrintCell([
 | 
				
			||||||
    'status' => function ($ceil) {
 | 
					    'status' => function ($ceil) {
 | 
				
			||||||
        return getStatusLabel()[$ceil];
 | 
					        return getStatusLabel()[$ceil];
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,7 +7,8 @@ use Itguild\Tables\ViewJsonTable;
 | 
				
			|||||||
$json = file_get_contents('view.json');
 | 
					$json = file_get_contents('view.json');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
$table = new ViewJsonTable($json);
 | 
					$table = new ViewJsonTable($json);
 | 
				
			||||||
$table->rows([
 | 
					//$table->rows([
 | 
				
			||||||
 | 
					$table->setBeforePrintCell([
 | 
				
			||||||
    'username' => function ($ceil) {
 | 
					    'username' => function ($ceil) {
 | 
				
			||||||
        return "<span style='color: aqua'>$ceil</span>";
 | 
					        return "<span style='color: aqua'>$ceil</span>";
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										60
									
								
								src/JasonTable.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								src/JasonTable.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,60 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Itguild\Tables;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					abstract class JasonTable
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    protected 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 setBeforePrintCell(\Closure|array $data): void
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        if(is_array($data)) {
 | 
				
			||||||
 | 
					            foreach ($data as $key => $value) {
 | 
				
			||||||
 | 
					                $this->beforePrintCell[$key] = $value;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            $this->beforePrintCell = $data;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function getCustomCeil(string $key, string $ceil)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        if (is_array($this->beforePrintCell)) {
 | 
				
			||||||
 | 
					            foreach ($this->beforePrintCell as $_key => $closure) {
 | 
				
			||||||
 | 
					                if ($key == $_key) {
 | 
				
			||||||
 | 
					                    $hook = $closure;
 | 
				
			||||||
 | 
					                    $ceil = $hook($ceil);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            $hook = $this->beforePrintCell;
 | 
				
			||||||
 | 
					            $ceil = $hook($key, $ceil);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return $ceil;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function afterPrint(\Closure $closure): void
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $this->afterPrintHook = $closure;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function beforePrint(\Closure $closure): void
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $this->beforePrintHook = $closure;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -11,17 +11,13 @@ use Itguild\Tables\ActionColumn\ViewActionColumn;
 | 
				
			|||||||
use Itguild\Tables\traits\CreateParams;
 | 
					use Itguild\Tables\traits\CreateParams;
 | 
				
			||||||
use JetBrains\PhpStorm\NoReturn;
 | 
					use JetBrains\PhpStorm\NoReturn;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ListJsonTable
 | 
					class ListJsonTable extends JasonTable
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    use CreateParams;
 | 
					    use CreateParams;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private string $html = '';
 | 
					 | 
				
			||||||
    private string $json;
 | 
					    private string $json;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private int $count = 0;
 | 
					    private int $count = 0;
 | 
				
			||||||
    private \Closure|array|false $beforePrintCell;
 | 
					 | 
				
			||||||
    private \Closure|false $beforePrintHook;
 | 
					 | 
				
			||||||
    private \Closure|false $afterPrintHook;
 | 
					 | 
				
			||||||
    private string $baseUrl;
 | 
					    private string $baseUrl;
 | 
				
			||||||
    private array $data;
 | 
					    private array $data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -57,16 +53,6 @@ class ListJsonTable
 | 
				
			|||||||
        $this->html .= "<table $paramsStr>";
 | 
					        $this->html .= "<table $paramsStr>";
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function beforePrint(\Closure $closure): void
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        $this->beforePrintHook = $closure;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function afterPrint(\Closure $closure): void
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        $this->afterPrintHook = $closure;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function createThead(): void
 | 
					    public function createThead(): void
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $columnKeys = [];
 | 
					        $columnKeys = [];
 | 
				
			||||||
@@ -118,7 +104,7 @@ class ListJsonTable
 | 
				
			|||||||
                }
 | 
					                }
 | 
				
			||||||
                foreach ($row as $key => $ceil) {
 | 
					                foreach ($row as $key => $ceil) {
 | 
				
			||||||
                    if ($this->issetColumn($key) and $this->is_fillable($key)) {
 | 
					                    if ($this->issetColumn($key) and $this->is_fillable($key)) {
 | 
				
			||||||
                        if($this->beforePrintCell and $ceil) {
 | 
					                        if($this->beforePrintCell and $ceil !== null) {
 | 
				
			||||||
                            $ceil = $this->getCustomCeil($key, $ceil);
 | 
					                            $ceil = $this->getCustomCeil($key, $ceil);
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
                        $this->html .= "<td>" . $ceil . "</td>";
 | 
					                        $this->html .= "<td>" . $ceil . "</td>";
 | 
				
			||||||
@@ -294,37 +280,10 @@ class ListJsonTable
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    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;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function columns(array $data): void
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        foreach ($data as $key => $value) {
 | 
					 | 
				
			||||||
            $this->beforePrintCell[$key] = $value;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function getCustomCeil(string $key, string $ceil)
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        if (is_array($this->beforePrintCell)) {
 | 
					 | 
				
			||||||
            foreach ($this->beforePrintCell as $column => $closure) {
 | 
					 | 
				
			||||||
                if ($key == $column) {
 | 
					 | 
				
			||||||
                    $hook = $closure;
 | 
					 | 
				
			||||||
                    $ceil = $hook($ceil);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
            $hook = $this->beforePrintCell;
 | 
					 | 
				
			||||||
            $ceil = $hook($key, $ceil);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return $ceil;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -4,20 +4,12 @@ namespace Itguild\Tables;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
use Itguild\Tables\traits\CreateParams;
 | 
					use Itguild\Tables\traits\CreateParams;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ViewJsonTable
 | 
					class ViewJsonTable extends JasonTable
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    use CreateParams;
 | 
					    use CreateParams;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private array $data;
 | 
					    private array $data;
 | 
				
			||||||
 | 
					 | 
				
			||||||
    private string $html = "";
 | 
					 | 
				
			||||||
    private string $json;
 | 
					    private string $json;
 | 
				
			||||||
 | 
					 | 
				
			||||||
    private \Closure|array|false $beforePrintCell;
 | 
					 | 
				
			||||||
    private \Closure|false $beforePrintTable;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private \Closure|false $afterPrintTable;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private array $dataJson;
 | 
					    private array $dataJson;
 | 
				
			||||||
    public function __construct($json)
 | 
					    public function __construct($json)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
@@ -42,7 +34,7 @@ class ViewJsonTable
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        foreach ($this->data['meta']['rows'] as $key => $row){
 | 
					        foreach ($this->data['meta']['rows'] as $key => $row){
 | 
				
			||||||
            if ($this->issetRow($key)){
 | 
					            if ($this->issetRow($key)){
 | 
				
			||||||
                if ($this->beforePrintCell and $this->dataJson[$key]) {
 | 
					                if ($this->beforePrintCell and $this->dataJson[$key] !== null) {
 | 
				
			||||||
                    $this->dataJson[$key] = $this->getCustomCeil($key, $this->dataJson[$key]);
 | 
					                    $this->dataJson[$key] = $this->getCustomCeil($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>";
 | 
				
			||||||
@@ -81,47 +73,11 @@ class ViewJsonTable
 | 
				
			|||||||
        $this->endTable();
 | 
					        $this->endTable();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function beforeTable(\Closure $closure): void
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        $this->beforePrintTable = $closure;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function afterTable(\Closure $closure): void
 | 
					//    public function rows(array $data): void
 | 
				
			||||||
    {
 | 
					//    {
 | 
				
			||||||
        $this->afterPrintTable = $closure;
 | 
					//        foreach ($data as $key => $value) {
 | 
				
			||||||
    }
 | 
					//            $this->beforePrintCell[$key] = $value;
 | 
				
			||||||
 | 
					//        }
 | 
				
			||||||
    public function render(): void
 | 
					//    }
 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        echo $this->html;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function setBeforePrintCell(\Closure $closure): void
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        $this->beforePrintCell = $closure;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function rows(array $data): void
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        foreach ($data as $key => $value) {
 | 
					 | 
				
			||||||
            $this->beforePrintCell[$key] = $value;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function getCustomCeil(string $key, string $ceil)
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        if (is_array($this->beforePrintCell)) {
 | 
					 | 
				
			||||||
            foreach ($this->beforePrintCell as $row => $closure) {
 | 
					 | 
				
			||||||
                if ($key == $row) {
 | 
					 | 
				
			||||||
                    $hook = $closure;
 | 
					 | 
				
			||||||
                    $ceil = $hook($ceil);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
            $hook = $this->beforePrintCell;
 | 
					 | 
				
			||||||
            $ceil = $hook($key, $ceil);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return $ceil;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user