77use Illuminate \Database \Eloquent \Relations \MorphPivot ;
88use Illuminate \Database \Eloquent \Relations \MorphTo ;
99use Illuminate \Support \Arr ;
10+ use Illuminate \Support \Collection ;
1011use Illuminate \Support \Str ;
1112use Maize \Markable \Exceptions \InvalidMarkableInstanceException ;
1213use Maize \Markable \Exceptions \InvalidMarkValueException ;
@@ -15,6 +16,10 @@ abstract class Mark extends MorphPivot
1516{
1617 public $ incrementing = true ;
1718
19+ protected $ casts = [
20+ 'metadata ' => 'array ' ,
21+ ];
22+
1823 abstract public static function markableRelationName (): string ;
1924
2025 public static function markRelationName (): string
@@ -37,7 +42,7 @@ public static function getMarkClassName(): string
3742 ->__toString ();
3843 }
3944
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
4146 {
4247 static ::validMarkable ($ markable );
4348
@@ -51,9 +56,14 @@ public static function add(Model $markable, Model $user, string $value = null):
5156 'markable_type ' => $ markable ->getMorphClass (),
5257 'value ' => $ value ,
5358 ];
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 ();
5767
5868 return static ::firstOrCreate ($ attributes , $ values );
5969 }
@@ -91,11 +101,11 @@ public static function has(Model $markable, Model $user, string $value = null):
91101 ])->exists ();
92102 }
93103
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 = [] )
95105 {
96106 return static ::has ($ markable , $ user , $ value )
97107 ? static ::remove ($ markable , $ user , $ value )
98- : static ::add ($ markable , $ user , $ value );
108+ : static ::add ($ markable , $ user , $ value, $ metadata );
99109 }
100110
101111 public static function hasAllowedValues (?string $ value ): bool
0 commit comments