Skip to content

Commit fcb244f

Browse files
authored
Merge pull request #4 from ruangdeveloper/feature/type-cast
feat: add type cast while get setting
2 parents 382640a + 5d9c4c7 commit fcb244f

File tree

4 files changed

+308
-0
lines changed

4 files changed

+308
-0
lines changed

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,39 @@ $siteTItle = Settings::get('site_title');
6969
$siteTitle = Settings::get('site_title', 'Your Default Awesome Website');
7070
```
7171

72+
**Get a setting and cast it to a specific type**
73+
74+
```php
75+
use RuangDeveloper\LaravelSettings\Enums\Type;
76+
use RuangDeveloper\LaravelSettings\Facades\Settings;
77+
78+
// retrieve the global site title
79+
$siteTitle = Settings::getAs('site_title', Type::String);
80+
81+
// you may want to add a default fallback value if the setting
82+
// with provided key doesn't exists in the database
83+
$siteTitle = Settings::getAs('site_title', Type::String, 'Your Default Awesome Website');
84+
```
85+
86+
Available types:
87+
88+
- String
89+
- Integer
90+
- Float
91+
- Boolean
92+
- Array
93+
- Object
94+
95+
You can also use the following methods to get a setting and cast it to a specific type:
96+
97+
- `Settings::getString('key', $default)`
98+
- `Settings::getInteger('key', $default)`
99+
- `Settings::getFloat('key', $default)`
100+
- `Settings::getBoolean('key', $default)`
101+
- `Settings::getArray('key', $default)`
102+
- `Settings::getObject('key', $default)`
103+
104+
72105
### Delete a global setting
73106

74107
Now, if you want to delete the setting
@@ -130,6 +163,38 @@ $isSubscribed = $user->getSetting('subscribe_newsletter', false);
130163
//
131164
```
132165

166+
**Get a setting and cast it to a specific type**
167+
168+
```php
169+
use RuangDeveloper\LaravelSettings\Enums\Type;
170+
use RuangDeveloper\LaravelSettings\Facades\Settings;
171+
172+
$user = App\Models\User::find(1);
173+
$isSubscribed = $user->getSettingAs('subscribe_newsletter', Type::Boolean);
174+
175+
// you may want to add a default fallback value if the setting
176+
// with provided key doesn't exists in the database
177+
$isSubscribed = $user->getSettingAs('subscribe_newsletter', Type::Boolean, false);
178+
```
179+
180+
Available types:
181+
182+
- String
183+
- Integer
184+
- Float
185+
- Boolean
186+
- Array
187+
- Object
188+
189+
You can also use the following methods to get a setting and cast it to a specific type:
190+
191+
- `$yourModel->getSettingString('key', $default)`
192+
- `$yourModel->getSettingInteger('key', $default)`
193+
- `$yourModel->getSettingFloat('key', $default)`
194+
- `$yourModel->getSettingBoolean('key', $default)`
195+
- `$yourModel->getSettingArray('key', $default)`
196+
- `$yourModel->getSettingObject('key', $default)`
197+
133198
**Delete a setting**
134199

135200
```php

src/Enums/Type.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace RuangDeveloper\LaravelSettings\Enums;
4+
5+
enum Type: string
6+
{
7+
case String = 'string';
8+
case Integer = 'integer';
9+
case Float = 'float';
10+
case Boolean = 'boolean';
11+
case Array = 'array';
12+
case Object = 'object';
13+
}

src/Services/SettingsService.php

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace RuangDeveloper\LaravelSettings\Services;
44

55
use Illuminate\Support\Facades\Cache;
6+
use RuangDeveloper\LaravelSettings\Enums\Type;
67
use RuangDeveloper\LaravelSettings\Supports\Support;
78

89
class SettingsService
@@ -49,6 +50,114 @@ public function get(string $key, mixed $default = null): mixed
4950
return $this->findSetting($key, null, null, $default);
5051
}
5152

53+
/**
54+
* Get a setting and cast it to a specific type.
55+
*
56+
* @param string $key
57+
* @param Type $type
58+
* @param mixed $default
59+
* @param string|null $modelType
60+
* @param mixed|null $modelId
61+
* @return mixed
62+
*/
63+
public function getAs(string $key, Type $type, mixed $default = null, string $modelType = null, mixed $modelId = null): mixed
64+
{
65+
$value = $default;
66+
if ($modelType && $modelId) {
67+
$value = $this->getWithModel($key, $modelType, $modelId, $default);
68+
} else {
69+
$value = $this->get($key, $default);
70+
}
71+
72+
if (is_null($value)) return $value;
73+
74+
return $this->cast($value, $type);
75+
}
76+
77+
/**
78+
* Get a setting and cast it to string
79+
*
80+
* @param string $key
81+
* @param mixed $default
82+
* @param string|null $modelType
83+
* @param mixed|null $modelId
84+
* @return mixed
85+
*/
86+
public function getString(string $key, mixed $default = null, string $modelType = null, mixed $modelId = null): mixed
87+
{
88+
return $this->getAs($key, Type::String, $default, $modelType, $modelId);
89+
}
90+
91+
/**
92+
* Get a setting and cast it to integer.
93+
*
94+
* @param string $key
95+
* @param mixed $default
96+
* @param string|null $modelType
97+
* @param mixed|null $modelId
98+
* @return mixed
99+
*/
100+
public function getInteger(string $key, mixed $default = null, string $modelType = null, mixed $modelId = null): mixed
101+
{
102+
return $this->getAs($key, Type::Integer, $default, $modelType, $modelId);
103+
}
104+
105+
/**
106+
* Get a setting and cast it to float.
107+
*
108+
* @param string $key
109+
* @param mixed $default
110+
* @param string|null $modelType
111+
* @param mixed|null $modelId
112+
* @return mixed
113+
*/
114+
public function getFloat(string $key, mixed $default = null, string $modelType = null, mixed $modelId = null): mixed
115+
{
116+
return $this->getAs($key, Type::Float, $default, $modelType, $modelId);
117+
}
118+
119+
/**
120+
* Get a setting and cast it to boolean.
121+
*
122+
* @param string $key
123+
* @param mixed $default
124+
* @param string|null $modelType
125+
* @param mixed|null $modelId
126+
* @return mixed
127+
*/
128+
public function getBoolean(string $key, mixed $default = null, string $modelType = null, mixed $modelId = null): mixed
129+
{
130+
return $this->getAs($key, Type::Boolean, $default, $modelType, $modelId);
131+
}
132+
133+
/**
134+
* Get a setting and cast it to array.
135+
*
136+
* @param string $key
137+
* @param mixed $default
138+
* @param string|null $modelType
139+
* @param mixed|null $modelId
140+
* @return mixed
141+
*/
142+
public function getArray(string $key, mixed $default = null, string $modelType = null, mixed $modelId = null): mixed
143+
{
144+
return $this->getAs($key, Type::Array, $default, $modelType, $modelId);
145+
}
146+
147+
/**
148+
* Get a setting and cast it to object.
149+
*
150+
* @param string $key
151+
* @param mixed $default
152+
* @param string|null $modelType
153+
* @param mixed|null $modelId
154+
* @return mixed
155+
*/
156+
public function getObject(string $key, mixed $default = null, string $modelType = null, mixed $modelId = null): mixed
157+
{
158+
return $this->getAs($key, Type::Object, $default, $modelType, $modelId);
159+
}
160+
52161
/**
53162
* Forget a setting value.
54163
*
@@ -229,4 +338,33 @@ private function deleteSetting(string $key, string $modelType = null, mixed $mod
229338
Cache::forget(Support::getCacheKey($key, $modelType, $modelId));
230339
}
231340
}
341+
342+
/**
343+
* Cast a value to a specific type.
344+
*
345+
* @param mixed $value
346+
* @param Type $type
347+
* @return mixed
348+
*/
349+
private function cast(mixed $value, Type $type): mixed
350+
{
351+
if (is_null($value)) return $value;
352+
353+
switch ($type) {
354+
case Type::String:
355+
return (string) $value;
356+
case Type::Integer:
357+
return (int) $value;
358+
case Type::Float:
359+
return (float) $value;
360+
case Type::Boolean:
361+
return (bool) $value;
362+
case Type::Array:
363+
return (array) $value;
364+
case Type::Object:
365+
return (object) $value;
366+
default:
367+
return $value;
368+
}
369+
}
232370
}

src/Traits/HasSettings.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace RuangDeveloper\LaravelSettings\Traits;
44

55
use Illuminate\Support\Facades\Cache;
6+
use RuangDeveloper\LaravelSettings\Enums\Type;
7+
use RuangDeveloper\LaravelSettings\Services\SettingsService;
68
use RuangDeveloper\LaravelSettings\Supports\Support;
79

810
trait HasSettings
@@ -131,4 +133,94 @@ public function deleteSetting(string $key): void
131133
Cache()->forget(Support::getCacheKey($key, $this->getMorphClass(), $this->getKey()));
132134
}
133135
}
136+
137+
/**
138+
* Get setting value and cast it to a specific type.
139+
*
140+
* @param string $key
141+
* @param Type
142+
* @param mixed $default
143+
* @return mixed
144+
*/
145+
public function getSettingAs(string $key, Type $type, mixed $default = null): mixed
146+
{
147+
return $this->getSettingService()->getAs($key, $type, $default, $this->getMorphClass(), $this->getKey());
148+
}
149+
150+
/**
151+
* Get setting value and cast it to string.
152+
*
153+
* @param string $key
154+
* @param mixed $default
155+
* @return mixed
156+
*/
157+
public function getSettingString(string $key, mixed $default = null): mixed
158+
{
159+
return $this->getSettingAs($key, Type::String, $default);
160+
}
161+
162+
/**
163+
* Get setting value and cast it to integer.
164+
*
165+
* @param string $key
166+
* @param mixed $default
167+
* @return mixed
168+
*/
169+
public function getSettingInteger(string $key, mixed $default = null): mixed
170+
{
171+
return $this->getSettingAs($key, Type::Integer, $default);
172+
}
173+
174+
/**
175+
* Get setting value and cast it to float.
176+
*
177+
* @param string $key
178+
* @param mixed $default
179+
* @return mixed
180+
*/
181+
public function getSettingFloat(string $key, mixed $default = null): mixed
182+
{
183+
return $this->getSettingAs($key, Type::Float, $default);
184+
}
185+
186+
/**
187+
* Get setting value and cast it to boolean.
188+
*
189+
* @param string $key
190+
* @param mixed $default
191+
* @return mixed
192+
*/
193+
public function getSettingBoolean(string $key, mixed $default = null): mixed
194+
{
195+
return $this->getSettingAs($key, Type::Boolean, $default);
196+
}
197+
198+
/**
199+
* Get setting value and cast it to array.
200+
*
201+
* @param string $key
202+
* @param mixed $default
203+
* @return mixed
204+
*/
205+
public function getSettingArray(string $key, mixed $default = null): mixed
206+
{
207+
return $this->getSettingAs($key, Type::Array, $default);
208+
}
209+
210+
/**
211+
* Get setting value and cast it to object.
212+
*
213+
* @param string $key
214+
* @param mixed $default
215+
* @return mixed
216+
*/
217+
public function getSettingObject(string $key, mixed $default = null): mixed
218+
{
219+
return $this->getSettingAs($key, Type::Object, $default);
220+
}
221+
222+
private function getSettingService(): SettingsService
223+
{
224+
return app(SettingsService::class);
225+
}
134226
}

0 commit comments

Comments
 (0)