Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/Enums/Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace RuangDeveloper\LaravelSettings\Enums;

enum Type: string
{
case String = 'string';
case Integer = 'integer';
case Float = 'float';
case Boolean = 'boolean';
case Array = 'array';
case Object = 'object';
}
138 changes: 138 additions & 0 deletions src/Services/SettingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace RuangDeveloper\LaravelSettings\Services;

use Illuminate\Support\Facades\Cache;
use RuangDeveloper\LaravelSettings\Enums\Type;
use RuangDeveloper\LaravelSettings\Supports\Support;

class SettingsService
Expand Down Expand Up @@ -49,6 +50,114 @@ public function get(string $key, mixed $default = null): mixed
return $this->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.
*
Expand Down Expand Up @@ -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;
}
}
}
92 changes: 92 additions & 0 deletions src/Traits/HasSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}