Skip to content

Commit 1ad6f98

Browse files
author
Stiven Katuuk
committed
build(traits): support non backed enum and support php 8.1
1 parent fc57d9c commit 1ad6f98

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
],
3232
"homepage": "https://github.com/spatie/activitylog",
3333
"require": {
34-
"php": "^8.0",
34+
"php": "^8.0|^8.1",
3535
"illuminate/config": "^8.0 || ^9.0",
3636
"illuminate/database": "^8.53 || ^9.0",
3737
"illuminate/support": "^8.0 || ^9.0",

src/Traits/LogsActivity.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Spatie\Activitylog\Contracts\LoggablePipe;
1818
use Spatie\Activitylog\EventLogBag;
1919
use Spatie\Activitylog\LogOptions;
20+
use UnitEnum;
2021

2122
trait LogsActivity
2223
{
@@ -366,6 +367,10 @@ public static function logChanges(Model $model): array
366367
if ($model->hasCast($attribute)) {
367368
$cast = $model->getCasts()[$attribute];
368369

370+
if(function_exists('enum_exists') && enum_exists($cast)) {
371+
$changes[$attribute] = $model->getStorableEnumValue($changes[$attribute]);
372+
}
373+
369374
if ($model->isCustomDateTimeCast($cast) || $model->isImmutableCustomDateTimeCast($cast)) {
370375
$changes[$attribute] = $model->asDateTime($changes[$attribute])->format(explode(':', $cast, 2)[1]);
371376
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Spatie\Activitylog\Test;
4+
5+
use Spatie\Activitylog\Test\Enum\NonBackedEnum;
6+
use Spatie\Activitylog\Test\Models\Activity;
7+
8+
it('can store non backed enum', function () {
9+
$description = 'ROLE LOG';
10+
11+
activity()->withProperty('role', NonBackedEnum::User)->log($description);
12+
13+
expect(Activity::query()->latest()->first()->description)->toEqual($description);
14+
});

tests/Enum/NonBackedEnum.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Spatie\Activitylog\Test\Enum;
4+
5+
enum NonBackedEnum
6+
{
7+
case Admin;
8+
9+
case User;
10+
}

0 commit comments

Comments
 (0)