-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractElement.php
More file actions
76 lines (67 loc) · 2.08 KB
/
AbstractElement.php
File metadata and controls
76 lines (67 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
declare(strict_types=1);
namespace PHPForge\Html\Base;
use PHPForge\{
Html\Attribute\Custom\HasAttributes,
Html\Attribute\Custom\HasPrefixCollection,
Html\Attribute\Custom\HasSuffixCollection,
Html\Attribute\Custom\HasTemplate,
Html\Attribute\HasClass,
Html\Attribute\HasData,
Html\Attribute\HasId,
Html\Attribute\HasLang,
Html\Attribute\HasStyle,
Html\Attribute\HasTitle,
Html\Attribute\Input\HasName,
Html\HtmlBuilder,
Html\Interop\RenderInterface,
Widget\Element
};
/**
* Provides a foundation for creating HTML elements with various attributes and content.
*/
abstract class AbstractElement extends Element implements RenderInterface
{
use HasAttributes;
use HasClass;
use HasData;
use HasId;
use HasLang;
use HasName;
use HasPrefixCollection;
use HasStyle;
use HasSuffixCollection;
use HasTemplate;
use HasTitle;
protected array $attributes = [];
/**
* This method is used to configure the widget with the provided default definitions.
*/
protected function loadDefaultDefinitions(): array
{
return [
'template()' => ['{prefix}\n{tag}\n{suffix}'],
];
}
/**
* Generate the HTML representation of the element.
*
* @param string $tagName The tag name of the element.
* @param string $content The content of the element.
* @param array $tokenValues The token values to be used in the template.
*
* @return string The HTML representation of the element.
*/
protected function buildElement(string $tagName, string $content = '', array $tokenValues = []): string
{
$attributes = $this->attributes;
$attributes['id'] ??= $this->id;
$tokenTemplateValues = [
'{prefix}' => $this->renderPrefixTag(),
'{tag}' => HtmlBuilder::create($tagName, $content, $attributes),
'{suffix}' => $this->renderSuffixTag(),
];
$tokenTemplateValues += $tokenValues;
return $this->renderTemplate($this->template, $tokenTemplateValues);
}
}