Skip to content

Commit dfaaca5

Browse files
author
Wazabii
committed
DOM improvement
1 parent 6c5a56b commit dfaaca5

File tree

2 files changed

+57
-24
lines changed

2 files changed

+57
-24
lines changed

Dom/Document.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* @Author: Daniel Ronkainen
55
* @Licence: The MIT License (MIT), Copyright © Daniel Ronkainen
66
Don't delete this comment, its part of the license.
7-
* @Version: 1.0.0
87
*/
98

109
namespace PHPFuse\Output\Dom;
@@ -15,10 +14,9 @@ class Document {
1514
"meta", "link", "img", "br", "hr", "input", "keygen", "param", "source", "track", "embed"
1615
];
1716

18-
private $html;
1917
protected $elements;
18+
private $html;
2019
private $el;
21-
2220
private static $inst;
2321

2422
/**
@@ -135,6 +133,7 @@ function __toString() {
135133
return $this->get();
136134
}
137135

136+
138137
/**
139138
* Build document
140139
* @param array $arr elements
@@ -143,22 +142,16 @@ function __toString() {
143142
private function build(array $arr, ?callable $call = NULL) {
144143
foreach($arr as $k => $a) {
145144
$hasNoEnding = in_array($a->getEl(), $this::TAG_NO_ENDING);
146-
147145
if(!is_null($call)) $call($a, $k, $hasNoEnding);
148146

149-
$this->html .= "\t<".$a->getEl().$a->buildAttr().">";
147+
if(!$a->hideTagValid()) $this->html .= "\t<".$a->getEl().$a->buildAttr().">";
150148
if(!$hasNoEnding) $this->html .= $a->getValue();
151149
if(isset($a->elements)) {
152150
$this->build($a->elements, $call);
153151
}
154-
if(!$hasNoEnding) $this->html .= "</".$a->getEl().">\n";
155-
if($hasNoEnding) $this->html .= "\n";
152+
if(!$hasNoEnding && !$a->hideTagValid()) $this->html .= "</".$a->getEl().">\n";
153+
if($hasNoEnding && !$a->hideTagValid()) $this->html .= "\n";
156154
}
157155
}
158156

159157
}
160-
161-
162-
163-
164-
?>

Dom/Element.php

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Element extends Document {
1919
private $snippet;
2020
private $value;
2121
private $node;
22+
private $hideEmptyTag = false;
2223

2324

2425
function __construct(string $el, ?string $value, bool $snippet = false) {
@@ -59,24 +60,55 @@ function attrArr(?array $arr) {
5960
return $this;
6061
}
6162

62-
function attrAddTo(string $key, string $value, string $sep = " ") {
63-
63+
/**
64+
* Hide html tag if its value is empty
65+
* @param bool $bool
66+
* @return self
67+
*/
68+
function hideEmptyTag(bool $bool): self
69+
{
70+
$this->hideEmptyTag = $bool;
71+
return $this;
72+
}
6473

74+
/**
75+
* Validate hide tag value
76+
* @return bool
77+
*/
78+
protected function hideTagValid(): bool
79+
{
80+
return (bool)(($this->hideEmptyTag && !$this->value));
81+
}
82+
83+
/**
84+
* Add value to attr
85+
* @param string $key
86+
* @param string $value
87+
* @param string $sep
88+
* @return self
89+
*/
90+
function attrAdd(string $key, string $value, string $sep = " "): self
91+
{
6592
if(isset($this->attr[$key])) {
6693
$this->attr[$key] .= "{$sep}{$value}";
6794
} else {
6895
$this->attr[$key] = $value;
6996
}
70-
7197
return $this;
7298
}
73-
99+
100+
// Same as above
101+
function attrAddTo(string $key, string $value, string $sep = " "): self
102+
{
103+
return $this->attrAdd($key, $value, $sep);
104+
}
74105

75106
/**
76107
* Set el value <elem>[VALUE]</elem>
77108
* @param self
78109
*/
79-
function setValue(?string $value) {
110+
function setValue(?string $value): self
111+
{
80112
$this->value = $value;
81113
return $this;
82114
}
@@ -85,23 +117,26 @@ function setValue(?string $value) {
85117
* Set el value
86118
* @param string
87119
*/
88-
function getValue() {
89-
return $this->value;
120+
function getValue(): string
121+
{
122+
return (string)$this->value;
90123
}
91124

92125
/**
93126
* Get el/HTML tag
94-
* @return [type] [description]
127+
* @return string
95128
*/
96-
function getEl() {
97-
return $this->el;
129+
function getEl(): string
130+
{
131+
return (string)$this->el;
98132
}
99133

100134
/**
101135
* Array attr to string
102136
* @return string
103137
*/
104-
function buildAttr() {
138+
protected function buildAttr(): string
139+
{
105140
$attr = "";
106141
if(count($this->attr) > 0) foreach($this->attr as $k => $v) {
107142
$attr .= " {$k}";
@@ -110,7 +145,12 @@ function buildAttr() {
110145
return $attr;
111146
}
112147

113-
function withElement() {
148+
/**
149+
* Clone/Static
150+
* @return static|false
151+
*/
152+
function withElement()
153+
{
114154
if(!is_null($this->el)) {
115155
return clone $this;
116156
}

0 commit comments

Comments
 (0)