PHP HTML DOM Builder | Cododel
CODODELDEV
EN / RU
Back to Deck
[utility]

PHP HTML DOM Builder

ИСХОДНИК PHP
ВЕРСИЯ 1.0
АВТОР Cododel

Полноценная библиотека для построения HTML на PHP, предоставляющая fluent объектно-ориентированный интерфейс для конструирования HTML элементов. Изначально создана для построения сложных таблиц с модульной архитектурой.

Возможности

  • Fluent API: Цепочки методов для чистого, читаемого кода
  • Типобезопасность: Классы для конкретных элементов (Table, Row, Cell)
  • Модульность: Расширение через кастомные модули (например, TailwindModule)
  • Статическая фабрика: Метод make() для удобного создания экземпляров
  • Атрибуты и классы: Простая манипуляция HTML атрибутами и CSS классами
  • Вложенные дочерние элементы: Полная поддержка иерархических структур

Применение

Идеально подходит для генерации сложных HTML структур в PHP, особенно таблиц с финансовыми отчетами, дата-гридами или админ-панелями:

$table = Table::make()
->class('text-center')
->children([
Row::make([
Cell::make("Финансовый результат по видам деятельности, связанным с исполнением договора №")
->class('font-bold')
->width(200)
->colSpan(2),
Cell::make($contract_number),
Cell::make("от"),
Cell::make($contract_date),
]),
Row::make([
Cell::make("Параметры: ИГК")->colSpan(2),
Cell::make($igk),
]),
Row::make([
Cell::make("Показатель")->span(2, 2),
Cell::make("Доходы")->rowSpan(2),
Cell::make("Расходы")->colSpan(3),
Cell::make("Прибыль (+) убыток (-)")->rowSpan(2),
]),
]);
echo $table->render();

Архитектура

  • HTMLElement: Базовый абстрактный класс с логикой рендеринга
  • Table/Row/Cell: Конкретные реализации для табличных структур
  • Система модулей: Плагин-архитектура для кастомной функциональности
  • Паттерн фабрика: Статический make() для чистого создания экземпляров

Исходный код

HTMLElement.php (Базовый класс)

<?php
namespace App\Services\HTML;
use App\Services\HTML\Modules\Module;
abstract class HTMLElement
{
protected static $modules = [];
protected $attributes = [];
protected $classList = [];
protected $children = [];
public function __call($method, $args)
{
foreach (self::$modules as $module) {
$callback = $module::getCallback($this);
if (is_callable($callback)) {
return call_user_func($callback, ...$args);
}
}
return $this;
}
abstract protected static function getTagName(): string;
public function attr(string $name, $value)
{
$this->attributes[$name] = $value;
return $this;
}
public function getAttributes(): array
{
return $this->attributes;
}
public function render()
{
$attributes = '';
foreach ($this->attributes as $name => $value) {
$attributes .= " $name=\"$value\"";
}
if (!empty($this->classList)) {
$attributes .= ' class="' . implode(' ', $this->classList) . '"';
}
$html = "<{$this->getTagName()} $attributes>";
foreach ($this->children as $child) {
if ($child instanceof HTMLElement) $html .= $child->render();
else $html .= (string) $child;
}
$html .= "</{$this->getTagName()}>";
return $html;
}
public function children(array|HTMLElement|string|int $children): static
{
if (is_array($children)) {
foreach ($children as $child) {
$this->children($child);
}
} else {
$this->children[] = $children;
}
return $this;
}
public function class(string $class): static
{
$this->classList[] = $class;
return $this;
}
public function getInnerText(): string
{
$text = '';
foreach ($this->children as $child) {
if ($child instanceof HTMLElement) $text .= $child->getInnerText();
else $text .= $child;
}
return $text;
}
public function getInnerHTML(): string
{
$html = '';
foreach ($this->children as $child) {
if ($child instanceof HTMLElement) $html .= $child->render();
else $html .= $child;
}
return $html;
}
public function getOuterHTML(): string
{
return $this->render();
}
public static function registerModule(string $module)
{
$moduleName = $module::getModuleName();
if (!property_exists(static::class, $moduleName)) {
static::$modules[$moduleName] = $module;
} else {
throw new \Exception("Module name {$module::getModuleName()} is not allowed, it is a reserved name.");
}
}
public static function getModules(): array
{
return static::$modules;
}
public static function make(array|string|int|null $children = []): static
{
$element = new static();
if (!empty($children)) {
$element->children($children);
}
return $element;
}
}

Table.php

<?php
namespace App\Services\Table;
use App\Services\HTML\HTMLElement;
class Table extends HTMLElement
{
private $data;
public static function getTagName(): string
{
return 'table';
}
public function getValue(int $column, int $row)
{
return $this->data[$row][$column] ?? null;
}
}

Row.php

<?php
namespace App\Services\Table;
use App\Services\HTML\HTMLElement;
class Row extends HTMLElement
{
public static function getTagName(): string
{
return 'tr';
}
}

Cell.php

<?php
namespace App\Services\Table;
use App\Services\HTML\HTMLElement;
class Cell extends HTMLElement
{
public static function getTagName(): string
{
return 'td';
}
public function rowSpan(int $rowSpan): static
{
$this->attr('rowspan', $rowSpan);
return $this;
}
public function colSpan(int $colSpan): static
{
$this->attr('colspan', $colSpan);
return $this;
}
public function span(int $rowSpan = 1, int $colSpan = 1): static
{
return $this->rowSpan($rowSpan)->colSpan($colSpan);
}
public function width(int $width): static
{
$this->attr('width', $width);
return $this;
}
public function height(int $height): static
{
$this->attr('height', $height);
return $this;
}
}
[ ▲ 0 ]