|
1 | 1 | <p align="center"> |
2 | | - <a href="https://github.com/yii-tools/template" target="_blank"> |
| 2 | + <a href="https://github.com/ui-awesome/html-helper" target="_blank"> |
3 | 3 | <img src="https://avatars.githubusercontent.com/u/121752654?s=200&v=4" height="100px"> |
4 | 4 | </a> |
5 | | - <h1 align="center">Template.</h1> |
| 5 | + <h1 align="center">UI Awesome HTML Helpers Code Generator for PHP.</h1> |
6 | 6 | <br> |
7 | 7 | </p> |
8 | 8 |
|
9 | 9 | <p align="center"> |
10 | | - <a href="https://github.com/yii-tools/template/actions/workflows/build.yml" target="_blank"> |
11 | | - <img src="https://github.com/yii-tools/template/actions/workflows/build.yml/badge.svg" alt="PHPUnit"> |
| 10 | + <a href="https://github.com/ui-awesome/html-helper/actions/workflows/build.yml" target="_blank"> |
| 11 | + <img src="https://github.com/ui-awesome/html-helper/actions/workflows/build.yml/badge.svg" alt="PHPUnit"> |
12 | 12 | </a> |
13 | | - <a href="https://codecov.io/gh/yii-tools/template" target="_blank"> |
14 | | - <img src="https://codecov.io/gh/yii-tools/template/branch/main/graph/badge.svg?token=MF0XUGVLYC" alt="Codecov"> |
| 13 | + <a href="https://codecov.io/gh/ui-awesome/html-helper" target="_blank"> |
| 14 | + <img src="https://codecov.io/gh/ui-awesome/html-helper/graph/badge.svg?token=6J8OECQN6I" alt="Codecov"> |
15 | 15 | </a> |
16 | | - <a href="https://dashboard.stryker-mutator.io/reports/github.com/yii-tools/template/main" target="_blank"> |
17 | | - <img src="https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyii2-extensions%2Fasset-bootstrap5%2Fmain" alt="Infection"> |
| 16 | + <a href="https://dashboard.stryker-mutator.io/reports/github.com/ui-awesome/html-helper/main" target="_blank"> |
| 17 | + <img src="https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fui-awesome%2Fhtml-helper%2Fmain" alt="Infection"> |
18 | 18 | </a> |
19 | | - <a href="https://github.com/yii-tools/template/actions/workflows/static.yml" target="_blank"> |
20 | | - <img src="https://github.com/yii-tools/template/actions/workflows/static.yml/badge.svg" alt="Psalm"> |
| 19 | + <a href="https://github.com/ui-awesome/html-helper/actions/workflows/static.yml" target="_blank"> |
| 20 | + <img src="https://github.com/ui-awesome/html-helper/actions/workflows/static.yml/badge.svg" alt="Psalm"> |
21 | 21 | </a> |
22 | | - <a href="https://shepherd.dev/github/yii-tools/template" target="_blank"> |
23 | | - <img src="https://shepherd.dev/github/yii-tools/template/coverage.svg" alt="Psalm Coverage"> |
| 22 | + <a href="https://shepherd.dev/github/ui-awesome/html-helper" target="_blank"> |
| 23 | + <img src="https://shepherd.dev/github/ui-awesome/html-helper/coverage.svg" alt="Psalm Coverage"> |
| 24 | + </a> |
| 25 | + <a href="https://github.styleci.io/repos/767410135?branch=main"> |
| 26 | + <img src="https://github.styleci.io/repos/767410135/shield?branch=main" alt="Style ci"> |
24 | 27 | </a> |
25 | | - <a href="https://github.styleci.io/repos/494495136?branch=main" target="_blank"> |
26 | | - <img src="https://github.styleci.io/repos/494495136/shield?branch=main" alt="Style ci"> |
27 | | - </a> |
28 | 28 | </p> |
29 | 29 |
|
| 30 | +HTML Helper is a PHP library that simplifies the creation of HTML elements. It provides a set of classes to generate |
| 31 | +HTML attributes, encode content, sanitize HTML content, and more. |
| 32 | + |
| 33 | + |
30 | 34 | ## Installation |
31 | 35 |
|
32 | 36 | The preferred way to install this extension is through [composer](https://getcomposer.org/download/). |
33 | 37 |
|
34 | 38 | Either run |
35 | 39 |
|
36 | 40 | ```shell |
37 | | -composer require --prefer-dist package |
| 41 | +composer require --prefer-dist ui-awesome/html-helper:^0.1 |
38 | 42 | ``` |
39 | 43 |
|
40 | 44 | or add |
41 | 45 |
|
42 | 46 | ```json |
43 | | -"package": "version" |
| 47 | +"ui-awesome/html-helper": "^0.1" |
44 | 48 | ``` |
45 | 49 |
|
46 | | -to the require-dev section of your `composer.json` file. |
| 50 | +to the require section of your `composer.json` file. |
47 | 51 |
|
48 | 52 | ## Usage |
49 | 53 |
|
50 | | -[Check the documentation docs](docs/README.md) to learn about usage. |
| 54 | +### Add CSS classes |
| 55 | + |
| 56 | +The `CssClasses::class` helper can be used to add CSS classes to an HTML element. |
| 57 | + |
| 58 | +The method accepts three parameters: |
| 59 | + |
| 60 | +- `attributes:` (array): The HTML attributes of the element. |
| 61 | +- `classes:` (array|string): The CSS classes to add. |
| 62 | +- `overwrite:` (bool): Whether to overwrite the `class` attribute or not. For default, it is `false`. |
| 63 | + |
| 64 | +```php |
| 65 | +<?php |
| 66 | + |
| 67 | +declare(strict_types=1); |
| 68 | + |
| 69 | +use UIAwesome\Html\Helper\CssClasses; |
| 70 | + |
| 71 | +private array $attributes = []; |
| 72 | + |
| 73 | +$classes = CssClasses::add($this->attributes, ['btn', 'btn-primary', 'btn-lg']); |
| 74 | +``` |
| 75 | + |
| 76 | +Overwriting the `class` attribute: |
| 77 | + |
| 78 | +```php |
| 79 | +<?php |
| 80 | + |
| 81 | +declare(strict_types=1); |
| 82 | + |
| 83 | +use UIAwesome\Html\Helper\CssClasses; |
| 84 | + |
| 85 | +private array $attributes = ['class' => 'btn']; |
| 86 | + |
| 87 | +$classes = CssClasses::add($this->attributes, ['btn-primary', 'btn-lg'], true); |
| 88 | +``` |
| 89 | + |
| 90 | +### Convert regular expression to pattern |
| 91 | + |
| 92 | +The `Utils::class` helper can be used to normalize a regular expression. |
| 93 | + |
| 94 | +The method accepts one parameter: |
| 95 | + |
| 96 | +- `regexp:` (string): The pattern to normalize. |
| 97 | +- `delimiter:` (string): The delimiter to use. For default, it is `null`. |
| 98 | + |
| 99 | +```php |
| 100 | +<?php |
| 101 | + |
| 102 | +declare(strict_types=1); |
| 103 | + |
| 104 | +use UIAwesome\Html\Helper\Utils; |
| 105 | + |
| 106 | +$pattern = Utils::convertToPattern('/([a-z0-9-]+)/im'); // return: `([a-z0-9-]+)` |
| 107 | +``` |
| 108 | + |
| 109 | +### Encode content |
| 110 | + |
| 111 | +The `Encode::class` helper can be used to encode HTML content. |
| 112 | + |
| 113 | +The method accepts tree parameters: |
| 114 | + |
| 115 | +- `content:` (string): The content to encode. |
| 116 | +- `doubleEncode:` (bool): Whether to double encode the content or not. For default, it is `true`. |
| 117 | +- `charset:` (string): The charset to use. For default, it is `UTF-8`. |
| 118 | + |
| 119 | +```php |
| 120 | +<?php |
| 121 | + |
| 122 | +declare(strict_types=1); |
| 123 | + |
| 124 | +use UIAwesome\Html\Helper\Encode; |
| 125 | + |
| 126 | +$content = Encode::html('<script>alert("Hello, World!")</script>'); |
| 127 | +``` |
| 128 | + |
| 129 | +### Encode value |
| 130 | + |
| 131 | +The `Encode::class` helper can be used to encode HTML value. |
| 132 | + |
| 133 | +The method accepts tree parameters: |
| 134 | + |
| 135 | +- `value:` (string): The value to encode. |
| 136 | +- `doubleEncode:` (bool): Whether to double encode the value or not. For default, it is `true`. |
| 137 | +- `charset:` (string): The charset to use. For default, it is `UTF-8`. |
| 138 | + |
| 139 | +```php |
| 140 | +<?php |
| 141 | + |
| 142 | +declare(strict_types=1); |
| 143 | + |
| 144 | +use UIAwesome\Html\Helper\Encode; |
| 145 | + |
| 146 | +$value = Encode::value('<script>alert("Hello, World!")</script>'); |
| 147 | +``` |
| 148 | + |
| 149 | +### Get short class name |
| 150 | + |
| 151 | +The `Utils::class` helper can be used to get the short class name. |
| 152 | + |
| 153 | +The method accepts one parameter: |
| 154 | + |
| 155 | +- `class:` (string): The class name to get the short name. |
| 156 | +- `suffix:` (string): Whether to append the `::class` suffix to the class name. |
| 157 | + For default, it is `true`. If it is `false`, the method will return the short name without the `::class` suffix. |
| 158 | +- `lowercase:` (bool): Whether to convert the class name to lowercase or not. |
| 159 | + For default, it is `false`. |
| 160 | + |
| 161 | +```php |
| 162 | +<?php |
| 163 | + |
| 164 | +declare(strict_types=1); |
| 165 | + |
| 166 | +use UIAwesome\Html\Helper\Utils; |
| 167 | + |
| 168 | +$shortName = Utils::getShortClassName('UIAwesome\Html\Helper\Utils'); // return: `Utils::class` |
| 169 | +``` |
| 170 | + |
| 171 | +### Generate arrayable name |
| 172 | + |
| 173 | +The `ArrayableName::class` helper can be used to generate an arrayable name. |
| 174 | + |
| 175 | +The method accepts one parameter: |
| 176 | + |
| 177 | +- `name:` (string): The name to generate. |
| 178 | + |
| 179 | +```php |
| 180 | +<?php |
| 181 | + |
| 182 | +declare(strict_types=1); |
| 183 | + |
| 184 | +use UIAwesome\Html\Helper\Utils; |
| 185 | + |
| 186 | +$name = Utils::generateArrayableName('name'); |
| 187 | +``` |
| 188 | + |
| 189 | +### Generate input id |
| 190 | + |
| 191 | +The `Utils::class` helper can be used to generate an input id. |
| 192 | + |
| 193 | +The method accepts tree parameters: |
| 194 | + |
| 195 | +- `fieldModel:` (string): The name of the field model. |
| 196 | +- `property:` (string): The name of the property. |
| 197 | +- `charset:` (string): The charset to use. For default, it is `UTF-8`. |
| 198 | + |
| 199 | +```php |
| 200 | +<?php |
| 201 | + |
| 202 | +declare(strict_types=1); |
| 203 | + |
| 204 | +use UIAwesome\Html\Helper\Utils; |
| 205 | + |
| 206 | +$id = Utils::generateInputId('user', 'name'); |
| 207 | +``` |
| 208 | + |
| 209 | +### Generate input name |
| 210 | + |
| 211 | +The `Utils::class` helper can be used to generate an input name. |
| 212 | + |
| 213 | +The method accepts tree parameters: |
| 214 | + |
| 215 | +- `fieldModel:` (string): The name of the field model. |
| 216 | +- `property:` (string): The name of the property. |
| 217 | +- `arrayable:` (bool): Whether the name is arrayable or not. For default, it is `false`. |
| 218 | + |
| 219 | +```php |
| 220 | +<?php |
| 221 | + |
| 222 | +declare(strict_types=1); |
| 223 | + |
| 224 | +use UIAwesome\Html\Helper\Utils; |
| 225 | + |
| 226 | +$name = Utils::generateInputName('user', 'name'); |
| 227 | +``` |
| 228 | + |
| 229 | +### Sanitize content |
| 230 | + |
| 231 | +The `Sanitize::class` helper can be used to sanitize HTML content. |
| 232 | + |
| 233 | +The method accepts one parameter: |
| 234 | + |
| 235 | +- `content:` (...string|RenderInterface): The content to sanitize. It can be a string or an object that implements the |
| 236 | + `RenderInterface::class`. |
| 237 | + |
| 238 | +```php |
| 239 | +<?php |
| 240 | + |
| 241 | +declare(strict_types=1); |
| 242 | + |
| 243 | +use UIAwesome\Html\Helper\Sanitize; |
| 244 | + |
| 245 | +$content = Sanitize::html('<script>alert("Hello, World!")</script>'); |
| 246 | +``` |
| 247 | + |
| 248 | +### Render HTML attributes |
| 249 | + |
| 250 | +The `Attributes::class` helper can be used to `render` HTML attributes in a programmatic way. |
| 251 | + |
| 252 | +```php |
| 253 | +<?php |
| 254 | + |
| 255 | +declare(strict_types=1); |
| 256 | + |
| 257 | +use UIAwesome\Html\Helper\Attributes; |
| 258 | + |
| 259 | +$attributes = Attributes::render( |
| 260 | + [ |
| 261 | + 'class' => 'btn btn-primary', |
| 262 | + 'id' => 'submit-button', |
| 263 | + 'disabled' => true, |
| 264 | + ] |
| 265 | +); |
| 266 | +``` |
| 267 | + |
| 268 | +### Validate value in list |
| 269 | + |
| 270 | +The `Validator::class` helper can be used to validate a value in a list. |
| 271 | + |
| 272 | +The method accepts tree parameters: |
| 273 | + |
| 274 | +- `value:` (mixed): The value to validate. |
| 275 | +- `exceptionMessage:` (string): The exception message to throw if the value is not in the list. |
| 276 | +- `list:` (...string): The list to validate the value. |
| 277 | + |
| 278 | +```php |
| 279 | +<?php |
| 280 | + |
| 281 | +declare(strict_types=1); |
| 282 | + |
| 283 | +use UIAwesome\Html\Helper\Validator; |
| 284 | + |
| 285 | +$value = Validator::inList('php', 'The value is not in the list.', 'php', 'javascript', 'typescript'); |
| 286 | +``` |
| 287 | + |
| 288 | +### Validate value iterable |
| 289 | + |
| 290 | +The `Validator::class` helper can be used to validate an iterable value. If the value is not iterable or `null`, the |
| 291 | +method will throw an `InvalidArgumentException`. |
| 292 | + |
| 293 | +The method accepts one parameter: |
| 294 | + |
| 295 | +- `value:` (mixed): The value to validate. |
| 296 | + |
| 297 | +```php |
| 298 | +<?php |
| 299 | + |
| 300 | +declare(strict_types=1); |
| 301 | + |
| 302 | +use UIAwesome\Html\Helper\Validator; |
| 303 | + |
| 304 | +$value = Validator::iterable([1, 2, 3]); |
| 305 | +``` |
| 306 | + |
| 307 | +### Validate value numeric |
| 308 | + |
| 309 | +The `Validator::class` helper can be used to validate a numeric value. If the value is not numeric or `null`, the method |
| 310 | +will throw an `InvalidArgumentException`. |
| 311 | + |
| 312 | +The method accepts one parameter: |
| 313 | + |
| 314 | +- `value:` (mixed): The value to validate. |
| 315 | + |
| 316 | +```php |
| 317 | +<?php |
| 318 | + |
| 319 | +declare(strict_types=1); |
| 320 | + |
| 321 | +use UIAwesome\Html\Helper\Validator; |
| 322 | + |
| 323 | +$value = Validator::numeric(1); |
| 324 | +``` |
| 325 | + |
| 326 | +### Validate value scalar |
| 327 | + |
| 328 | +The `Validator::class` helper can be used to validate a scalar value. If the value is not scalar or `null`, the method |
| 329 | +will throw an `InvalidArgumentException`. |
| 330 | + |
| 331 | +The method accepts one parameter: |
| 332 | + |
| 333 | +- `value:` (...mixed): The value to validate. |
| 334 | + |
| 335 | +```php |
| 336 | +<?php |
| 337 | + |
| 338 | +declare(strict_types=1); |
| 339 | + |
| 340 | +use UIAwesome\Html\Helper\Validator; |
| 341 | + |
| 342 | +$value = Validator::scalar('Hello, World!'); |
| 343 | +``` |
| 344 | + |
| 345 | +### Validate value string |
| 346 | + |
| 347 | +The `Validator::class` helper can be used to validate a string value. If the value is not a string or `null`, the method |
| 348 | +will throw an `InvalidArgumentException`. |
| 349 | + |
| 350 | +The method accepts one parameter: |
| 351 | + |
| 352 | +- `value:` (mixed): The value to validate. |
| 353 | + |
| 354 | +```php |
| 355 | +<?php |
| 356 | + |
| 357 | +declare(strict_types=1); |
| 358 | + |
| 359 | +use UIAwesome\Html\Helper\Validator; |
| 360 | + |
| 361 | +$value = Validator::string('Hello, World!'); |
| 362 | +``` |
51 | 363 |
|
52 | 364 | ## Testing |
53 | 365 |
|
|
0 commit comments