File tree Expand file tree Collapse file tree 3 files changed +60
-6
lines changed Expand file tree Collapse file tree 3 files changed +60
-6
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Spatie \Activitylog \Actions ;
4
+
5
+ class ResolveForPropertyValueAction
6
+ {
7
+ /**
8
+ * Action that resolve property value of log
9
+ * that cannot be handled by PHP or This Package
10
+ *
11
+ * @param mixed $value
12
+ * @return mixed
13
+ */
14
+ public static function execute (mixed $ value ): mixed
15
+ {
16
+ $ instance = new static ;
17
+
18
+ if ($ instance ->isValueAreEnum ($ value )) {
19
+ return $ value ->value ?? $ value ->name ;
20
+ }
21
+
22
+ return $ value ;
23
+ }
24
+
25
+ protected function isValueAreEnum ($ value ): bool
26
+ {
27
+ if (! function_exists ('enum_exists ' )){
28
+ return false ;
29
+ }
30
+
31
+ $ enumNamespace = is_object ($ value )
32
+ ? get_class ($ value )
33
+ : $ value ;
34
+
35
+ return enum_exists ($ enumNamespace );
36
+ }
37
+ }
Original file line number Diff line number Diff line change 10
10
use Illuminate \Support \Str ;
11
11
use Illuminate \Support \Traits \Conditionable ;
12
12
use Illuminate \Support \Traits \Macroable ;
13
+ use Spatie \Activitylog \Actions \ResolveForPropertyValueAction ;
13
14
use Spatie \Activitylog \Contracts \Activity as ActivityContract ;
14
15
15
16
class ActivityLogger
@@ -102,16 +103,14 @@ public function setEvent(string $event): static
102
103
103
104
public function withProperties (mixed $ properties ): static
104
105
{
105
- $ this ->getActivity ()->properties = collect ($ properties );
106
+ $ this ->getActivity ()->properties = collect ($ properties )-> map ( fn ( $ value ) => ResolveForPropertyValueAction:: execute ( $ value )) ;
106
107
107
108
return $ this ;
108
109
}
109
110
110
111
public function withProperty (string $ key , mixed $ value ): static
111
112
{
112
- if (is_object ($ value ) && function_exists ('enum_exists ' ) && enum_exists (get_class ($ value ))) {
113
- $ value = $ value ->value ?? $ value ->name ;
114
- }
113
+ $ value = ResolveForPropertyValueAction::execute ($ value );
115
114
116
115
$ this ->getActivity ()->properties = $ this ->getActivity ()->properties ->put ($ key , $ value );
117
116
Original file line number Diff line number Diff line change 6
6
use Spatie \Activitylog \Test \Models \Activity ;
7
7
use Spatie \Activitylog \Test \Models \User ;
8
8
9
- it ('can store non backed enum ' , function () {
9
+ afterEach (fn () => Activity::query ()->latest ()->first ()->delete ());
10
+
11
+ it ('can store non backed only a property ' , function () {
10
12
$ description = 'ROLE LOG ' ;
11
13
12
14
activity ()
13
15
->performedOn (User::first ())
14
16
->withProperty ('role ' , NonBackedEnum::User)->log ($ description );
15
17
16
- expect (Activity::query ()->latest ()->first ()->description )->toEqual ($ description );
18
+ $ latestActivity = Activity::query ()->latest ()->first ();
19
+
20
+ expect ($ latestActivity ->description )->toEqual ($ description )
21
+ ->and ($ latestActivity ->properties ['role ' ])->toEqual ('User ' );
22
+ });
23
+
24
+ it ('can store non backed with properties ' , function () {
25
+ $ description = 'ROLE LOG ' ;
26
+
27
+ activity ()
28
+ ->performedOn (User::first ())
29
+ ->withProperties (['role ' => NonBackedEnum::User])->log ($ description );
30
+
31
+ $ latestActivity = Activity::query ()->latest ()->first ();
32
+
33
+ expect ($ latestActivity ->description )->toEqual ($ description )
34
+ ->and ($ latestActivity ->properties ['role ' ])->toEqual ('User ' );
17
35
});
You can’t perform that action at this time.
0 commit comments