@@ -37,34 +37,40 @@ php artisan migrate
37
37
38
38
### Concepts
39
39
40
- > Preferences are defined by their name, and optionally grouped
41
-
40
+ > Preferences are defined by their name
41
+ >
42
42
> Each preference has at least a name and a caster. For additional validation you can add you custom Rule object
43
- >> The default caster supports all major primitives, including datetime/date and timestamp which get converted
44
- > > with ` Carbon/Carbon `
43
+ >> The default caster supports all major primitives, Enums, as well as time/ datetime/date and timestamp which get converted
44
+ >> with ` Carbon/Carbon `
45
45
46
46
### Create a Preference
47
47
#### single mode
48
48
``` php
49
- public function up(): void
49
+ public function up(): void
50
50
{
51
- PreferenceBuilder::init("language" )
51
+ PreferenceBuilder::init(Preferences::LANGUAGE )
52
52
->withDefaultValue("en")
53
- // optional ->withGroup('general')
54
53
->withRule(new InRule("en", "it", "de"))
55
54
->create();
56
55
57
- // Or
58
- PreferenceBuilder::init("language")->create()
59
-
56
+
57
+ // Or
58
+ PreferenceBuilder::init(Preferences::LANGUAGE)->create()
59
+ // different enums with the same value do not conflict
60
+ PreferenceBuilder::init(OtherPreferences::LANGUAGE)->create()
61
+
62
+ // update
63
+ PreferenceBuilder::init(Preferences::LANGUAGE)
64
+ ->withRule(new InRule("en", "it", "de"))
65
+ ->updateOrCreate()
66
+
67
+ // Discouraged, consider using Enums
68
+ PreferenceBuilder::init("language")->create()
60
69
}
61
70
62
- /**
63
- * Reverse the migrations.
64
- */
65
71
public function down(): void
66
72
{
67
- PreferenceBuilder::delete("language" );
73
+ PreferenceBuilder::delete(Preferences::LANGUAGE );
68
74
}
69
75
```
70
76
#### Bulk mode
@@ -98,9 +104,10 @@ return new class extends Migration {
98
104
public function preferences(): array
99
105
{
100
106
return [
101
- ['name' => 'language', 'cast' => Cast::STRING, 'default_value' => 'en', 'rule' => new InRule("en", "it", "de"), 'group' => 'general'],
102
- ['name' => 'theme', 'cast' => Cast::STRING, 'default_value' => 'light'],
103
- ['name' => 'configuration', 'cast' => Cast::ARRAY],
107
+ ['name' => Preferences::LANGUAGE, 'cast' => Cast::STRING, 'default_value' => 'en', 'rule' => new InRule("en", "it", "de")],
108
+ ['name' => Preferences::THEME, 'cast' => Cast::STRING, 'default_value' => 'light'],
109
+ ['name' => Preferences::CONFIGURATION, 'cast' => Cast::ARRAY],
110
+ ['name' => Preferences::CONFIGURATION, 'cast' => Cast::ARRAY],
104
111
];
105
112
}
106
113
};
@@ -111,33 +118,43 @@ return new class extends Migration {
111
118
112
119
> use the trait ` HasPreferences `
113
120
121
+ > string will be deprecated, consider sticking to ` UnitEnum `
122
+
114
123
``` php
115
124
// remove a preference, reverting it to the default value if set.
116
- public function removePreference(string $name, string $group = 'general' ): void
125
+ public function removePreference(UnitEnum| string $name, string $group = null ): void
117
126
118
127
// set / update a preference
119
- public function setPreference(string $name, mixed $value, string $group = 'general' ): void
128
+ public function setPreference(UnitEnum| string $name, mixed $value, string $group = null ): void
120
129
121
130
// collection of UserPreferences | optional filter by group
122
131
public function getPreferences(string $group = null): Collection
123
132
124
133
// get the value of the preference | if no value or default_value are found, returns $default
125
- public function getPreference(string $name, string $group = 'general' , mixed $default = null): mixed
134
+ public function getPreference(UnitEnum| string $name, string $group = null , mixed $default = null): mixed
126
135
```
127
136
128
137
### Examples
129
138
130
139
``` php
131
- $user->setPreference('language' ,"de");
132
- $user->getPreference('language' ); // 'de' as string
140
+ $user->setPreference(UserPreferences::LANGUAGE ,"de");
141
+ $user->getPreference(UserPreferences::LANGUAGE, ); // 'de' as string
133
142
134
- $user->setPreference('language' ,"fr");
143
+ $user->setPreference(UserPreferences::LANGUAGE, ,"fr");
135
144
// ValidationException because of the rule: ->withRule(new InRule("en","it","de"))
136
- $user->setPreference('language' ,2);
145
+ $user->setPreference(UserPreferences::LANGUAGE, ,2);
137
146
// ValidationException because of the cast: Cast::STRING
138
147
139
- $user->removePreference('language');
140
- $user->getPreference('language'); // 'en' as string
148
+ $user->removePreference(UserPreferences::LANGUAGE);
149
+ $user->getPreference(UserPreferences::LANGUAGE,); // 'en' as string
150
+
151
+ // Or with Enums
152
+ $user->setPreference(UserPreferences::LANGUAGE,"de");
153
+ $user->setPreference(UserPreferences::THEME,"light");
154
+ // get all of type UserPreferences
155
+ $user->getPreferences(UserPreferences::class)
156
+ //get all
157
+ $user->getPreferences()
141
158
```
142
159
143
160
## Casting
@@ -147,21 +164,21 @@ public function getPreference(string $name, string $group = 'general', mixed $de
147
164
148
165
### Available Casts
149
166
150
- INT, FLOAT, STRING, BOOL, ARRAY, TIME, DATE, DATETIME, TIMESTAMP
167
+ INT, FLOAT, STRING, BOOL, ARRAY, TIME, DATE, DATETIME, TIMESTAMP, BACKED_ENUM
151
168
152
169
### Custom Caster
153
170
154
171
create a ` BackedEnum ` , and implement ` CastableEnum `
155
172
156
173
``` php
157
- use Illuminate\Validation\Rule;
174
+ use Illuminate\Contracts\ Validation\Rule;
158
175
use Matteoc99\LaravelPreference\Contracts\CastableEnum;
159
176
160
177
enum MyCast: string implements CastableEnum
161
178
{
162
179
case TIMEZONE = 'tz';
163
180
164
- public function validation(): Rule|string
181
+ public function validation(): Rule|array| string
165
182
{
166
183
return match ($this) {
167
184
self::TIMEZONE => 'timezone:all',
@@ -214,10 +231,26 @@ class MyRule extends DataRule
214
231
->withRule(new MyRule("Europe","Asia"))
215
232
```
216
233
234
+ ## Deprecation plans
235
+
236
+ ### HasValidation for custom Rules
237
+ ` HasValidation ` will be deprecated in version >2.x, since Laravel is Deprecating the Rule Contract
238
+
239
+ ### string names
240
+ string ` name ` for preferences, will be removed in version >2.x, consider using enums, as its the direction this package will take.
241
+
242
+ ### groups
243
+ for preferences is deprecated and creating groups will be removed with version 2.x
244
+ > groups created with version 1.x will however be still supported.
245
+ >
246
+ > the intended use for groups is internal only in combination with enum names
247
+
217
248
## Test
218
249
219
250
` composer test <path> `
220
251
252
+ ` composer coverage `
253
+
221
254
## Security Vulnerabilities
222
255
223
256
Please review [ our security policy] ( SECURITY.md ) on how to report security vulnerabilities.
0 commit comments