Skip to content

Commit

Permalink
Merge pull request #1110 from pemudakoding/main
Browse files Browse the repository at this point in the history
Support non backed enum  & php 8.1
  • Loading branch information
Gummibeer authored Nov 10, 2022
2 parents fc57d9c + ec8bd33 commit e2cc899
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/Actions/ResolveForPropertyValueAction.php
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);
}
}
5 changes: 4 additions & 1 deletion src/ActivityLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Macroable;
use Spatie\Activitylog\Actions\ResolveForPropertyValueAction;
use Spatie\Activitylog\Contracts\Activity as ActivityContract;

class ActivityLogger
Expand Down Expand Up @@ -102,13 +103,15 @@ public function setEvent(string $event): static

public function withProperties(mixed $properties): static
{
$this->getActivity()->properties = collect($properties);
$this->getActivity()->properties = collect($properties)->map(fn ($value) => ResolveForPropertyValueAction::execute($value));

return $this;
}

public function withProperty(string $key, mixed $value): static
{
$value = ResolveForPropertyValueAction::execute($value);

$this->getActivity()->properties = $this->getActivity()->properties->put($key, $value);

return $this;
Expand Down
4 changes: 4 additions & 0 deletions src/Traits/LogsActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ public static function logChanges(Model $model): array
if ($model->hasCast($attribute)) {
$cast = $model->getCasts()[$attribute];

if (function_exists('enum_exists') && enum_exists($cast)) {
$changes[$attribute] = $model->getStorableEnumValue($changes[$attribute]);
}

if ($model->isCustomDateTimeCast($cast) || $model->isImmutableCustomDateTimeCast($cast)) {
$changes[$attribute] = $model->asDateTime($changes[$attribute])->format(explode(':', $cast, 2)[1]);
}
Expand Down
39 changes: 39 additions & 0 deletions tests/AbleStoreNonBackedEnumTest.php
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");

10 changes: 10 additions & 0 deletions tests/Enum/NonBackedEnum.php
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;
}

0 comments on commit e2cc899

Please sign in to comment.