Skip to content

UI Awesome HTML Helper Code Generator for PHP.

License

Notifications You must be signed in to change notification settings

ui-awesome/html-helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

UI Awesome HTML Helpers Code Generator for PHP.


PHPUnit Codecov Infection Psalm Psalm Coverage Style ci

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 ui-awesome/html-helper:^0.2

or add

"ui-awesome/html-helper": "^0.2"

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 the class attribute or not. For default, it is false.
<?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);

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 is null.
<?php

declare(strict_types=1);

use UIAwesome\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 is true.
  • charset: (string): The charset to use. For default, it is UTF-8.
<?php

declare(strict_types=1);

use UIAwesome\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 is true.
  • charset: (string): The charset to use. For default, it is UTF-8.
<?php

declare(strict_types=1);

use UIAwesome\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 is true. If it is false, 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 is false.
<?php

declare(strict_types=1);

use UIAwesome\Html\Helper\Utils;

$shortName = Utils::getShortClassName('UIAwesome\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 UIAwesome\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 is UTF-8.
<?php

declare(strict_types=1);

use UIAwesome\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 is false.
<?php

declare(strict_types=1);

use UIAwesome\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 the RenderInterface::class.
<?php

declare(strict_types=1);

use UIAwesome\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 UIAwesome\Html\Helper\Attributes;

$attributes = Attributes::render(
    [
        'class' => 'btn btn-primary',
        'id' => 'submit-button',
        'disabled' => true,
    ]
);

Render HMTL class attribute

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'],
);

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 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.

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 UIAwesome\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 UIAwesome\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 UIAwesome\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 UIAwesome\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 UIAwesome\Html\Helper\Validator;

$value = Validator::string('Hello, World!');

Testing

Check the documentation testing to learn about testing.

Support versions

PHP81

License

The MIT License (MIT). Please see License File for more information.

Our social networks

Twitter