7
7
use Illuminate \Database \Eloquent \Relations \MorphPivot ;
8
8
use Illuminate \Database \Eloquent \Relations \MorphTo ;
9
9
use Illuminate \Support \Arr ;
10
+ use Illuminate \Support \Collection ;
10
11
use Illuminate \Support \Str ;
11
12
use Maize \Markable \Exceptions \InvalidMarkableInstanceException ;
12
13
use Maize \Markable \Exceptions \InvalidMarkValueException ;
@@ -15,6 +16,10 @@ abstract class Mark extends MorphPivot
15
16
{
16
17
public $ incrementing = true ;
17
18
19
+ protected $ casts = [
20
+ 'metadata ' => 'array ' ,
21
+ ];
22
+
18
23
abstract public static function markableRelationName (): string ;
19
24
20
25
public static function markRelationName (): string
@@ -37,7 +42,7 @@ public static function getMarkClassName(): string
37
42
->__toString ();
38
43
}
39
44
40
- public static function add (Model $ markable , Model $ user , string $ value = null ): self
45
+ public static function add (Model $ markable , Model $ user , string $ value = null , array $ metadata = [] ): self
41
46
{
42
47
static ::validMarkable ($ markable );
43
48
@@ -51,9 +56,14 @@ public static function add(Model $markable, Model $user, string $value = null):
51
56
'markable_type ' => $ markable ->getMorphClass (),
52
57
'value ' => $ value ,
53
58
];
54
- $ values = static ::forceSingleValuePerUser ()
55
- ? [Arr::pull ($ attributes , 'value ' )]
56
- : [];
59
+
60
+ $ values = collect ([
61
+ 'metadata ' => $ metadata ,
62
+ ])->when (
63
+ value: static ::forceSingleValuePerUser (),
64
+ callback: fn (Collection $ values ) => $ values
65
+ ->add (Arr::pull ($ attributes , 'value ' ))
66
+ )->toArray ();
57
67
58
68
return static ::firstOrCreate ($ attributes , $ values );
59
69
}
@@ -91,11 +101,11 @@ public static function has(Model $markable, Model $user, string $value = null):
91
101
])->exists ();
92
102
}
93
103
94
- public static function toggle (Model $ markable , Model $ user , string $ value = null )
104
+ public static function toggle (Model $ markable , Model $ user , string $ value = null , array $ metadata = [] )
95
105
{
96
106
return static ::has ($ markable , $ user , $ value )
97
107
? static ::remove ($ markable , $ user , $ value )
98
- : static ::add ($ markable , $ user , $ value );
108
+ : static ::add ($ markable , $ user , $ value, $ metadata );
99
109
}
100
110
101
111
public static function hasAllowedValues (?string $ value ): bool
0 commit comments