@@ -30,17 +30,19 @@ private function userPreferences(): MorphMany
30
30
/**
31
31
* Get a user's preference value or default if not set.
32
32
*
33
- * @param PreferenceGroup $name
34
- * @param mixed|null $default Default value if preference not set.
33
+ * @param PreferenceGroup|Preference $preference
34
+ * @param mixed|null $default Default value if preference not set.
35
35
*
36
36
* @return mixed
37
- * @throws PreferenceNotFoundException|AuthorizationException
37
+ * @throws AuthorizationException
38
+ * @throws PreferenceNotFoundException
38
39
*/
39
- public function getPreference (PreferenceGroup $ name , mixed $ default = null ): mixed
40
+ public function getPreference (PreferenceGroup | Preference $ preference , mixed $ default = null ): mixed
40
41
{
42
+
41
43
$ this ->authorize (PolicyAction::GET );
42
44
43
- $ preference = $ this ->validateAndRetrievePreference ($ name );
45
+ $ preference = $ this ->validateAndRetrievePreference ($ preference );
44
46
45
47
$ userPreference = $ this ->userPreferences ()->where ('preference_id ' , $ preference ->id )->first ();
46
48
@@ -50,17 +52,18 @@ public function getPreference(PreferenceGroup $name, mixed $default = null): mix
50
52
/**
51
53
* Set or update a user's preference value.
52
54
*
53
- * @param PreferenceGroup $name
54
- * @param mixed $value Value to set for the preference.
55
+ * @param PreferenceGroup|Preference $preference
56
+ * @param mixed $value Value to set for the preference.
55
57
*
56
- * @throws PreferenceNotFoundException|AuthorizationException|ValidationException
58
+ * @throws AuthorizationException
59
+ * @throws PreferenceNotFoundException
60
+ * @throws ValidationException
57
61
*/
58
- public function setPreference (PreferenceGroup $ name , mixed $ value ): void
62
+ public function setPreference (PreferenceGroup | Preference $ preference , mixed $ value ): void
59
63
{
60
64
$ this ->authorize (PolicyAction::UPDATE );
61
65
62
-
63
- $ preference = $ this ->validateAndRetrievePreference ($ name );
66
+ $ preference = $ this ->validateAndRetrievePreference ($ preference );
64
67
65
68
$ validator = Validator::make (['value ' => $ value ], ['value ' => $ preference ->getValidationRules ()]);
66
69
@@ -74,16 +77,18 @@ public function setPreference(PreferenceGroup $name, mixed $value): void
74
77
/**
75
78
* Remove a user's preference.
76
79
*
77
- * @param PreferenceGroup $name
80
+ * @param PreferenceGroup|Preference $preference
78
81
*
79
82
* @return int Number of deleted records.
80
- * @throws PreferenceNotFoundException|AuthorizationException
83
+ * @throws AuthorizationException
84
+ * @throws PreferenceNotFoundException
81
85
*/
82
- public function removePreference (PreferenceGroup $ name ): int
86
+ public function removePreference (PreferenceGroup | Preference $ preference ): int
83
87
{
84
88
$ this ->authorize (PolicyAction::DELETE );
85
89
86
- $ preference = $ this ->validateAndRetrievePreference ($ name );
90
+ $ preference = $ this ->validateAndRetrievePreference ($ preference );
91
+
87
92
88
93
return $ this ->userPreferences ()->where ('preference_id ' , $ preference ->id )->delete ();
89
94
}
@@ -112,22 +117,27 @@ public function getPreferences(string $group = null): Collection
112
117
/**
113
118
* Validate existence of a preference and retrieve it.
114
119
*
115
- * @param PreferenceGroup $name Preference name.
120
+ * @param PreferenceGroup|Preference $preference Preference name.
116
121
*
117
122
* @return Preference
118
123
* @throws PreferenceNotFoundException If preference not found.
119
124
*/
120
- private function validateAndRetrievePreference (PreferenceGroup $ name ): Preference
125
+ private function validateAndRetrievePreference (PreferenceGroup | Preference $ preference ): Preference
121
126
{
122
- SerializeHelper::conformNameAndGroup ($ name , $ group );
123
127
124
- /**@var string $name * */
125
- $ preference = Preference::where ('group ' , $ group )->where ('name ' , $ name )->first ();
128
+ if (!$ preference instanceof Preference) {
129
+
130
+ SerializeHelper::conformNameAndGroup ($ preference , $ group );
126
131
132
+ /**@var Preference $preference * */
133
+ $ preference = Preference::where ('group ' , $ group )->where ('name ' , $ preference )->first ();
134
+ }
127
135
if (!$ preference ) {
128
- throw new PreferenceNotFoundException ("Preference not found: $ name in group $ group " );
136
+ throw new PreferenceNotFoundException ("Preference not found: $ preference in group $ group " );
129
137
}
130
138
139
+ //Todo Gate
140
+
131
141
return $ preference ;
132
142
}
133
143
0 commit comments