HTML Helper is a PHP library that simplifies the creation of HTML elements. It provides a set of classes to generate HTML attributes, encode content, sanitize HTML content, and more.
The preferred way to install this extension is through composer.
Either run
composer require --prefer-dist ui-awesome/html-helper:^0.2
or add
"ui-awesome/html-helper": "^0.2"
to the require section of your composer.json
file.
The CssClasses::class
helper can be used to add CSS classes to an HTML element.
The method accepts three parameters:
attributes:
(array): The HTML attributes of the element.classes:
(array|string): The CSS classes to add.overwrite:
(bool): Whether to overwrite theclass
attribute or not. For default, it isfalse
.
<?php
declare(strict_types=1);
use UIAwesome\Html\Helper\CssClasses;
private array $attributes = [];
$classes = CssClasses::add($this->attributes, ['btn', 'btn-primary', 'btn-lg']);
Overwriting the class
attribute:
<?php
declare(strict_types=1);
use UIAwesome\Html\Helper\CssClasses;
private array $attributes = ['class' => 'btn'];
$classes = CssClasses::add($this->attributes, ['btn-primary', 'btn-lg'], true);
The Utils::class
helper can be used to normalize a regular expression.
The method accepts one parameter:
regexp:
(string): The pattern to normalize.delimiter:
(string): The delimiter to use. For default, it isnull
.
<?php
declare(strict_types=1);
use UIAwesome\Html\Helper\Utils;
$pattern = Utils::convertToPattern('/([a-z0-9-]+)/im'); // return: `([a-z0-9-]+)`
The Encode::class
helper can be used to encode HTML content.
The method accepts tree parameters:
content:
(string): The content to encode.doubleEncode:
(bool): Whether to double encode the content or not. For default, it istrue
.charset:
(string): The charset to use. For default, it isUTF-8
.
<?php
declare(strict_types=1);
use UIAwesome\Html\Helper\Encode;
$content = Encode::html('<script>alert("Hello, World!")</script>');
The Encode::class
helper can be used to encode HTML value.
The method accepts tree parameters:
value:
(string): The value to encode.doubleEncode:
(bool): Whether to double encode the value or not. For default, it istrue
.charset:
(string): The charset to use. For default, it isUTF-8
.
<?php
declare(strict_types=1);
use UIAwesome\Html\Helper\Encode;
$value = Encode::value('<script>alert("Hello, World!")</script>');
The Utils::class
helper can be used to get the short class name.
The method accepts one parameter:
class:
(string): The class name to get the short name.suffix:
(string): Whether to append the::class
suffix to the class name. For default, it istrue
. If it isfalse
, the method will return the short name without the::class
suffix.lowercase:
(bool): Whether to convert the class name to lowercase or not. For default, it isfalse
.
<?php
declare(strict_types=1);
use UIAwesome\Html\Helper\Utils;
$shortName = Utils::getShortClassName('UIAwesome\Html\Helper\Utils'); // return: `Utils::class`
The ArrayableName::class
helper can be used to generate an arrayable name.
The method accepts one parameter:
name:
(string): The name to generate.
<?php
declare(strict_types=1);
use UIAwesome\Html\Helper\Utils;
$name = Utils::generateArrayableName('name');
The Utils::class
helper can be used to generate an input id.
The method accepts tree parameters:
fieldModel:
(string): The name of the field model.property:
(string): The name of the property.charset:
(string): The charset to use. For default, it isUTF-8
.
<?php
declare(strict_types=1);
use UIAwesome\Html\Helper\Utils;
$id = Utils::generateInputId('user', 'name');
The Utils::class
helper can be used to generate an input name.
The method accepts tree parameters:
fieldModel:
(string): The name of the field model.property:
(string): The name of the property.arrayable:
(bool): Whether the name is arrayable or not. For default, it isfalse
.
<?php
declare(strict_types=1);
use UIAwesome\Html\Helper\Utils;
$name = Utils::generateInputName('user', 'name');
The Sanitize::class
helper can be used to sanitize HTML content.
The method accepts one parameter:
content:
(...string|RenderInterface): The content to sanitize. It can be a string or an object that implements theRenderInterface::class
.
<?php
declare(strict_types=1);
use UIAwesome\Html\Helper\Sanitize;
$content = Sanitize::html('<script>alert("Hello, World!")</script>');
The Attributes::class
helper can be used to render
HTML attributes in a programmatic way.
<?php
declare(strict_types=1);
use UIAwesome\Html\Helper\Attributes;
$attributes = Attributes::render(
[
'class' => 'btn btn-primary',
'id' => 'submit-button',
'disabled' => true,
]
);
The CssClass::class
helper can be used to render the class
attribute.
The method accepts one parameter:
class:
(string): The class to render.baseClass:
(string): The base class to use.inList:
(array): The list of classes to validate.
<?php
declare(strict_types=1);
use UIAwesome\Html\Helper\CssClass;
$class = CssClass::render(
'yellow',
'p-4 mb-4 text-sm text-%1$s-800 rounded-lg bg-%1$s-50 dark:bg-gray-800 dark:text-%1$s-400',
['blue', 'gray', 'green', 'red', 'yellow'],
);
The Template::class
helper can be used to render a template.
The method accepts two parameters:
template:
(string): The template to render.tokenValues:
(array): The token values to replace in the template.
<?php
declare(strict_types=1);
use UIAwesome\Html\Helper\Template;
$template = '{{prefix}}\n{{tag}}\n{{suffix}}';
$tokenValues = [
'{{prefix}}' => 'prefix',
'{{tag}}' => '<div>content</div>',
'{{suffix}}' => 'suffix',
];
$content = Template::render($template, $tokenValues);
\n
is a new line character, and it is used to separate the tokens in the template.
The Validator::class
helper can be used to validate a value in a list.
The method accepts tree parameters:
value:
(mixed): The value to validate.exceptionMessage:
(string): The exception message to throw if the value is not in the list.list:
(...string): The list to validate the value.
<?php
declare(strict_types=1);
use UIAwesome\Html\Helper\Validator;
$value = Validator::inList('php', 'The value is not in the list.', 'php', 'javascript', 'typescript');
The Validator::class
helper can be used to validate an iterable value. If the value is not iterable or null
, the
method will throw an InvalidArgumentException
.
The method accepts one parameter:
value:
(mixed): The value to validate.
<?php
declare(strict_types=1);
use UIAwesome\Html\Helper\Validator;
$value = Validator::iterable([1, 2, 3]);
The Validator::class
helper can be used to validate a numeric value. If the value is not numeric or null
, the method
will throw an InvalidArgumentException
.
The method accepts one parameter:
value:
(mixed): The value to validate.
<?php
declare(strict_types=1);
use UIAwesome\Html\Helper\Validator;
$value = Validator::numeric(1);
The Validator::class
helper can be used to validate a scalar value. If the value is not scalar or null
, the method
will throw an InvalidArgumentException
.
The method accepts one parameter:
value:
(...mixed): The value to validate.
<?php
declare(strict_types=1);
use UIAwesome\Html\Helper\Validator;
$value = Validator::scalar('Hello, World!');
The Validator::class
helper can be used to validate a string value. If the value is not a string or null
, the method
will throw an InvalidArgumentException
.
The method accepts one parameter:
value:
(mixed): The value to validate.
<?php
declare(strict_types=1);
use UIAwesome\Html\Helper\Validator;
$value = Validator::string('Hello, World!');
Check the documentation testing to learn about testing.
The MIT License (MIT). Please see License File for more information.