Skip to content

Commit fc81780

Browse files
committed
Refactor ReplaceModelAttributes to handle array values efficiently
1 parent 96795fe commit fc81780

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/Replacers/Concerns/ReplaceModelAttributes.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,20 @@ trait ReplaceModelAttributes
1919
{
2020
public function replaceModelAttributes(string $text, string $replaceText, Model $model)
2121
{
22-
return preg_replace_callback('/::' . $replaceText . '::/', function ($match) use ($model) {
22+
return preg_replace_callback('/::'.$replaceText.'::/', function ($match) use ($model) {
2323
$parts = collect(explode('.', $match[0] ?? ''));
2424

2525
$replace = $parts->reduce(function ($value, $part) {
2626
$part = Str::between($part, '::', '::');
2727

28-
return $value->$part
29-
?? $value[$part]
30-
?? '';
28+
return $value->$part ?? $value[$part] ?? '';
3129
}, $model);
3230

33-
return $replace ?: $match;
31+
if (is_array($replace)) {
32+
return implode(', ', $replace);
33+
}
34+
35+
return $replace ?: $match[0];
3436
}, $text);
3537
}
3638
}

0 commit comments

Comments
 (0)