Skip to content

Commit c9446f3

Browse files
author
Andrey Helldar
committed
Added setSkipEmptyValues() method
1 parent 6bf6434 commit c9446f3

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Diff for: src/Facades/Xml.php

+13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class Xml
1414
/** @var \DOMElement */
1515
protected $root;
1616

17+
protected $skip_empty_values = false;
18+
1719
public function __construct(string $root = 'root', array $attributes = [], bool $format_output = false)
1820
{
1921
$this->doc = new DOMDocument('1.0', 'utf-8');
@@ -38,6 +40,13 @@ public static function init(string $root = 'root', array $attributes = [], bool
3840
return new self($root, $attributes, $format_output);
3941
}
4042

43+
public function setSkipEmptyValues(): self
44+
{
45+
$this->skip_empty_values = true;
46+
47+
return $this;
48+
}
49+
4150
public function doctype($qualified_name = null, $public_id = null, $system_id = null): self
4251
{
4352
$implementation = new DOMImplementation;
@@ -106,6 +115,10 @@ public function get(): string
106115
private function setAttributes(DOMElement &$element, array $attributes = [])
107116
{
108117
foreach ($attributes as $name => $value) {
118+
if ($this->skip_empty_values && empty($value)) {
119+
continue;
120+
}
121+
109122
$element->setAttribute($name, $value);
110123
}
111124
}

0 commit comments

Comments
 (0)