php-forge/html-helper

PHP HTML 辅助代码生成器,功能强大。

0.2.1 2024-03-02 10:08 UTC

This package is auto-updated.

Last update: 2024-09-03 12:35:55 UTC


README

PHP HTML 辅助代码生成器,功能强大。


PHPUnit Codecov Infection Psalm Psalm Coverage Style ci

HTML Helper 是一个 PHP 库,简化了 HTML 元素的创建。它提供了一系列类来生成 HTML 属性、编码内容、清理 HTML 内容等。

安装

安装此扩展的首选方式是通过 composer

运行

composer require --prefer-dist php-forge/html-helper:^0.1

或添加

"php-forge/html-helper": "^0.1"

到您的 composer.json 文件的 require 部分。

使用方法

添加 CSS 类

可以使用 CssClasses::class 辅助类将 CSS 类添加到 HTML 元素。

该方法接受三个参数

  • attributes: (array): 元素的 HTML 属性。
  • classes: (array|string): 要添加的 CSS 类。
  • overwrite: (bool): 是否覆盖 class 属性。默认为 false
<?php

declare(strict_types=1);

use PHPForge\Html\Helper\CssClasses;

private array $attributes = [];

$classes = CssClasses::add($this->attributes, ['btn', 'btn-primary', 'btn-lg']);

覆盖 class 属性

<?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);

将正则表达式转换为模式

可以使用 Utils::class 辅助类来标准化正则表达式。

该方法接受一个参数

  • regexp: (string): 要标准化的模式。
  • delimiter: (string): 要使用的分隔符。默认为 null
<?php

declare(strict_types=1);

use PHPForge\Html\Helper\Utils;

$pattern = Utils::convertToPattern('/([a-z0-9-]+)/im'); // return: `([a-z0-9-]+)`

编码内容

可以使用 Encode::class 辅助类来编码 HTML 内容。

该方法接受三个参数

  • content: (string): 要编码的内容。
  • doubleEncode: (bool): 是否双重编码内容。默认为 true
  • charset: (string): 要使用的字符集。默认为 UTF-8
<?php

declare(strict_types=1);

use PHPForge\Html\Helper\Encode;

$content = Encode::html('<script>alert("Hello, World!")</script>');

编码值

可以使用 Encode::class 辅助类来编码 HTML 值。

该方法接受三个参数

  • value: (string): 要编码的值。
  • doubleEncode: (bool): 是否双重编码值。默认为 true
  • charset: (string): 要使用的字符集。默认为 UTF-8
<?php

declare(strict_types=1);

use PHPForge\Html\Helper\Encode;

$value = Encode::value('<script>alert("Hello, World!")</script>');

获取短类名

可以使用 Utils::class 辅助类来获取短类名。

该方法接受一个参数

  • class: (string): 要获取短名的类名。
  • suffix: (string): 是否追加 ::class 后缀。默认为 true。如果为 false,方法将返回不带 ::class 后缀的短名。
  • lowercase: (bool): 是否将类名转换为小写。默认为 false
<?php

declare(strict_types=1);

use PHPForge\Html\Helper\Utils;

$shortName = Utils::getShortClassName('PHPForge\Html\Helper\Utils'); // return: `Utils::class`

生成可数数组名

可以使用 ArrayableName::class 辅助类来生成可数数组名。

该方法接受一个参数

  • name: (string): 要生成的名称。
<?php

declare(strict_types=1);

use PHPForge\Html\Helper\Utils;

$name = Utils::generateArrayableName('name');

生成输入 ID

可以使用 Utils::class 辅助类来生成输入 ID。

该方法接受三个参数

  • fieldModel: (string): 字段模型的名称。
  • property: (string): 属性的名称。
  • charset: (string): 要使用的字符集。默认为 UTF-8
<?php

declare(strict_types=1);

use PHPForge\Html\Helper\Utils;

$id = Utils::generateInputId('user', 'name');

生成输入名称

可以使用 Utils::class 辅助类来生成输入名称。

该方法接受三个参数

  • fieldModel: (string): 字段模型的名称。
  • property: (string): 属性的名称。
  • arrayable: (bool): 名称是否可数。默认为 false
<?php

declare(strict_types=1);

use PHPForge\Html\Helper\Utils;

$name = Utils::generateInputName('user', 'name');

清理内容

可以使用 Sanitize::class 辅助类来清理 HTML 内容。

该方法接受一个参数

  • content: (...string|RenderInterface): 要清理的内容。可以是字符串或实现了 RenderInterface::class 的对象。
<?php

declare(strict_types=1);

use PHPForge\Html\Helper\Sanitize;

$content = Sanitize::html('<script>alert("Hello, World!")</script>');

渲染 HTML 属性

Attributes::class 辅助类可用于以编程方式 渲染 HTML 属性。

<?php

declare(strict_types=1);

use PHPForge\Html\Helper\Attributes;

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

渲染模板

Template::class 辅助类可用于渲染模板。

该方法接受两个参数

  • template: (字符串): 要渲染的模板。
  • tokenValues: (数组): 要在模板中替换的占位符值。
<?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 是换行符,用于在模板中分隔占位符。

列表中验证值

Validator::class 辅助类可用于验证列表中的值。

该方法接受三个参数

  • value: (混合类型): 要验证的值。
  • exceptionMessage: (字符串): 如果值不在列表中则抛出的异常信息。
  • list: (...字符串): 用于验证值的列表。
<?php

declare(strict_types=1);

use PHPForge\Html\Helper\Validator;

$value = Validator::inList('php', 'The value is not in the list.', 'php', 'javascript', 'typescript');

验证可迭代值

Validator::class 辅助类可用于验证可迭代值。如果值不可迭代或为 null,该方法将抛出 InvalidArgumentException 异常。

该方法接受一个参数

  • value: (混合类型): 要验证的值。
<?php

declare(strict_types=1);

use PHPForge\Html\Helper\Validator;

$value = Validator::iterable([1, 2, 3]);

验证数值

Validator::class 辅助类可用于验证数值。如果值不是数值或为 null,该方法将抛出 InvalidArgumentException 异常。

该方法接受一个参数

  • value: (混合类型): 要验证的值。
<?php

declare(strict_types=1);

use PHPForge\Html\Helper\Validator;

$value = Validator::numeric(1);

验证标量值

Validator::class 辅助类可用于验证标量值。如果值不是标量或为 null,该方法将抛出 InvalidArgumentException 异常。

该方法接受一个参数

  • value: (...混合类型): 要验证的值。
<?php

declare(strict_types=1);

use PHPForge\Html\Helper\Validator;

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

验证字符串值

Validator::class 辅助类可用于验证字符串值。如果值不是字符串或为 null,该方法将抛出 InvalidArgumentException 异常。

该方法接受一个参数

  • value: (混合类型): 要验证的值。
<?php

declare(strict_types=1);

use PHPForge\Html\Helper\Validator;

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

测试

检查文档测试部分 了解有关测试的信息。

支持版本

PHP81

许可

MIT 许可协议 (MIT)。请参阅 许可文件 获取更多信息。

我们的社交媒体

Twitter