php-forge / html-helper
Awesome HTML Helpers Code Generator for PHP.
Installs: 2 156
Dependents: 2
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- ext-mbstring: *
- php-forge/html-interop: ^0.3
- voku/anti-xss: ^4.1
Requires (Dev)
- maglnet/composer-require-checker: ^4.7
- php-forge/support: ^0.1
- phpunit/phpunit: ^10.5
- roave/infection-static-analysis-plugin: ^1.34
- symplify/easy-coding-standard: ^12.1
- vimeo/psalm: ^5.19
This package is auto-updated.
Last update: 2024-11-03 13:02:16 UTC
README
Awesome HTML Helpers Code Generator for PHP.
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.
Installation
The preferred way to install this extension is through composer.
Either run
composer require --prefer-dist php-forge/html-helper:^0.1
or add
"php-forge/html-helper": "^0.1"
to the require section of your composer.json
file.
Usage
Add CSS classes
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 PHPForge\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 PHPForge\Html\Helper\CssClasses; private array $attributes = ['class' => 'btn']; $classes = CssClasses::add($this->attributes, ['btn-primary', 'btn-lg'], true);
Convert regular expression to pattern
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 PHPForge\Html\Helper\Utils; $pattern = Utils::convertToPattern('/([a-z0-9-]+)/im'); // return: `([a-z0-9-]+)`
Encode content
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 PHPForge\Html\Helper\Encode; $content = Encode::html('<script>alert("Hello, World!")</script>');
Encode value
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 PHPForge\Html\Helper\Encode; $value = Encode::value('<script>alert("Hello, World!")</script>');
Get short class name
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 PHPForge\Html\Helper\Utils; $shortName = Utils::getShortClassName('PHPForge\Html\Helper\Utils'); // return: `Utils::class`
Generate arrayable name
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 PHPForge\Html\Helper\Utils; $name = Utils::generateArrayableName('name');
Generate input id
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 PHPForge\Html\Helper\Utils; $id = Utils::generateInputId('user', 'name');
Generate input 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 PHPForge\Html\Helper\Utils; $name = Utils::generateInputName('user', 'name');
Sanitize content
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 PHPForge\Html\Helper\Sanitize; $content = Sanitize::html('<script>alert("Hello, World!")</script>');
Render HTML attributes
The Attributes::class
helper can be used to render
HTML attributes in a programmatic way.
<?php declare(strict_types=1); use PHPForge\Html\Helper\Attributes; $attributes = Attributes::render( [ 'class' => 'btn btn-primary', 'id' => 'submit-button', 'disabled' => true, ] );
Render Template
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 PHPForge\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.
Validate value in list
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 PHPForge\Html\Helper\Validator; $value = Validator::inList('php', 'The value is not in the list.', 'php', 'javascript', 'typescript');
Validate value iterable
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 PHPForge\Html\Helper\Validator; $value = Validator::iterable([1, 2, 3]);
Validate value numeric
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 PHPForge\Html\Helper\Validator; $value = Validator::numeric(1);
Validate value scalar
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 PHPForge\Html\Helper\Validator; $value = Validator::scalar('Hello, World!');
Validate value string
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 PHPForge\Html\Helper\Validator; $value = Validator::string('Hello, World!');
Testing
Check the documentation testing to learn about testing.
Support versions
License
The MIT License (MIT). Please see License File for more information.