Skip to content

Commit d774c23

Browse files
authored
Merge pull request spatie#1038 from stevebauman/fix-placeholder-to-array
Use `data_get()` for placeholder replacement instead of `Arr::get()`
2 parents ed0a1e4 + b56e0f4 commit d774c23

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/ActivityLogger.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use DateTimeInterface;
77
use Illuminate\Contracts\Config\Repository;
88
use Illuminate\Database\Eloquent\Model;
9-
use Illuminate\Support\Arr;
109
use Illuminate\Support\Carbon;
1110
use Illuminate\Support\Str;
1211
use Illuminate\Support\Traits\Conditionable;
@@ -213,9 +212,7 @@ protected function replacePlaceholders(string $description, ActivityContract $ac
213212
return $match;
214213
}
215214

216-
$attributeValue = $attributeValue->toArray();
217-
218-
return Arr::get($attributeValue, $propertyName, $match);
215+
return data_get($attributeValue, $propertyName, $match);
219216
}, $description);
220217
}
221218

tests/ActivityLoggerTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,26 @@
227227
expect($this->getLastActivity()->description)->toEqual($expectedDescription);
228228
});
229229

230+
it('can replace the placeholders with object properties and accessors', function () {
231+
$article = Article::create([
232+
'name' => 'article name',
233+
'user_id' => User::first()->id,
234+
]);
235+
236+
$article->foo = new stdClass();
237+
$article->foo->bar = new stdClass();
238+
$article->foo->bar->baz = 'zal';
239+
240+
activity()
241+
->performedOn($article)
242+
->withProperties(['key' => 'value', 'key2' => ['subkey' => 'subvalue']])
243+
->log('Subject name is :subject.name, deeply nested property is :subject.foo.bar.baz, accessor property is :subject.owner_name');
244+
245+
$expectedDescription = 'Subject name is article name, deeply nested property is zal, accessor property is name 1';
246+
247+
expect($this->getLastActivity()->description)->toEqual($expectedDescription);
248+
});
249+
230250
it('can log an activity with event', function () {
231251
$article = Article::create(['name' => 'article name']);
232252
activity()

tests/Models/Article.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ class Article extends Model
1010

1111
protected $guarded = [];
1212

13-
public function User()
13+
public function user()
1414
{
1515
return $this->belongsTo(User::class);
1616
}
17+
18+
public function getOwnerNameAttribute()
19+
{
20+
return $this->user?->name;
21+
}
1722
}

0 commit comments

Comments
 (0)