Skip to content

Commit

Permalink
Add use of getAttributes() in the render method
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Oct 27, 2023
1 parent a616e34 commit 36c4ed2
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Child.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,16 @@ public function hasAttribute(string $name): bool
return (isset($this->attributes[$name]));
}

/**
* Determine if the child object has attributes
*
* @return bool
*/
public function hasAttributes(): bool
{
return (count($this->attributes) > 0);
}

/**
* Get the attribute of the child object
*
Expand Down Expand Up @@ -472,8 +482,9 @@ public function render(int $depth = 0, ?string $indent = null, bool $inner = fal
}

// Format child attributes, if applicable.
if (count($this->attributes) > 0) {
foreach ($this->attributes as $key => $value) {
if ($this->hasAttributes()) {

This comment has been minimized.

Copy link
@XedinUnknown

XedinUnknown Nov 23, 2023

I'd just do foreach ($this->getAttributes() as $key => $value: if there's nothing, then 0 iterations will be made. But this works! Thank you!

$attributes = $this->getAttributes();
foreach ($attributes as $key => $value) {
$attribAry[] = $key . "=\"" . $value . "\"";
}
$attribs = ' ' . implode(' ', $attribAry);
Expand Down

0 comments on commit 36c4ed2

Please sign in to comment.