HtmlBuilder: change all return error to Throw, update Element/StrinReplace

We do not return old style bool on error, we throw Exceptions: HtmlBuilderExcpetion

Element has more classes to set tag, id, etc with basic checks for valid data

String Replace to set strings is one array with key -> value entries

Errors thrown on index for element/replace blocks
This commit is contained in:
Clemens Schwaighofer
2023-06-28 14:16:44 +09:00
parent 3c37899a48
commit 1754ecf2ee
12 changed files with 316 additions and 118 deletions

View File

@@ -20,6 +20,7 @@ ob_end_flush();
use CoreLibs\Template\HtmlBuilder\Element;
use CoreLibs\Template\HtmlBuilder\General\Error;
use CoreLibs\Template\HtmlBuilder\General\HtmlBuilderExcpetion;
use CoreLibs\Debug\Support;
$log = new CoreLibs\Logging\Logging([
@@ -83,7 +84,11 @@ echo "<hr>";
// self loop
$el_s = new Element('div', 'id-s', 'Self');
$el_s->addSub($el_s, new Element('span', '', 'Span'));
try {
$el_s->addSub($el_s, new Element('span', '', 'Span'));
} catch (HtmlBuilderExcpetion $e) {
print "E: " . $e->getMessage() . " | " . $e->getTraceAsString() . "<br>";
}
// var_dump($el_s);
print "el_s, buildHtml(): <pre>" . htmlentities($el_s->buildHtml()) . "</pre>";
@@ -95,8 +100,12 @@ $el_s_2->addSub(
);
$el_s_3 = new Element('div', 'id-3', 'ID 3');
$el_s_3->addSub($el_s_2);
$el_s_2->addSub($el_s_2);
try {
$el_s_3->addSub($el_s_2);
$el_s_2->addSub($el_s_2);
} catch (HtmlBuilderExcpetion $e) {
print "E: " . $e->getMessage() . " | " . $e->getTraceAsString() . "<br>";
}
// print "<pre>" . var_export($el_s_3, true) . "</pre>";
@@ -104,12 +113,22 @@ print "el_s_3, buildHtml(): <pre>" . htmlentities($el_s_3->buildHtml()) . "</pre
echo "<hr>";
Error::resetMessages();
$el_er = new Element('');
try {
$el_er = new Element('');
} catch (HtmlBuilderExcpetion $e) {
print "E: " . $e->getMessage() . " | " . $e->getTraceAsString() . "<br>";
print "E: " . $e->getPrevious()->getMessage() . " | " . $e->getPrevious()->getTraceAsString() . "<br>";
}
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');
try {
$el_er = new Element('123123');
} catch (HtmlBuilderExcpetion $e) {
print "E: " . $e->getMessage() . " | " . $e->getTraceAsString() . "<br>";
print "E: " . $e->getPrevious()->getMessage() . " | " . $e->getPrevious()->getTraceAsString() . "<br>";
}
print "Errors: <pre>" . print_r(Error::getMessages(), true) . "</pre>";
print "Warning: " . Support::printToString(Error::hasWarning()) . "<br>";
print "Error: " . Support::printToString(Error::hasError()) . "<br>";

View File

@@ -44,11 +44,9 @@ HTML;
print "<pre>" . htmlentities(StringReplace::replaceData(
$html_block,
[
'ID', 'CSS', '{CONTENT}'
],
[
'block-id', join(',', ['blue', 'red']),
'Some content here<br>with bla bla inside'
'ID' => 'block-id',
'CSS' => join(',', ['blue', 'red']),
'{CONTENT}' => 'Some content here<br>with bla bla inside'
]
)) . "</pre>";
@@ -63,14 +61,18 @@ print "Get: <pre>" . htmlentities(StringReplace::getElement('bar') ?: '') . '</p
print "Build element: <pre>" . htmlentities(StringReplace::buildElement(
'bar',
['ID}', '{CONTENT'],
['new-id', 'Test cow 日本語']
[
'ID}' => 'new-id',
'{CONTENT' => 'Test cow 日本語'
]
)) . '</pre>' ;
print "Build element as replace: <pre>" . htmlentities(StringReplace::buildElement(
'bar',
['ID}', '{CONTENT'],
['diff-id', 'Test cow 日本語. More text plus'],
['
ID}' => 'diff-id',
'{CONTENT' => 'Test cow 日本語. More text plus'
],
'rpl-1'
)) . '</pre>' ;

View File

@@ -83,7 +83,7 @@ $test_files = [
'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.html_build.replace.php' => 'Class Test: HTML BUILDER: STRING 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

@@ -6,11 +6,15 @@
* DESCRIPTION:
* html builder: array
* static build for array lists (not objects)
*
* Recommended to use the Object one or for speed the String Replace
*/
namespace CoreLibs\Template\HtmlBuilder;
use CoreLibs\Template\HtmlBuilder\General\Settings;
use CoreLibs\Template\HtmlBuilder\General\Error;
use CoreLibs\Template\HtmlBuilder\General\HtmlBuilderExcpetion;
class Block
{
@@ -22,6 +26,7 @@ class Block
* @param string $content
* @param array<string> $css,
* @param array<string,string> $options
* @throws HtmlBuilderExcpetion
*/
public static function cel(
string $tag,
@@ -30,6 +35,14 @@ class Block
array $css = [],
array $options = []
): array {
if (!preg_match("/^[A-Za-z]+$/", $tag)) {
Error::setError(
'201',
'invalid or empty tag',
['tag' => $tag]
);
throw new HtmlBuilderExcpetion('Invalid or empty tag');
}
return [
'tag' => $tag,
'id' => $id,

View File

@@ -14,6 +14,7 @@ namespace CoreLibs\Template\HtmlBuilder;
use CoreLibs\Template\HtmlBuilder\General\Settings;
use CoreLibs\Template\HtmlBuilder\General\Error;
use CoreLibs\Template\HtmlBuilder\General\HtmlBuilderExcpetion;
class Element
{
@@ -46,6 +47,7 @@ class Element
* eg: onClick => 'something();'
* id, css are skipped
* name only set on input/button
* @throws HtmlBuilderExcpetion
*/
public function __construct(
string $tag,
@@ -54,6 +56,28 @@ class Element
array $css = [],
array $options = []
) {
// exit if not valid tag
try {
$this->setTag($tag);
} catch (HtmlBuilderExcpetion $e) {
throw new HtmlBuilderExcpetion('Could not create Element', 0, $e);
}
$this->setId($id);
$this->setName($options['name'] ?? '');
$this->setContent($content);
$this->addCss(...$css);
$this->setOptions($options);
}
/**
* set tag
*
* @param string $tag
* @return void
* @throws HtmlBuilderExcpetion
*/
public function setTag(string $tag): void
{
// tag must be letters only
if (!preg_match("/^[A-Za-z]+$/", $tag)) {
Error::setError(
@@ -61,9 +85,29 @@ class Element
'invalid or empty tag',
['tag' => $tag]
);
return;
throw new HtmlBuilderExcpetion('Invalid or empty tag');
}
$this->tag = $tag;
}
/**
* get the tag name
*
* @return string HTML element tag
*/
public function getTag(): string
{
return $this->tag;
}
/**
* set the element id
*
* @param string $id
* @return void
*/
public function setId(string $id): void
{
// invalid id and name check too
// be strict: [a-zA-Z0-9], -, _
// cannot start with digit, two hyphens or a hyphen with a digit:
@@ -77,63 +121,11 @@ class Element
Error::setWarning(
'202',
'possible invalid id',
['id' => $id, 'tag' => $this->tag]
['id' => $id, 'tag' => $this->getTag()]
);
// TODO: shoud throw error
}
$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;
}
}
/**
@@ -147,13 +139,26 @@ class Element
}
/**
* get the tag name
* Set name for elements
* only for elements that need it (input/button/form)
*
* @return string HTML element tag
* @param string $name
* @return void
*/
public function getTag(): string
public function setName(string $name): void
{
return $this->tag;
if (
!empty($name) &&
!preg_match("/^[A-Za-z][\w-]*$/", $name)
) {
Error::setWarning(
'203',
'possible invalid name',
['name' => $name, 'id' => $this->getId(), 'tag' => $this->getTag()]
);
// TODO: shoud throw error
}
$this->name = $name;
}
/**
@@ -202,11 +207,21 @@ class Element
'Cannot set option with empty key',
['id' => $this->getId(), 'tag' => $this->getTag()]
);
// TODO: shoud throw error
continue;
}
// if data is null
if ($value === null) {
unset($this->options[$key]);
if (isset($this->options[$key])) {
unset($this->options[$key]);
} else {
Error::setError(
'210',
'Cannot set option with null value',
['id' => $this->getId(), 'tag' => $this->getTag()]
);
}
// TODO: shoud throw error
continue;
}
$this->options[$key] = $value;
@@ -243,6 +258,7 @@ class Element
*
* @param Element $sub One or many elements to add
* @return void
* @throws HtmlBuilderExcpetion
*/
public function addSub(Element ...$sub): void
{
@@ -252,10 +268,10 @@ class Element
if ($_sub == $this) {
Error::setError(
'100',
'Cannot assign Element, this would create a loop',
'Cannot assign Element to itself, this would create an infinite loop',
['id' => $this->getId(), 'tag' => $this->getTag()]
);
continue;
throw new HtmlBuilderExcpetion('Cannot assign Element to itself, this would create an infinite loop');
}
array_push($this->sub, $_sub);
}
@@ -319,7 +335,29 @@ class Element
*/
public function addCss(string ...$css): Element
{
$this->css = array_unique(array_merge($this->css, $css));
// should do check for empty/invalid css
$_set_css = [];
foreach ($css as $_css) {
if (empty($_css)) {
Error::setError(
'204',
'cannot have empty css string',
);
// TODO: shoud throw error
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]
);
// TODO: shoud throw error
}
$_set_css[] = $_css;
}
$this->css = array_unique(array_merge($this->css, $_set_css));
return $this;
}

View File

@@ -0,0 +1,21 @@
<?php
/**
* AUTOR: Clemens Schwaighofer
* CREATED: 2023/6/28
* DESCRIPTION:
* Exception class for the HtmlBuilder blocks
*/
declare(strict_types=1);
namespace CoreLibs\Template\HtmlBuilder\General;
/**
* Exception class for HtmlBuilder
*/
class HtmlBuilderExcpetion extends \Exception
{
}
// __END__

View File

@@ -12,12 +12,13 @@ declare(strict_types=1);
namespace CoreLibs\Template\HtmlBuilder;
use CoreLibs\Template\HtmlBuilder\General\Error;
use CoreLibs\Template\HtmlBuilder\General\HtmlBuilderExcpetion;
class StringReplace
{
/** @var array<string,string> */
private static array $elements = [];
/** @var array<string,array<string,string>> */
/** @var array<string,string> */
private static array $replace = [];
/**
@@ -28,16 +29,14 @@ class StringReplace
* 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
* @return void
* @throws HtmlBuilderExcpetion
*/
public static function loadElements(array ...$element): bool
public static function loadElements(array ...$element): void
{
$error = false;
foreach ($element as $el) {
$index = $el[0] ?? '';
if (empty($index)) {
$error = true;
Error::setError(
'310',
'Index cannot be an empty string',
@@ -45,9 +44,9 @@ class StringReplace
'element' => $index
]
);
throw new HtmlBuilderExcpetion('Index cannot be an empty string');
}
if (isset(self::$elements[$index])) {
$error = true;
Error::setError(
'311',
'Index already exists',
@@ -55,11 +54,11 @@ class StringReplace
'element' => $index
]
);
throw new HtmlBuilderExcpetion('Index already exists: ' . $index);
}
// content check?
self::$elements[$index] = $el[1];
}
return $error;
}
/**
@@ -80,7 +79,7 @@ class StringReplace
'element' => $index
]
);
return;
throw new HtmlBuilderExcpetion('Index does not exists: ' . $index);
}
// allow empty reset
self::$elements[$index] = $element;
@@ -91,13 +90,14 @@ class StringReplace
* if not found will return false
*
* @param string $index
* @return string|false
* @return string
* @throws HtmlBuilderExcpetion
*/
public static function getElement(string $index): string|bool
public static function getElement(string $index): string
{
if (!isset(self::$elements[$index])) {
Error::setError('321', 'Index not found in elements', ['element' => $index]);
return false;
throw new HtmlBuilderExcpetion('Index not found in elements array: ' . $index);
}
return self::$elements[$index];
}
@@ -120,12 +120,13 @@ class StringReplace
*
* @param string $index
* @return string
* @throws HtmlBuilderExcpetion
*/
public static function getReplaceBlock(string $index): string
{
if (!isset(self::$replace[$index])) {
Error::setError('331', 'Index not found in replace block', ['replace' => $index]);
return '';
throw new HtmlBuilderExcpetion('Index not found in replace block array: ' . $index);
}
return self::$replace[$index];
}
@@ -136,14 +137,12 @@ class StringReplace
* 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
* @param array<string,string> $replace array of text to search (key) and replace (value) for
* @return string|false
*/
public static function buildElement(
string $index,
array $replace,
array $content,
string $replace_index = ''
): string|bool {
if (self::getElement($index) === false) {
@@ -152,11 +151,19 @@ class StringReplace
if ($replace_index) {
self::setReplaceBlock(
$replace_index,
self::replaceData(self::$elements[$index], $replace, $content)
self::replaceData(
self::$elements[$index],
array_keys($replace),
array_values($replace)
)
);
return self::getReplaceBlock($replace_index);
} else {
return self::replaceData(self::$elements[$index], $replace, $content);
return self::replaceData(
self::$elements[$index],
array_keys($replace),
array_values($replace)
);
}
}
@@ -166,17 +173,14 @@ class StringReplace
* 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
* @param string $data
* @param array<string,string> $replace
* @return string
* @throws HtmlBuilderExcpetion
*/
public static function replaceData(string $data, array $replace, array $content): string|bool
public static function replaceData(string $data, array $replace): string
{
if (count($replace) != count($content)) {
Error::setError('340', 'Replace and content count differ');
return false;
}
$replace = array_keys($replace);
// all replace data must have {} around
array_walk($replace, function (&$entry) {
if (!str_starts_with($entry, '{')) {
@@ -185,9 +189,10 @@ class StringReplace
if (!str_ends_with($entry, '}')) {
$entry .= '}';
}
// do some validation?
});
// replace content
return str_replace($replace, $content, $data);
return str_replace($replace, array_values($replace), $data);
}
}

View File

@@ -80,8 +80,9 @@ return array(
'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\\HtmlBuilderExcpetion' => $baseDir . '/lib/CoreLibs/Template/HtmlBuilder/General/HtmlBuilderExcpetion.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\\HtmlBuilder\\StringReplace' => $baseDir . '/lib/CoreLibs/Template/HtmlBuilder/StringReplace.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

@@ -131,8 +131,9 @@ class ComposerStaticInit10fe8fe2ec4017b8644d2b64bcf398b9
'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\\HtmlBuilderExcpetion' => __DIR__ . '/../..' . '/lib/CoreLibs/Template/HtmlBuilder/General/HtmlBuilderExcpetion.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\\HtmlBuilder\\StringReplace' => __DIR__ . '/../..' . '/lib/CoreLibs/Template/HtmlBuilder/StringReplace.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',