-
-
Notifications
You must be signed in to change notification settings - Fork 718
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1110 from pemudakoding/main
Support non backed enum & php 8.1
- Loading branch information
Showing
5 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Spatie\Activitylog\Actions; | ||
|
||
class ResolveForPropertyValueAction | ||
{ | ||
/** | ||
* Action that resolve property value of log | ||
* that cannot be handled by PHP as default | ||
* | ||
* @param mixed $value | ||
* @return mixed | ||
*/ | ||
public static function execute(mixed $value): mixed | ||
{ | ||
$instance = new static; | ||
|
||
/** | ||
* Give a fallback value if value not a backed enum | ||
*/ | ||
if ($instance->isValueAnEnum($value)) { | ||
return $value->value ?? $value->name; | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
protected function isValueAnEnum($value): bool | ||
{ | ||
if (! function_exists('enum_exists')){ | ||
return false; | ||
} | ||
|
||
$enumNamespace = is_object($value) ? get_class($value): $value; | ||
|
||
return ! is_array($value) && enum_exists($enumNamespace); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Spatie\Activitylog\Test; | ||
|
||
use Illuminate\Support\Str; | ||
use Spatie\Activitylog\Test\Enum\NonBackedEnum; | ||
use Spatie\Activitylog\Test\Models\Activity; | ||
use Spatie\Activitylog\Test\Models\User; | ||
|
||
afterEach(fn() => Activity::query()->latest()->first()->delete()); | ||
|
||
it('can store non-backed enum only a property', function () { | ||
$description = 'ROLE LOG'; | ||
|
||
activity() | ||
->performedOn(User::first()) | ||
->withProperty('role', NonBackedEnum::User)->log($description); | ||
|
||
$latestActivity = Activity::query()->latest()->first(); | ||
|
||
expect($latestActivity->description)->toEqual($description) | ||
->and($latestActivity->properties['role'])->toEqual('User'); | ||
}) | ||
->skip(version_compare(PHP_VERSION, '8.1', '<'), "PHP < 8.1 doesn't support enum"); | ||
|
||
it('can store non-backed enum with properties', function () { | ||
$description = 'ROLE LOG'; | ||
|
||
activity() | ||
->performedOn(User::first()) | ||
->withProperties(['role' => NonBackedEnum::User])->log($description); | ||
|
||
$latestActivity = Activity::query()->latest()->first(); | ||
|
||
expect($latestActivity->description)->toEqual($description) | ||
->and($latestActivity->properties['role'])->toEqual('User'); | ||
}) | ||
->skip(version_compare(PHP_VERSION, '8.1', '<'), "PHP < 8.1 doesn't support enum"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Spatie\Activitylog\Test\Enum; | ||
|
||
enum NonBackedEnum | ||
{ | ||
case Admin; | ||
|
||
case User; | ||
} |