From 5d9c4c727716e8f3aac46744e436579ddf3589be Mon Sep 17 00:00:00 2001 From: kykurniawan Date: Wed, 5 Feb 2025 06:56:23 +0700 Subject: [PATCH] feat: add type cast while get setting --- README.md | 65 +++++++++++++++ src/Enums/Type.php | 13 +++ src/Services/SettingsService.php | 138 +++++++++++++++++++++++++++++++ src/Traits/HasSettings.php | 92 +++++++++++++++++++++ 4 files changed, 308 insertions(+) create mode 100644 src/Enums/Type.php diff --git a/README.md b/README.md index 4a7779f..4695b34 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,39 @@ $siteTItle = Settings::get('site_title'); $siteTitle = Settings::get('site_title', 'Your Default Awesome Website'); ``` +**Get a setting and cast it to a specific type** + +```php +use RuangDeveloper\LaravelSettings\Enums\Type; +use RuangDeveloper\LaravelSettings\Facades\Settings; + +// retrieve the global site title +$siteTitle = Settings::getAs('site_title', Type::String); + +// you may want to add a default fallback value if the setting +// with provided key doesn't exists in the database +$siteTitle = Settings::getAs('site_title', Type::String, 'Your Default Awesome Website'); +``` + +Available types: + +- String +- Integer +- Float +- Boolean +- Array +- Object + +You can also use the following methods to get a setting and cast it to a specific type: + +- `Settings::getString('key', $default)` +- `Settings::getInteger('key', $default)` +- `Settings::getFloat('key', $default)` +- `Settings::getBoolean('key', $default)` +- `Settings::getArray('key', $default)` +- `Settings::getObject('key', $default)` + + ### Delete a global setting Now, if you want to delete the setting @@ -130,6 +163,38 @@ $isSubscribed = $user->getSetting('subscribe_newsletter', false); // ``` +**Get a setting and cast it to a specific type** + +```php +use RuangDeveloper\LaravelSettings\Enums\Type; +use RuangDeveloper\LaravelSettings\Facades\Settings; + +$user = App\Models\User::find(1); +$isSubscribed = $user->getSettingAs('subscribe_newsletter', Type::Boolean); + +// you may want to add a default fallback value if the setting +// with provided key doesn't exists in the database +$isSubscribed = $user->getSettingAs('subscribe_newsletter', Type::Boolean, false); +``` + +Available types: + +- String +- Integer +- Float +- Boolean +- Array +- Object + +You can also use the following methods to get a setting and cast it to a specific type: + +- `$yourModel->getSettingString('key', $default)` +- `$yourModel->getSettingInteger('key', $default)` +- `$yourModel->getSettingFloat('key', $default)` +- `$yourModel->getSettingBoolean('key', $default)` +- `$yourModel->getSettingArray('key', $default)` +- `$yourModel->getSettingObject('key', $default)` + **Delete a setting** ```php diff --git a/src/Enums/Type.php b/src/Enums/Type.php new file mode 100644 index 0000000..a9a2049 --- /dev/null +++ b/src/Enums/Type.php @@ -0,0 +1,13 @@ +findSetting($key, null, null, $default); } + /** + * Get a setting and cast it to a specific type. + * + * @param string $key + * @param Type $type + * @param mixed $default + * @param string|null $modelType + * @param mixed|null $modelId + * @return mixed + */ + public function getAs(string $key, Type $type, mixed $default = null, string $modelType = null, mixed $modelId = null): mixed + { + $value = $default; + if ($modelType && $modelId) { + $value = $this->getWithModel($key, $modelType, $modelId, $default); + } else { + $value = $this->get($key, $default); + } + + if (is_null($value)) return $value; + + return $this->cast($value, $type); + } + + /** + * Get a setting and cast it to string + * + * @param string $key + * @param mixed $default + * @param string|null $modelType + * @param mixed|null $modelId + * @return mixed + */ + public function getString(string $key, mixed $default = null, string $modelType = null, mixed $modelId = null): mixed + { + return $this->getAs($key, Type::String, $default, $modelType, $modelId); + } + + /** + * Get a setting and cast it to integer. + * + * @param string $key + * @param mixed $default + * @param string|null $modelType + * @param mixed|null $modelId + * @return mixed + */ + public function getInteger(string $key, mixed $default = null, string $modelType = null, mixed $modelId = null): mixed + { + return $this->getAs($key, Type::Integer, $default, $modelType, $modelId); + } + + /** + * Get a setting and cast it to float. + * + * @param string $key + * @param mixed $default + * @param string|null $modelType + * @param mixed|null $modelId + * @return mixed + */ + public function getFloat(string $key, mixed $default = null, string $modelType = null, mixed $modelId = null): mixed + { + return $this->getAs($key, Type::Float, $default, $modelType, $modelId); + } + + /** + * Get a setting and cast it to boolean. + * + * @param string $key + * @param mixed $default + * @param string|null $modelType + * @param mixed|null $modelId + * @return mixed + */ + public function getBoolean(string $key, mixed $default = null, string $modelType = null, mixed $modelId = null): mixed + { + return $this->getAs($key, Type::Boolean, $default, $modelType, $modelId); + } + + /** + * Get a setting and cast it to array. + * + * @param string $key + * @param mixed $default + * @param string|null $modelType + * @param mixed|null $modelId + * @return mixed + */ + public function getArray(string $key, mixed $default = null, string $modelType = null, mixed $modelId = null): mixed + { + return $this->getAs($key, Type::Array, $default, $modelType, $modelId); + } + + /** + * Get a setting and cast it to object. + * + * @param string $key + * @param mixed $default + * @param string|null $modelType + * @param mixed|null $modelId + * @return mixed + */ + public function getObject(string $key, mixed $default = null, string $modelType = null, mixed $modelId = null): mixed + { + return $this->getAs($key, Type::Object, $default, $modelType, $modelId); + } + /** * Forget a setting value. * @@ -229,4 +338,33 @@ private function deleteSetting(string $key, string $modelType = null, mixed $mod Cache::forget(Support::getCacheKey($key, $modelType, $modelId)); } } + + /** + * Cast a value to a specific type. + * + * @param mixed $value + * @param Type $type + * @return mixed + */ + private function cast(mixed $value, Type $type): mixed + { + if (is_null($value)) return $value; + + switch ($type) { + case Type::String: + return (string) $value; + case Type::Integer: + return (int) $value; + case Type::Float: + return (float) $value; + case Type::Boolean: + return (bool) $value; + case Type::Array: + return (array) $value; + case Type::Object: + return (object) $value; + default: + return $value; + } + } } diff --git a/src/Traits/HasSettings.php b/src/Traits/HasSettings.php index f078a84..946a110 100644 --- a/src/Traits/HasSettings.php +++ b/src/Traits/HasSettings.php @@ -3,6 +3,8 @@ namespace RuangDeveloper\LaravelSettings\Traits; use Illuminate\Support\Facades\Cache; +use RuangDeveloper\LaravelSettings\Enums\Type; +use RuangDeveloper\LaravelSettings\Services\SettingsService; use RuangDeveloper\LaravelSettings\Supports\Support; trait HasSettings @@ -131,4 +133,94 @@ public function deleteSetting(string $key): void Cache()->forget(Support::getCacheKey($key, $this->getMorphClass(), $this->getKey())); } } + + /** + * Get setting value and cast it to a specific type. + * + * @param string $key + * @param Type + * @param mixed $default + * @return mixed + */ + public function getSettingAs(string $key, Type $type, mixed $default = null): mixed + { + return $this->getSettingService()->getAs($key, $type, $default, $this->getMorphClass(), $this->getKey()); + } + + /** + * Get setting value and cast it to string. + * + * @param string $key + * @param mixed $default + * @return mixed + */ + public function getSettingString(string $key, mixed $default = null): mixed + { + return $this->getSettingAs($key, Type::String, $default); + } + + /** + * Get setting value and cast it to integer. + * + * @param string $key + * @param mixed $default + * @return mixed + */ + public function getSettingInteger(string $key, mixed $default = null): mixed + { + return $this->getSettingAs($key, Type::Integer, $default); + } + + /** + * Get setting value and cast it to float. + * + * @param string $key + * @param mixed $default + * @return mixed + */ + public function getSettingFloat(string $key, mixed $default = null): mixed + { + return $this->getSettingAs($key, Type::Float, $default); + } + + /** + * Get setting value and cast it to boolean. + * + * @param string $key + * @param mixed $default + * @return mixed + */ + public function getSettingBoolean(string $key, mixed $default = null): mixed + { + return $this->getSettingAs($key, Type::Boolean, $default); + } + + /** + * Get setting value and cast it to array. + * + * @param string $key + * @param mixed $default + * @return mixed + */ + public function getSettingArray(string $key, mixed $default = null): mixed + { + return $this->getSettingAs($key, Type::Array, $default); + } + + /** + * Get setting value and cast it to object. + * + * @param string $key + * @param mixed $default + * @return mixed + */ + public function getSettingObject(string $key, mixed $default = null): mixed + { + return $this->getSettingAs($key, Type::Object, $default); + } + + private function getSettingService(): SettingsService + { + return app(SettingsService::class); + } }