|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Botble\Api\Models; |
| 4 | + |
| 5 | +use Botble\Base\Models\BaseModel; |
| 6 | +use Botble\Base\Models\Concerns\HasUuidsOrIntegerIds; |
| 7 | +use Illuminate\Database\Eloquent\Relations\MorphTo; |
| 8 | + |
| 9 | +class UserSetting extends BaseModel |
| 10 | +{ |
| 11 | + use HasUuidsOrIntegerIds; |
| 12 | + |
| 13 | + protected $table = 'user_settings'; |
| 14 | + |
| 15 | + protected $fillable = [ |
| 16 | + 'user_type', |
| 17 | + 'user_id', |
| 18 | + 'key', |
| 19 | + 'value', |
| 20 | + ]; |
| 21 | + |
| 22 | + protected $casts = [ |
| 23 | + 'value' => 'array', |
| 24 | + ]; |
| 25 | + |
| 26 | + public function user(): MorphTo |
| 27 | + { |
| 28 | + return $this->morphTo('user', 'user_type', 'user_id'); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Get a setting value for a user |
| 33 | + */ |
| 34 | + public static function getUserSetting(string $userType, int $userId, string $key, $default = null) |
| 35 | + { |
| 36 | + $setting = static::query() |
| 37 | + ->where('user_type', $userType) |
| 38 | + ->where('user_id', $userId) |
| 39 | + ->where('key', $key) |
| 40 | + ->first(); |
| 41 | + |
| 42 | + return $setting ? $setting->value : $default; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Set a setting value for a user |
| 47 | + */ |
| 48 | + public static function setUserSetting(string $userType, int $userId, string $key, $value): self |
| 49 | + { |
| 50 | + return static::query()->updateOrCreate( |
| 51 | + [ |
| 52 | + 'user_type' => $userType, |
| 53 | + 'user_id' => $userId, |
| 54 | + 'key' => $key, |
| 55 | + ], |
| 56 | + [ |
| 57 | + 'value' => $value, |
| 58 | + ] |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Get all settings for a user |
| 64 | + */ |
| 65 | + public static function getUserSettings(string $userType, int $userId): array |
| 66 | + { |
| 67 | + $settings = static::query() |
| 68 | + ->where('user_type', $userType) |
| 69 | + ->where('user_id', $userId) |
| 70 | + ->get(); |
| 71 | + |
| 72 | + $result = []; |
| 73 | + foreach ($settings as $setting) { |
| 74 | + $result[$setting->key] = $setting->value; |
| 75 | + } |
| 76 | + |
| 77 | + return $result; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Set multiple settings for a user |
| 82 | + */ |
| 83 | + public static function setUserSettings(string $userType, int $userId, array $settings): void |
| 84 | + { |
| 85 | + foreach ($settings as $key => $value) { |
| 86 | + static::setUserSetting($userType, $userId, $key, $value); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Delete a setting for a user |
| 92 | + */ |
| 93 | + public static function deleteUserSetting(string $userType, int $userId, string $key): bool |
| 94 | + { |
| 95 | + return static::query() |
| 96 | + ->where('user_type', $userType) |
| 97 | + ->where('user_id', $userId) |
| 98 | + ->where('key', $key) |
| 99 | + ->delete() > 0; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Get biometric setting for a user |
| 104 | + */ |
| 105 | + public static function getBiometricEnabled(string $userType, int $userId): bool |
| 106 | + { |
| 107 | + return (bool) static::getUserSetting($userType, $userId, 'biometric_enabled', false); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Set biometric setting for a user |
| 112 | + */ |
| 113 | + public static function setBiometricEnabled(string $userType, int $userId, bool $enabled): self |
| 114 | + { |
| 115 | + return static::setUserSetting($userType, $userId, 'biometric_enabled', $enabled); |
| 116 | + } |
| 117 | +} |
0 commit comments