HtmlBuilder classes for Object, Array, String Replace build

Object build is a replicata from the JS one
Array is similar but build on pure Array elements
String replace is just a simple string replacer for now

General\Error for overall error handling
General\Settings for Object/Array based checks and settings
This commit is contained in:
Clemens Schwaighofer
2023-06-27 18:30:26 +09:00
parent 3606de1a00
commit 0436cfe3da
14 changed files with 1999 additions and 0 deletions

View File

@@ -0,0 +1,83 @@
<?php // phpcs:ignore warning
/**
* @phan-file-suppress PhanTypeSuspiciousStringExpression
*/
declare(strict_types=1);
error_reporting(E_ALL | E_STRICT | E_ERROR | E_WARNING | E_PARSE | E_COMPILE_ERROR);
ob_start();
// basic class test file
define('USE_DATABASE', false);
// sample config
require 'config.php';
// define log file id
$LOG_FILE_ID = 'classTest-html_build-block';
ob_end_flush();
use CoreLibs\Template\HtmlBuilder\Block;
$log = new CoreLibs\Logging\Logging([
'log_folder' => BASE . LOG,
'log_file_id' => $LOG_FILE_ID,
'log_per_date' => true,
]);
// define a list of from to color sets for conversion test
$PAGE_NAME = 'TEST CLASS: HTML BUILD: BLOCK';
print "<!DOCTYPE html>";
print "<html><head><title>" . $PAGE_NAME . "</title><head>";
print "<body>";
print '<div><a href="class_test.php">Class Test Master</a></div>';
print '<div><h1>' . $PAGE_NAME . '</h1></div>';
$el = Block::cel('div', 'el-1', 'Content', ['red'], ['onClick' => 'javascript:alert(\'JS alert\');']);
print "<pre>" . htmlentities(Block::buildHtml($el)) . "</pre>";
$el_a = Block::cel('div', 'u-id', '', ['base', 'cool']);
$el_a_1 = Block::cel('span', 's-id-1', 'Span A', ['bold']);
$el_a_2 = Block::cel('span', 's-id-2', 'Span B');
$el_a_3 = Block::cel('a', 'link-a', 'Title', ['l-highlight'], ['OnClick' => 'Foo();']);
$el_a_2 = Block::aelx($el_a_2, $el_a_3);
// css changes before added to array
$el_a_1 = Block::acssel($el_a_1, 'italic', 'green', 'italic', 'font-large');
$el_a_1 = Block::rcssel($el_a_1, 'green');
// switch
$el_a_1 = Block::scssel($el_a_1, ['one', 'two', 'three'], ['three']);
// this will add el_a_2 to the el_a block
$el_a_1 = Block::aelx($el_a_1, $el_a_2);
$el_a = Block::aelx($el_a, $el_a_1, $el_a_2);
// this will not update el_a
// $el_a_1 = Block::aelx($el_a_1, $el_a_2);
$el_a_list = [];
$el_a_list[] = Block::cel('foo', 'foo-A');
$el_a_list[] = Block::cel('bar', 'foo-B');
$el_a_list[] = Block::cel('baz', 'foo-C');
$el_a_list[] = Block::cel('br');
$el_a_list[] = Block::cel('input');
echo "<hr>";
print "EL_A: <pre>" . print_r($el_a, true) . "</pre>";
echo "<hr>";
print "phfo(\$el_o): <pre>" . htmlentities(Block::buildHtml($el_a, true)) . "</pre>";
echo "<hr>";
print "phfa(\$el_list): <pre>" . htmlentities(Block::buildHtmlFromList($el_a_list, true)) . "</pre>";
echo "<hr>";
// self loop test (will not trigger, are arrays)
$el_s = Block::cel('div', 'id-s', 'Self', []);
$el_s = Block::aelx($el_s, $el_s);
print "phfo(\$el_): <pre>" . htmlentities(Block::buildHtml($el_s, true)) . "</pre>";
print "</body></html>";
// __END__

View File

@@ -0,0 +1,119 @@
<?php // phpcs:ignore warning
/**
* @phan-file-suppress PhanTypeSuspiciousStringExpression
*/
declare(strict_types=1);
error_reporting(E_ALL | E_STRICT | E_ERROR | E_WARNING | E_PARSE | E_COMPILE_ERROR);
ob_start();
// basic class test file
define('USE_DATABASE', false);
// sample config
require 'config.php';
// define log file id
$LOG_FILE_ID = 'classTest-html_build';
ob_end_flush();
use CoreLibs\Template\HtmlBuilder\Element;
use CoreLibs\Template\HtmlBuilder\General\Error;
use CoreLibs\Debug\Support;
$log = new CoreLibs\Logging\Logging([
'log_folder' => BASE . LOG,
'log_file_id' => $LOG_FILE_ID,
'log_per_date' => true,
]);
// define a list of from to color sets for conversion test
$PAGE_NAME = 'TEST CLASS: HTML BUILD';
print "<!DOCTYPE html>";
print "<html><head><title>" . $PAGE_NAME . "</title><head>";
print "<body>";
print '<div><a href="class_test.php">Class Test Master</a></div>';
print '<div><h1>' . $PAGE_NAME . '</h1></div>';
$el = new Element('div', 'el-1', 'Content', ['red'], ['onClick' => 'javascript:alert(\'JS alert\');']);
print "<pre>" . htmlentities($el->buildHtml()) . "</pre>";
$el_o = new Element('div', 'u-id', '', ['base', 'cool']);
$el_o_1 = new Element('span', 's-id-1', 'Span A', ['bold']);
$el_o_2 = new Element('span', 's-id-2', 'Span B');
$el_o_3 = new Element('a', 'link-a', 'Title', ['l-highlight'], ['OnClick' => 'Foo();']);
$el_o_2->addSub($el_o_3);
$el_o->addSub($el_o_1, $el_o_2);
$el_o_1->addCss('italic', 'green', 'italic', 'font-large')
->removeCss('green');
$el_o_2->addCss('wrong-css')
->removeCss('wrong-css', 'correct-css');
$el_o_2->addCss('a', 'b')->removeCss('correct-css');
$el_o_1->addSub($el_o_2);
// var_dump($el2);
$el_o_list = [];
$el_o_list[] = new Element('foo', 'foo-A');
$el_o_list[] = new Element('bar', 'foo-B');
$el_o_list[] = new Element('baz', 'foo-C');
$el_o_list[] = new Element('br');
$el_o_list[] = new Element('input', 'tag', '', [], ['name' => 'foo', 'value' => 'ABC']);
// $el2->resetSub();
// var_dump($el2);
echo "<hr>";
print "EL_O: <pre>" . print_r($el_o, true) . "</pre>";
echo "<hr>";
print "buildHtml(): <pre>" . htmlentities($el_o->buildHtml()) . "</pre>";
echo "<hr>";
print "phfo(\$el_o): <pre>" . htmlentities($el_o::printHtmlFromObject($el_o, true)) . "</pre>";
echo "<hr>";
print "phfa(\$el_list): <pre>" . htmlentities($el_o::buildHtmlFromList($el_o_list, true)) . "</pre>";
echo "<hr>";
// self loop
$el_s = new Element('div', 'id-s', 'Self');
$el_s->addSub($el_s, new Element('span', '', 'Span'));
// var_dump($el_s);
print "el_s, buildHtml(): <pre>" . htmlentities($el_s->buildHtml()) . "</pre>";
$el_s_2 = new Element('div', 'id-s', 'Self', []);
$el_s_2->addSub(
new Element('span', 's-1', 's 1'),
new Element('span', 's-2', 's 2'),
);
$el_s_3 = new Element('div', 'id-3', 'ID 3');
$el_s_3->addSub($el_s_2);
$el_s_2->addSub($el_s_2);
// print "<pre>" . var_export($el_s_3, true) . "</pre>";
print "el_s_3, buildHtml(): <pre>" . htmlentities($el_s_3->buildHtml()) . "</pre>";
echo "<hr>";
Error::resetMessages();
$el_er = new Element('');
print "Errors: <pre>" . print_r(Error::getMessages(), true) . "</pre>";
print "Warning: " . Support::printToString(Error::hasWarning()) . "<br>";
print "Error: " . Support::printToString(Error::hasError()) . "<br>";
Error::resetMessages();
$el_er = new Element('123123');
print "Errors: <pre>" . print_r(Error::getMessages(), true) . "</pre>";
print "Warning: " . Support::printToString(Error::hasWarning()) . "<br>";
print "Error: " . Support::printToString(Error::hasError()) . "<br>";
print "</body></html>";
// __END__

View File

@@ -0,0 +1,81 @@
<?php // phpcs:ignore warning
/**
* @phan-file-suppress PhanTypeSuspiciousStringExpression
*/
declare(strict_types=1);
error_reporting(E_ALL | E_STRICT | E_ERROR | E_WARNING | E_PARSE | E_COMPILE_ERROR);
ob_start();
// basic class test file
define('USE_DATABASE', false);
// sample config
require 'config.php';
// define log file id
$LOG_FILE_ID = 'classTest-html_build-replace';
ob_end_flush();
use CoreLibs\Template\HtmlBuilder\Replace;
$log = new CoreLibs\Logging\Logging([
'log_folder' => BASE . LOG,
'log_file_id' => $LOG_FILE_ID,
'log_per_date' => true,
]);
// define a list of from to color sets for conversion test
$PAGE_NAME = 'TEST CLASS: HTML BUILD: REPLACE';
print "<!DOCTYPE html>";
print "<html><head><title>" . $PAGE_NAME . "</title><head>";
print "<body>";
print '<div><a href="class_test.php">Class Test Master</a></div>';
print '<div><h1>' . $PAGE_NAME . '</h1></div>';
$html_block = <<<HTML
<div id="{ID}" class="{CSS}">
{CONTENT}
</div>
HTML;
print "<pre>" . htmlentities(Replace::replaceData(
$html_block,
[
'ID', 'CSS', '{CONTENT}'
],
[
'block-id', join(',', ['blue', 'red']),
'Some content here<br>with bla bla inside'
]
)) . "</pre>";
Replace::loadElements(
['foo', $html_block],
['bar', <<<HTML
<span id="{ID}">{CONTENT}</span>
HTML]
);
print "Get: <pre>" . htmlentities(Replace::getElement('bar') ?: '') . '</pre>';
print "Build element: <pre>" . htmlentities(Replace::buildElement(
'bar',
['ID}', '{CONTENT'],
['new-id', 'Test cow 日本語']
)) . '</pre>' ;
print "Build element as replace: <pre>" . htmlentities(Replace::buildElement(
'bar',
['ID}', '{CONTENT'],
['diff-id', 'Test cow 日本語. More text plus'],
'rpl-1'
)) . '</pre>' ;
print "Get replacement: <pre>" . htmlentities(Replace::getReplaceBlock('rpl-1')) . "</pre>";
print "</body></html>";
// __END__

View File

@@ -81,6 +81,9 @@ $test_files = [
'class_test.encryption.php' => 'Class Test: ENCRYPTION',
'class_test.math.php' => 'Class Test: MATH',
'class_test.html.php' => 'Class Test: HTML/ELEMENTS',
'class_test.html_build.element.php' => 'Class Test: HTML BUILDER: ELEMENT',
'class_test.html_build.block.php' => 'Class Test: HTML BUILDER: BLOCK',
'class_test.html_build.replace.php' => 'Class Test: HTML BUILDER: REPLACE',
'class_test.email.php' => 'Class Test: EMAIL',
'class_test.create_email.php' => 'Class Test: CREATE EMAIL',
'class_test.uids.php' => 'Class Test: UIDS',

View File

@@ -0,0 +1,248 @@
<?php
/**
* AUTOR: Clemens Schwaighofer
* CREATED: 2023/6/1
* DESCRIPTION:
* html builder: array
* static build for array lists (not objects)
*/
namespace CoreLibs\Template\HtmlBuilder;
use CoreLibs\Template\HtmlBuilder\General\Settings;
class Block
{
/**
* Undocumented function
*
* @param string $tag
* @param string $id
* @param string $content
* @param array<string> $css,
* @param array<string,string> $options
*/
public static function cel(
string $tag,
string $id = '',
string $content = '',
array $css = [],
array $options = []
): array {
return [
'tag' => $tag,
'id' => $id,
'name' => $options['name'] ?? '',
'content' => $content,
'css' => $css,
'options' => $options,
'sub' => [],
];
}
/**
* Search element tree for id and add
* if id is empty add at current
*
* @param array<mixed> $base
* @param array<mixed> $attach
* @param string $id
* @return array<mixed>
*/
public static function ael(
array $base,
array $attach,
string $id = ''
): array {
// no id or matching id
if (
empty($id) ||
$base['id'] == $id
) {
self::addSub($base, $attach);
return $base;
}
// find id in 'id' in all 'sub'
foreach ($base['sub'] as $el) {
$el = self::ael($el, $attach, $id);
}
return $base;
}
/**
* Undocumented function
*
* @param array<mixed> $base
* @param array<mixed> ...$attach
* @return array<mixed>
*/
public static function aelx(
array $base,
array ...$attach
): array {
$base = self::addSub($base, ...$attach);
return $base;
}
/**
* Undocumented function
*
* @param array<mixed> $element
* @param array<mixed> $sub
* @return array<mixed>
*/
public static function addSub(array $element, array ...$sub): array
{
if (!isset($element['sub'])) {
$element['sub'] = [];
}
array_push($element['sub'], ...$sub);
return $element;
}
/**
* Undocumented function
*
* @param array<mixed> $element
* @return array<mixed>
*/
public static function resetSub(array $elment): array
{
$element['sub'] = [];
return $element;
}
// CSS Elements
/**
* Undocumented function
*
* @param array<mixed> $element
* @param string ...$css
* @return array<mixed>
*/
public static function acssel(array $element, string ...$css): array
{
$element['css'] = array_unique(array_merge($element['css'] ?? [], $css));
return $element;
}
/**
* Undocumented function
*
* @param string ...$css
* @return array<mixed>
*/
public static function rcssel(array $element, string ...$css): array
{
$element['css'] = array_diff($element['css'] ?? [], $css);
return $element;
}
/**
* Undocumented function
* scssel (switch) is not supported
* use rcssel -> acssel
*
* @param array $element
* @param array $rcss
* @param array $acss
* @return array
*/
public static function scssel(array $element, array $rcss, array $acss): array
{
return self::acssel(
self::rcssel($element, ...$rcss),
...$acss
);
}
/**
* Undocumented function
* alias phfo
*
* @param array<mixed> $tree
* @param bool $add_nl [default=false]
* @return string
*/
public static function buildHtml(array $tree, bool $add_nl = false): string
{
if (empty($tree['tag'])) {
return '';
}
// print "D01: " . microtime(true) . "<br>";
$line = '<' . $tree['tag'];
if (!empty($tree['id'])) {
$line .= ' id="' . $tree['id'] . '"';
if (in_array($tree['tag'], Settings::NAME_ELEMENTS)) {
$line .= ' name="'
. (!empty($tree['name']) ? $tree['name'] : $tree['id'])
. '"';
}
}
if (count($tree['css'])) {
$line .= ' class="' . join(' ', $tree['css']) . '"';
}
foreach ($tree['options'] ?? [] as $key => $item) {
if (in_array($key, Settings::SKIP_OPTIONS)) {
continue;
}
$line .= ' ' . $key . '="' . $item . '"';
}
$line .= '>';
if (!empty($tree['content'])) {
$line .= $tree['content'];
}
// sub nodes
foreach ($tree['sub'] ?? [] as $sub) {
if ($add_nl === true) {
$line .= "\n";
}
$line .= self::buildHtml($sub, $add_nl);
if ($add_nl === true) {
$line .= "\n";
}
}
// close line if needed
if (!in_array($tree['tag'], Settings::NO_CLOSE)) {
$line .= '</' . $tree['tag'] . '>';
}
return $line;
}
/**
* Undocumented function
* alias phfa
*
* @param array<mixed> $list
* @return string
*/
public static function buildHtmlFromList(array $list): string
{
$output = '';
foreach ($list as $el) {
$output .= self::buildHtml($el);
}
return $output;
}
/**
* Undocumented function
* wrapper for buildHtmlFromList
*
* @param array<Element> $list array of Elements to build string from
* @param bool $add_nl [default=false] Optional output string line break
* @return string build html as string
*/
public static function printHtmlFromArray(array $list, bool $add_nl = false): string
{
return self::buildHtmlFromList($list, $add_nl);
}
}
// __END__

View File

@@ -0,0 +1,519 @@
<?php
/**
* AUTOR: Clemens Schwaighofer
* CREATED: 2023/6/1
* DESCRIPTION:
* html builder: element
* nested and connected objects
*/
declare(strict_types=1);
namespace CoreLibs\Template\HtmlBuilder;
use CoreLibs\Template\HtmlBuilder\General\Settings;
use CoreLibs\Template\HtmlBuilder\General\Error;
class Element
{
/** @var string */
private string $tag = '';
/** @var string */
private string $id = '';
/** @var string */
private string $name = '';
/** @var string */
private string $content = '';
/** @var array<string> */
private array $css = [];
/** @var array<string,mixed> */
private array $options = [];
/** @var array<Element> list of elements */
private array $sub = [];
/**
* create new html element
*
* @param string $tag html tag (eg div, button, etc)
* @param string $id html tag id, used also for name if name
* not set in $options
* @param string $content content text inside, eg <div>Content</div>
* if sub elements exist, they are added after content
* @param array<string> $css array of css names, put style in $options
* @param array<string,string> $options Additional element options in
* key = value format
* eg: onClick => 'something();'
* id, css are skipped
* name only set on input/button
*/
public function __construct(
string $tag,
string $id = '',
string $content = '',
array $css = [],
array $options = []
) {
// tag must be letters only
if (!preg_match("/^[A-Za-z]+$/", $tag)) {
Error::setError(
'201',
'invalid or empty tag',
['tag' => $tag]
);
return;
}
$this->tag = $tag;
// invalid id and name check too
// be strict: [a-zA-Z0-9], -, _
// cannot start with digit, two hyphens or a hyphen with a digit:
// 0abc
// __abc
// _0abc
if (
!empty($id) &&
!preg_match("/^[A-Za-z][\w-]*$/", $id)
) {
Error::setWarning(
'202',
'possible invalid id',
['id' => $id, 'tag' => $this->tag]
);
}
$this->id = $id;
if (
!empty($options['name']) &&
!preg_match("/^[A-Za-z][\w-]*$/", $options['name'])
) {
Error::setWarning(
'203',
'possible invalid name',
['name' => $options['name'], 'id' => $this->id, 'tag' => $this->tag]
);
}
// same as id
$this->name = $options['name'] ?? '';
// anything allowed
$this->content = $content;
// should do check for empty/invalid css
foreach ($css as $_css) {
if (empty($_css)) {
Error::setError(
'204',
'cannot have empty css string',
);
continue;
}
// -?[_A-Za-z][_A-Za-z0-9-]*
if (!preg_match("/^-?[_A-Za-z][_A-Za-z0-9-]*$/", $_css)) {
Error::setWarning(
'205',
'possible invalid css string',
['css' => $_css, 'id' => $this->id, 'tag' => $this->tag]
);
}
$this->css[] = $_css;
}
// some basic options check
foreach ($options as $key => $value) {
if (empty($key)) {
Error::setError(
'110',
'Cannot set option with empty key',
['id' => $this->id, 'tag' => $this->tag]
);
continue;
}
if ($value === null) {
Error::setError(
'210',
'Cannot set option with null value',
['id' => $this->id, 'tag' => $this->tag]
);
continue;
}
$this->options[$key] = $value;
}
}
/**
* get the html tag id
*
* @return string HTML element id
*/
public function getId(): string
{
return $this->id;
}
/**
* get the tag name
*
* @return string HTML element tag
*/
public function getTag(): string
{
return $this->tag;
}
/**
* get the name if set
*
* @return string Optional HTML name (eg for input)
*/
public function getName(): string
{
return $this->name;
}
/**
* Set new content for element
*
* @param string $content
* @return void
*/
public function setContent(string $content): void
{
$this->content = $content;
}
/**
* get the elment text content (not sub elements)
*
* @return string HTML content text as is
*/
public function getContent(): string
{
return $this->content;
}
/**
* set or update options
*
* @param array<string,mixed> $options
* @return void
*/
public function setOptions(array $options): void
{
foreach ($options as $key => $value) {
if (empty($key)) {
Error::setError(
'110',
'Cannot set option with empty key',
['id' => $this->getId(), 'tag' => $this->getTag()]
);
continue;
}
// if data is null
if ($value === null) {
unset($this->options[$key]);
continue;
}
$this->options[$key] = $value;
}
}
/**
* get the options array
* also holds "name" option
* anything like: style, javascript, value or any other html tag option
* right side can be empty but not null
*
* @return array<string,string> get options as list html option name and value
*/
public function getOptions(): array
{
return $this->options;
}
// Sub Elements
/**
* get the sub elements (array of Elements)
*
* @return array<Element> Array of Elements (that can have sub elements)
*/
public function getSub(): array
{
return $this->sub;
}
/**
* add one or many sub elements (add at the end)
*
* @param Element $sub One or many elements to add
* @return void
*/
public function addSub(Element ...$sub): void
{
foreach ($sub as $_sub) {
// if one of the elements is the same as this class, ignore it
// with this we avoid self reference loop
if ($_sub == $this) {
Error::setError(
'100',
'Cannot assign Element, this would create a loop',
['id' => $this->getId(), 'tag' => $this->getTag()]
);
continue;
}
array_push($this->sub, $_sub);
}
}
/**
* Remove an element from the sub array
* By pos in array or id set on first level
*
* @param int|string $id String id name or int pos number in array
* @return void
*/
public function removeSub(int|string $id): void
{
// find element with id and remove it
// or when number find pos in sub and remove it
if (is_int($id)) {
if (!isset($this->sub[$id])) {
return;
}
unset($this->sub[$id]);
return;
}
// only on first level
foreach ($this->sub as $pos => $el) {
if ($el['id'] === $id) {
unset($this->sub[$pos]);
return;
}
}
}
/**
* remove all sub elements
*
* @return void
*/
public function resetSub(): void
{
$this->sub = [];
}
// CSS Elements
/**
* get the current set css elements
*
* @return array<string> list of css element entries
*/
public function getCss(): array
{
return $this->css;
}
/**
* add one or many new css elements
* Note that we can chain: add/remove/reset
*
* @param string ...$css one or more css strings to add
* @return Element Current element for chaining
*/
public function addCss(string ...$css): Element
{
$this->css = array_unique(array_merge($this->css, $css));
return $this;
}
/**
* remove one or more css elements
* Note that we can chain: add/remove/reset
*
* @param string ...$css one or more css strings to remove
* @return Element Current element for chaining
*/
public function removeCss(string ...$css): Element
{
$this->css = array_diff($this->css, $css);
return $this;
}
/**
* unset all css elements
* Note that we can chain: add/remove/reset
*
* @return Element
*/
public function resetCss(): Element
{
$this->css = [];
return $this;
}
// build output from tree
/**
* build html string from the current element tree (self)
* or from the Element tree given as parameter
* if $add_nl is set then new lines are added before each sub element added
* no indet is done (tab or other)
*
* @param Element|null $tree Different Element tree to build
* if not set (null), self is used
* @param bool $add_nl [default=false] Optional output string line breaks
* @return string HTML as string
*/
public function buildHtml(Element $tree = null, bool $add_nl = false): string
{
// print "D01: " . microtime(true) . "<br>";
if ($tree === null) {
$tree = $this;
}
$line = '<' . $tree->getTag();
if ($tree->getId()) {
$line .= ' id="' . $tree->getId() . '"';
if (in_array($tree->getTag(), Settings::NAME_ELEMENTS)) {
$line .= ' name="'
. (!empty($tree->getName()) ? $tree->getName() : $tree->getId())
. '"';
}
}
if (count($tree->getCss())) {
$line .= ' class="' . join(' ', $tree->getCss()) . '"';
}
foreach ($tree->getOptions() as $key => $item) {
// skip
if (in_array($key, Settings::SKIP_OPTIONS)) {
continue;
}
$line .= ' ' . $key . '="' . $item . '"';
}
$line .= '>';
if (strlen($tree->getContent()) > 0) {
$line .= $tree->getContent();
}
// sub nodes
foreach ($tree->getSub() as $sub) {
if ($add_nl === true) {
$line .= "\n";
}
$line .= $tree->buildHtml($sub, $add_nl);
if ($add_nl === true) {
$line .= "\n";
}
}
// close line if needed
if (!in_array($tree->getTag(), Settings::NO_CLOSE)) {
$line .= '</' . $tree->getTag() . '>';
}
return $line;
}
// this is static
/**
* Builds a single string from an array of elements
* a new line can be added before each new element if $add_nl is set to true
*
* @param array<Element> $list array of Elements, uses buildHtml internal
* @param bool $add_nl [default=false] Optional output string line breaks
* @return string HTML as string
*/
public static function buildHtmlFromList(array $list, bool $add_nl = false): string
{
$output = '';
foreach ($list as $el) {
if (!empty($output) && $add_nl === true) {
$output .= "\n";
}
$output .= $el->buildHtml();
}
return $output;
}
// so we can call builder statically
/**
* Search element tree for id and add
* if id is empty add at element given in parameter $base
*
* @param Element $base Element to attach to
* @param Element $attach Element to attach (single)
* @param string $id Optional id, if empty then attached at the end
* If set will loop through ALL sub elements until
* matching id found. if not found, not added
* @return Element Element with attached sub element
*/
public static function addElementWithId(
Element $base,
Element $attach,
string $id = ''
): Element {
// no id or matching id
if (
empty($id) ||
$base->getId() == $id
) {
$base->addSub($attach);
return $base;
}
// find id in 'id' in all 'sub'
foreach ($base->getSub() as $el) {
self::addElementWithId($el, $attach, $id);
}
return $base;
}
/**
* add one or more elemens to $base
*
* @param Element $base Element to attach to
* @param Element ...$attach Element or Elements to attach
* @return Element Element with attached sub elements
*/
public static function addElement(
Element $base,
Element ...$attach
): Element {
// we must make sure we do not self attach
$base->addSub(...$attach);
return $base;
}
/**
* Static call version for building
* not recommended to be used, rather use "Element->buildHtml()"
* wrapper for buildHtml
*
* @param Element $element Element tree to build
* if not set returns empty string
* @param bool $add_nl [default=false] Optional output string line break
* @return string build html as string
* @deprecated Do not use, use Element->buildHtml() instead
*/
public static function printHtmlFromObject(Element $tree = null, bool $add_nl = false): string
{
// nothing ->bad
if ($tree === null) {
return '';
}
return $tree->buildHtml(add_nl: $add_nl);
}
/**
* Undocumented function
* wrapper for buildHtmlFromList
*
* @param array<Element> $list array of Elements to build string from
* @param bool $add_nl [default=false] Optional output string line break
* @return string build html as string
*/
public static function printHtmlFromArray(array $list, bool $add_nl = false): string
{
return self::buildHtmlFromList($list, $add_nl);
}
}
// __END__

View File

@@ -0,0 +1,127 @@
<?php
/**
* AUTOR: Clemens Schwaighofer
* CREATED: 2023/6/27
* DESCRIPTION:
* Error logging for the HtmlBuilder systs
*/
declare(strict_types=1);
namespace CoreLibs\Template\HtmlBuilder\General;
class Error
{
/** @var array{level:string,id:string,message:string,context:array<mixed>} */
private static array $messages = [];
/**
* internal writer for messages
*
* @param string $level
* @param string $id
* @param string $message
* @param array $context
* @return void
*/
private static function writeContent(
string $level,
string $id,
string $message,
array $context
): void {
self::$messages[] = [
'level' => $level,
'id' => $id,
'message' => $message,
'context' => $context,
];
}
/**
* warning collector for all internal string errors
* builds an warning with warning id, message text and array with optional content
*
* @param string $id
* @param string $message
* @param array<mixed> $context
* @return void
*/
public static function setWarning(string $id, string $message, array $context = []): void
{
self::writeContent('Warning', $id, $message, $context);
}
/**
* error collector for all internal string errors
* builds an error with error id, message text and array with optional content
*
* @param string $id
* @param string $message
* @param array<mixed> $context
* @return void
*/
public static function setError(string $id, string $message, array $context = []): void
{
self::writeContent('Error', $id, $message, $context);
}
/**
* Return all set errors
*
* @return array<mixed>
*/
public static function getMessages(): array
{
return self::$messages;
}
/**
* Reset all errors
*
* @return void
*/
public static function resetMessages(): void
{
self::$messages = [];
}
/**
* internal level in message array exists check
*
* @param string $level
* @return bool
*/
private static function hasLevel(string $level): bool
{
return array_filter(
self::$messages,
function ($var) use ($level) {
return ($var['level'] ?? '') == $level ? true : false;
}
) === [] ? false : true;
}
/**
* Check if any error is set
*
* @return bool
*/
public static function hasError(): bool
{
return self::hasLevel('Error');
}
/**
* Check if any warning is set
*
* @return bool
*/
public static function hasWarning(): bool
{
return self::hasLevel('Warning');
}
}
// __END__

View File

@@ -0,0 +1,69 @@
<?php
/**
* AUTOR: Clemens Schwaighofer
* CREATED: 2023/7/22
* DESCRIPTION:
* General settings for html elements
*/
declare(strict_types=1);
namespace CoreLibs\Template\HtmlBuilder\General;
class Settings
{
/** @var array>string> list of html elements that can have the name tag */
public const NAME_ELEMENTS = [
'button',
'fieldset',
'form',
'iframe',
'input',
'map',
'meta',
'object',
'output',
'param',
'select',
'textarea',
];
/** @var array<string> options key entries to be skipped in build */
public const SKIP_OPTIONS = [
'id',
'name',
'class',
];
/** @var array<string> html elements that don't need to be closed */
public const NO_CLOSE = [
'input',
'br',
'img',
'hr',
'area',
'col',
'keygen',
'wbr',
'track',
'source',
'param',
'command',
// only in header
'base',
'meta',
'link',
'embed',
];
/** @var array<string> invalid tags, not allowed in body */
public const NOT_IN_BODY_ALLOWED = [
'base',
'meta',
'link',
'embed', // not sure
];
}
// __END__

View File

@@ -0,0 +1,194 @@
<?php
/**
* AUTOR: Clemens Schwaighofer
* CREATED: 2023/6/23
* DESCRIPTION:
* Simeple string replace calls for elements
*/
declare(strict_types=1);
namespace CoreLibs\Template\HtmlBuilder;
use CoreLibs\Template\HtmlBuilder\General\Error;
class Replace
{
/** @var array<string,string> */
private static array $elements = [];
/** @var array<string,array<string,string>> */
private static array $replace = [];
/**
* load html blocks into array for repeated usage
* each array group parameter has 0: index, 1: content
* There is no content check done.
* index must be non empty (but has no fixed format)
* if same index is tried twice it will set an error and skip
*
* @param array<string,string> ...$element Elements to load
* @return bool False if double index or other error
* True on ok
*/
public static function loadElements(array ...$element): bool
{
$error = false;
foreach ($element as $el) {
$index = $el[0] ?? '';
if (empty($index)) {
$error = true;
Error::setError(
'310',
'Index cannot be an empty string',
[
'element' => $index
]
);
}
if (isset(self::$elements[$index])) {
$error = true;
Error::setError(
'311',
'Index already exists',
[
'element' => $index
]
);
}
// content check?
self::$elements[$index] = $el[1];
}
return $error;
}
/**
* update an element at index
* can also be used to reset (empty string)
*
* @param string $index
* @param string $element
* @return void
*/
public static function updateElement(string $index, string $element): void
{
if (!isset(self::$elements[$index])) {
Error::setError(
'312',
'Index does not exists',
[
'element' => $index
]
);
return;
}
// allow empty reset
self::$elements[$index] = $element;
}
/**
* get an element block at index
* if not found will return false
*
* @param string $index
* @return string|false
*/
public static function getElement(string $index): string|bool
{
if (!isset(self::$elements[$index])) {
Error::setError('321', 'Index not found in elements', ['element' => $index]);
return false;
}
return self::$elements[$index];
}
/**
* set a replacement block at index
* can be used for setting one block and using it agai
*
* @param string $index
* @param string $content
* @return void
*/
public static function setReplaceBlock(string $index, string $content): void
{
self::$replace[$index] = $content;
}
/**
* get replacement block at index, if not found return empty and set error
*
* @param string $index
* @return string
*/
public static function getReplaceBlock(string $index): string
{
if (!isset(self::$replace[$index])) {
Error::setError('331', 'Index not found in replace block', ['replace' => $index]);
return '';
}
return self::$replace[$index];
}
/**
* build and element on an index and either returns it or also sets it
* into the replace block array
* if index not found in relement list will return false
*
* @param string $index index of set element
* @param array<string> $replace array of text to search for
* @param array<string> $content content data to be set for replace
* @return string|false
*/
public static function buildElement(
string $index,
array $replace,
array $content,
string $replace_index = ''
): string|bool {
if (self::getElement($index) === false) {
return false;
}
if ($replace_index) {
self::setReplaceBlock(
$replace_index,
self::replaceData(self::$elements[$index], $replace, $content)
);
return self::getReplaceBlock($replace_index);
} else {
return self::replaceData(self::$elements[$index], $replace, $content);
}
}
/**
* main replace entries in text string
* elements to be replaced are in {} brackets. if they are missing in the
* replace array they will be added.
* if the replace and content count is not the same then an error will be thrown
*
* @param string $data
* @param array<string> $replace
* @param array<string> $content
* @return string|bool
*/
public static function replaceData(string $data, array $replace, array $content): string|bool
{
if (count($replace) != count($content)) {
Error::setError('340', 'Replace and content count differ');
return false;
}
// all replace data must have {} around
array_walk($replace, function (&$entry) {
if (!str_starts_with($entry, '{')) {
$entry = '{' . $entry;
}
if (!str_ends_with($entry, '}')) {
$entry .= '}';
}
});
// replace content
return str_replace($replace, $content, $data);
}
}
// __END__

View File

@@ -77,6 +77,11 @@ return array(
'CoreLibs\\Security\\CreateKey' => $baseDir . '/lib/CoreLibs/Security/CreateKey.php',
'CoreLibs\\Security\\Password' => $baseDir . '/lib/CoreLibs/Security/Password.php',
'CoreLibs\\Security\\SymmetricEncryption' => $baseDir . '/lib/CoreLibs/Security/SymmetricEncryption.php',
'CoreLibs\\Template\\HtmlBuilder\\Block' => $baseDir . '/lib/CoreLibs/Template/HtmlBuilder/Block.php',
'CoreLibs\\Template\\HtmlBuilder\\Element' => $baseDir . '/lib/CoreLibs/Template/HtmlBuilder/Element.php',
'CoreLibs\\Template\\HtmlBuilder\\General\\Error' => $baseDir . '/lib/CoreLibs/Template/HtmlBuilder/General/Error.php',
'CoreLibs\\Template\\HtmlBuilder\\General\\Settings' => $baseDir . '/lib/CoreLibs/Template/HtmlBuilder/General/Settings.php',
'CoreLibs\\Template\\HtmlBuilder\\Replace' => $baseDir . '/lib/CoreLibs/Template/HtmlBuilder/Replace.php',
'CoreLibs\\Template\\SmartyExtend' => $baseDir . '/lib/CoreLibs/Template/SmartyExtend.php',
'FileUpload\\Core\\qqUploadedFile' => $baseDir . '/lib/FileUpload/Core/qqUploadedFile.php',
'FileUpload\\Core\\qqUploadedFileForm' => $baseDir . '/lib/FileUpload/Core/qqUploadedFileForm.php',

View File

@@ -128,6 +128,11 @@ class ComposerStaticInit10fe8fe2ec4017b8644d2b64bcf398b9
'CoreLibs\\Security\\CreateKey' => __DIR__ . '/../..' . '/lib/CoreLibs/Security/CreateKey.php',
'CoreLibs\\Security\\Password' => __DIR__ . '/../..' . '/lib/CoreLibs/Security/Password.php',
'CoreLibs\\Security\\SymmetricEncryption' => __DIR__ . '/../..' . '/lib/CoreLibs/Security/SymmetricEncryption.php',
'CoreLibs\\Template\\HtmlBuilder\\Block' => __DIR__ . '/../..' . '/lib/CoreLibs/Template/HtmlBuilder/Block.php',
'CoreLibs\\Template\\HtmlBuilder\\Element' => __DIR__ . '/../..' . '/lib/CoreLibs/Template/HtmlBuilder/Element.php',
'CoreLibs\\Template\\HtmlBuilder\\General\\Error' => __DIR__ . '/../..' . '/lib/CoreLibs/Template/HtmlBuilder/General/Error.php',
'CoreLibs\\Template\\HtmlBuilder\\General\\Settings' => __DIR__ . '/../..' . '/lib/CoreLibs/Template/HtmlBuilder/General/Settings.php',
'CoreLibs\\Template\\HtmlBuilder\\Replace' => __DIR__ . '/../..' . '/lib/CoreLibs/Template/HtmlBuilder/Replace.php',
'CoreLibs\\Template\\SmartyExtend' => __DIR__ . '/../..' . '/lib/CoreLibs/Template/SmartyExtend.php',
'FileUpload\\Core\\qqUploadedFile' => __DIR__ . '/../..' . '/lib/FileUpload/Core/qqUploadedFile.php',
'FileUpload\\Core\\qqUploadedFileForm' => __DIR__ . '/../..' . '/lib/FileUpload/Core/qqUploadedFileForm.php',