Skip to content

Commit

Permalink
Handle nested arrays in CurlyListNode (#274)
Browse files Browse the repository at this point in the history
* Handle nested arrays in CurlyListNode

* Specify array value type

This could be almost anything, so mixed is the best we can do for now - also matches the type used in stringifyValue
  • Loading branch information
lukasluecke committed Jun 23, 2021
1 parent ea11625 commit 68d7671
Showing 1 changed file with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,7 @@ final class CurlyListNode extends AbstractValuesAwareNode implements Stringable
{
public function __toString(): string
{
$itemContents = '';
$lastItemKey = array_key_last($this->values);

foreach ($this->values as $key => $value) {
if (is_int($key)) {
$itemContents .= $this->stringifyValue($value);
} else {
$itemContents .= $key . '=' . $this->stringifyValue($value);
}

if ($lastItemKey !== $key) {
$itemContents .= ', ';
}
}

return '{' . $itemContents . '}';
return $this->implode($this->values);
}

/**
Expand All @@ -42,9 +27,32 @@ private function stringifyValue($value): string
}

if (is_array($value)) {
return implode(', ', $value);
return $this->implode($value);
}

return (string) $value;
}

/**
* @param mixed[] $array
*/
private function implode(array $array): string
{
$itemContents = '';
$lastItemKey = array_key_last($array);

foreach ($array as $key => $value) {
if (is_int($key)) {
$itemContents .= $this->stringifyValue($value);
} else {
$itemContents .= $key . '=' . $this->stringifyValue($value);
}

if ($lastItemKey !== $key) {
$itemContents .= ', ';
}
}

return '{' . $itemContents . '}';
}
}

0 comments on commit 68d7671

Please sign in to comment.