Repository navigation
Expand file tree
/
Copy pathElement.php
More file actions
104 lines (89 loc) · 2.03 KB
/
Copy pathElement.php
File metadata and controls
104 lines (89 loc) · 2.03 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
namespace HTML;
use HTML\Traits\HasAttributes;
use HTML\Traits\HasContent;
use HTML\Traits\HasChildren;
use HTML\Traits\HasClosingTag;
class Element
{
use HasAttributes,
HasContent,
HasChildren,
HasClosingTag;
/**
* Tag name.
*
* @var string
*/
private $tag;
/**
* Constructor.
*
* @param string $tag The Element tag.
* @param array $attributes The attributes array.
* @param string $content The content string.
*/
public function __construct(string $tag = 'div', $attributes = [], $content = '')
{
$this->setTag($tag);
$this->setAttributes($attributes);
$this->setContent($content);
}
/**
* Set the HTML Element tag name.
*
* @param string $tag The tag name.
*
* @return Element
*/
public function setTag(string $tag)
{
$this->tag = $tag;
return $this;
}
/**
* Get the HTML Element tag name.
*
* @return string
*/
public function getTag()
{
return trim($this->tag);
}
/**
* Render the HTML element.
*
* @return string
*/
public function render()
{
// Start the HTML element tag.
$html = sprintf('<%1$s>', $this->getTag());
// Add attributes.
if ($this->hasAttributes()) {
$html = sprintf('<%1$s %2$s>', $this->tag, $this->renderAttributes());
}
// Add contents.
if ($this->hasContent()) {
$html .= $this->renderContent();
}
// Render children.
if ($this->hasChildren()) {
$html .= $this->renderChildren();
}
// Close the HTML element tag.
if ($this->hasClosingTag()) {
$html .= sprintf('</%1$s>', $this->getTag());
}
return $html;
}
/**
* Render the element HTML when the objected is echoed.
*
* @return string
*/
public function __toString()
{
return $this->render();
}
}