44
55namespace Rawilk \Settings ;
66
7+ use BackedEnum ;
78use Illuminate \Contracts \Cache \Repository as Cache ;
89use Illuminate \Contracts \Encryption \Encrypter ;
910use Illuminate \Database \Eloquent \Model ;
1718use Rawilk \Settings \Events \SettingWasDeleted ;
1819use Rawilk \Settings \Events \SettingWasStored ;
1920use Rawilk \Settings \Exceptions \InvalidBulkValueResult ;
21+ use Rawilk \Settings \Exceptions \InvalidEnumType ;
2022use Rawilk \Settings \Exceptions \InvalidKeyGenerator ;
2123use Rawilk \Settings \Support \Context ;
2224use Rawilk \Settings \Support \KeyGenerators \Md5KeyGenerator ;
@@ -108,7 +110,7 @@ public function setTeamForeignKey(?string $foreignKey): self
108110 return $ this ;
109111 }
110112
111- public function forget ($ key )
113+ public function forget (string | BackedEnum $ key )
112114 {
113115 $ key = $ this ->normalizeKey ($ key );
114116
@@ -141,7 +143,7 @@ public function forget($key)
141143 return $ driverResult ;
142144 }
143145
144- public function get (string $ key , $ default = null )
146+ public function get (string | BackedEnum $ key , $ default = null )
145147 {
146148 $ key = $ this ->normalizeKey ($ key );
147149
@@ -210,7 +212,7 @@ public function all($keys = null): Collection
210212 return $ values ;
211213 }
212214
213- public function has ($ key ): bool
215+ public function has (string | BackedEnum $ key ): bool
214216 {
215217 $ key = $ this ->normalizeKey ($ key );
216218
@@ -229,7 +231,7 @@ public function has($key): bool
229231 return $ has ;
230232 }
231233
232- public function set (string $ key , $ value = null ): mixed
234+ public function set (string | BackedEnum $ key , $ value = null ): mixed
233235 {
234236 $ key = $ this ->normalizeKey ($ key );
235237
@@ -269,14 +271,14 @@ public function set(string $key, $value = null): mixed
269271 return $ driverResult ;
270272 }
271273
272- public function isFalse (string $ key , $ default = false ): bool
274+ public function isFalse (string | BackedEnum $ key , $ default = false ): bool
273275 {
274276 $ value = $ this ->get (key: $ key , default: $ default );
275277
276278 return $ value === false || $ value === '0 ' || $ value === 0 ;
277279 }
278280
279- public function isTrue (string $ key , $ default = true ): bool
281+ public function isTrue (string | BackedEnum $ key , $ default = true ): bool
280282 {
281283 $ value = $ this ->get (key: $ key , default: $ default );
282284
@@ -400,7 +402,7 @@ public function getKeyGenerator(): KeyGenerator
400402 * Generate the key to use for caching a specific setting.
401403 * This is meant for external usage.
402404 */
403- public function cacheKeyForSetting (string $ key ): string
405+ public function cacheKeyForSetting (string | BackedEnum $ key ): string
404406 {
405407 $ storageKey = $ this ->getKeyForStorage (
406408 $ this ->normalizeKey ($ key ),
@@ -417,8 +419,17 @@ public function cacheKeyForSetting(string $key): string
417419 return $ cacheKey ;
418420 }
419421
420- protected function normalizeKey (string $ key ): string
422+ protected function normalizeKey (string | BackedEnum $ key ): string
421423 {
424+ if ($ key instanceof BackedEnum) {
425+ throw_unless (
426+ is_string ($ key ->value ),
427+ InvalidEnumType::make ($ key ::class)
428+ );
429+
430+ $ key = $ key ->value ;
431+ }
432+
422433 if (Str::startsWith (haystack: $ key , needles: 'file_ ' )) {
423434 return str_replace (search: 'file_ ' , replace: 'file. ' , subject: $ key );
424435 }
@@ -542,6 +553,6 @@ protected function normalizeBulkLookupKey($key): string|Collection|bool
542553 return collect ($ key )
543554 ->flatten ()
544555 ->filter ()
545- ->map (fn (string $ key ): string => $ this ->getKeyForStorage ($ this ->normalizeKey ($ key )));
556+ ->map (fn (string | BackedEnum $ key ): string => $ this ->getKeyForStorage ($ this ->normalizeKey ($ key )));
546557 }
547558}
0 commit comments