Skip to content

Commit 471ebbc

Browse files
committed
bug #208 Fix incorrect classes assignation and controller rendering (ker0x)
This PR was merged into the main branch. Discussion ---------- Fix incorrect classes assignation and controller rendering Fix incorrect class assignation mentioned by `@pbories` in #206 (comment) Commits ------- e716638 Fix incorrect classes assignation and controller rendering
2 parents 7e3b6f6 + e716638 commit 471ebbc

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/Dto/StimulusControllersDto.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function addController(string $controllerName, array $controllerValues =
3939
foreach ($controllerClasses as $key => $class) {
4040
$key = $this->escapeAsHtmlAttr($this->normalizeKeyName($key));
4141

42-
$this->values['data-'.$controllerName.'-'.$key.'-class'] = $class;
42+
$this->classes['data-'.$controllerName.'-'.$key.'-class'] = $class;
4343
}
4444
}
4545

@@ -49,15 +49,11 @@ public function __toString(): string
4949
return '';
5050
}
5151

52-
return rtrim(
53-
'data-controller="'.implode(' ', $this->controllers).'" '.
54-
implode(' ', array_map(function (string $attribute, string $value): string {
55-
return $attribute.'="'.$this->escapeAsHtmlAttr($value).'"';
56-
}, array_keys($this->values), $this->values)).' '.
57-
implode(' ', array_map(function (string $attribute, string $value): string {
58-
return $attribute.'="'.$this->escapeAsHtmlAttr($value).'"';
59-
}, array_keys($this->classes), $this->classes))
60-
);
52+
return rtrim(implode(' ', array_filter([
53+
'data-controller="'.implode(' ', $this->controllers).'"',
54+
$this->formatDataAttribute($this->values),
55+
$this->formatDataAttribute($this->classes),
56+
])));
6157
}
6258

6359
public function toArray(): array
@@ -85,4 +81,11 @@ private function normalizeKeyName(string $str): string
8581
// Adapted from ByteString::snake
8682
return strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1-\2', $str));
8783
}
84+
85+
private function formatDataAttribute(array $data): string
86+
{
87+
return implode(' ', array_map(function (string $attribute, string $value): string {
88+
return $attribute.'="'.$this->escapeAsHtmlAttr($value).'"';
89+
}, array_keys($data), $data));
90+
}
8891
}

0 commit comments

Comments
 (0)