@@ -26,7 +26,7 @@ This Laravel package aims to store and manage user settings/preferences in a sim
2626* [ Preference Building] ( #preference-building )
2727* [ Routing] ( #routing )
2828 * [ Anantomy] ( #anantomy )
29- * [ Example] ( #example )
29+ * [ Example] ( #example- )
3030 * [ Actions] ( #actions )
3131 * [ Middlewares] ( #middlewares )
3232* [ Security] ( #security )
@@ -61,8 +61,11 @@ You can install the package via composer:
6161composer require matteoc99/laravel-preference
6262```
6363
64- consider installing also ` graham-campbell/security-core:^4.0 ` to take advantage of xss cleaning.
65- see [ Security] ( #security ) for more information
64+ > [ !IMPORTANT]
65+ > consider installing also ` graham-campbell/security-core:^4.0 ` to take advantage of xss cleaning.
66+ > see [ Security] ( #security ) for more information
67+
68+ ### Configuration
6669
6770You can publish the config file with:
6871
@@ -96,6 +99,7 @@ php artisan vendor:publish --tag="laravel-preference-config"
9699 ]
97100```
98101
102+ > [ !NOTE]
99103> Consider changing the base table names before running the migrations, if needed
100104
101105Run the migrations with:
@@ -109,6 +113,7 @@ php artisan migrate
109113### Concepts
110114
111115Each preference has at least a name and a caster. For additional validation you can add you custom Rule object
116+ > [ !TIP]
112117> The default caster supports all major primitives, enums, objects, as well as time/datetime/date and timestamp which
113118> get converted with ` Carbon/Carbon `
114119
@@ -244,6 +249,7 @@ Signature:
244249- $user the logged in user
245250- PolicyAction enum: the action the user wants to perform index/get/update/delete
246251
252+ > [ !NOTE]
247253> this is just the bare minimum regarding Authorization.
248254> For more fine-grained authorization checks refer to [ Policies] ( #policies )
249255
@@ -291,8 +297,11 @@ class User extends \Illuminate\Foundation\Auth\User implements PreferenceableMod
291297
292298## Casting
293299
294- > set the cast when creating a Preference
295- >> PreferenceBuilder::init(Preferences::LANGUAGE, Cast::STRING)
300+ set the cast when creating a Preference
301+
302+ ``` php
303+ PreferenceBuilder::init(Preferences::LANGUAGE, Cast::STRING)
304+ ```
296305
297306### Available Casts
298307
@@ -318,7 +327,12 @@ class User extends \Illuminate\Foundation\Auth\User implements PreferenceableMod
318327
319328### Custom Caster
320329
321- create a ** string backed** enum, and implement ` CastableEnum `
330+ implement ` CastableEnum `
331+
332+ > [ !IMPORTANT]
333+ > The custom caster needs to be a ** string backed** enum
334+
335+ #### Example:
322336
323337``` php
324338use Illuminate\Contracts\Validation\ValidationRule;
@@ -357,7 +371,9 @@ enum MyCast: string implements CastableEnum
357371
358372## Custom Rules
359373
360- > rules need to implement ` ValidationRule `
374+ implement ` ValidationRule `
375+
376+ #### Example:
361377
362378``` php
363379class MyRule implements ValidationRule
@@ -433,7 +449,8 @@ implement `PreferencePolicy` and the 4 methods defined by the contract
433449
434450off by default, enable it in the config
435451
436- > Current limitation: it's not possible to set object casts via API
452+ > [ !WARNING]
453+ > ** (Current) limitation** : it's not possible to set object casts via API
437454
438455### Anantomy:
439456
@@ -458,7 +475,7 @@ which can all be accessed via the route name: {prefix}.{scope}.{group}.{index/ge
458475` group ` : A mapping of group names to their corresponding Enum classes. See config below
459476` scope ` : A mapping of scope names to their corresponding Eloquent model. See config below
460477
461- ### Example
478+ ### Example:
462479
463480``` php
464481 'routes' => [
@@ -467,7 +484,7 @@ which can all be accessed via the route name: {prefix}.{scope}.{group}.{index/ge
467484 'auth',
468485 'user'=> 'verified'
469486 ],
470- 'prefix' => 'my_preferences ',
487+ 'prefix' => 'custom_prefix ',
471488 'groups' => [
472489 'general'=>\Matteoc99\LaravelPreference\Tests\TestSubjects\Enums\General::class
473490 'video'=>\Matteoc99\LaravelPreference\Tests\TestSubjects\Enums\VideoPreferences::class
@@ -478,81 +495,87 @@ which can all be accessed via the route name: {prefix}.{scope}.{group}.{index/ge
478495 ]
479496```
480497
481- will result in:
498+ will result in the following ** route names ** :
482499
483- - my_preferences .user.general.index
484- - my_preferences .user.general.get
485- - my_preferences .user.general.update
486- - my_preferences .user.general.delete
487- - my_preferences .user.video.index
488- - my_preferences .user.video.get
489- - my_preferences .user.video.update
490- - my_preferences .user.video.delete
500+ - custom_prefix .user.general.index
501+ - custom_prefix .user.general.get
502+ - custom_prefix .user.general.update
503+ - custom_prefix .user.general.delete
504+ - custom_prefix .user.video.index
505+ - custom_prefix .user.video.get
506+ - custom_prefix .user.video.update
507+ - custom_prefix .user.video.delete
491508
492509### Actions
493510
511+ > [ !NOTE]
512+ > Examples are with scope ` user ` and group ` general `
513+
514+
494515#### INDEX
495516
496- (my_preferences.user.general.index)
497- equivalent to: ` $user->getPreferences(General::class) `
498517
499- ``` shell
500- curl -X GET ' https://example.com/my_preferences/user/{scope_id}/general'
501- ```
518+ - Route Name: custom_prefix.user.general.index
519+ - Url params: ` scope_id `
520+ - Equivalent to: ` $user->getPreferences(General::class) `
521+ - Http method: GET
522+ - Endpoint: 'https://your.domain/custom_prefix/user/{scope_id}/general '
502523
503- #### GET
504524
505- (my_preferences.user.general.get)
506- equivalent to: ` $user->getPreference(General::{preference}) `
525+ #### GET
507526
508- ``` shell
509- curl -X GET ' https://example.com/my_preferences/user/{scope_id}/general/{preference}'
510- ```
527+ - Route Name: custom_prefix.user.general.get
528+ - Url params: ` scope_id ` ,` preference `
529+ - Equivalent to: ` $user->getPreference(General::{preference}) `
530+ - Http method: GET
531+ - Endpoint: https://your.domain/custom_prefix/user/{scope_id}/general/{preference}
511532
512533#### UPDATE
513534
514- (my_preferences.user.general.update)
515- equivalent to: ` $user->setPreference(General::{preference}, >value<) `
516- Payload:
535+ - Route Name: custom_prefix.user.general.update
536+ - Url params: ` scope_id ` ,` preference `
537+ - Equivalent to: ` $user->setPreference(General::{preference}, >value<) `
538+ - Http method: PATCH/PUT
539+ - Endpoint: https://your.domain/custom_prefix/user/{scope_id}/general/{preference}
540+ - Payload:
541+ `
517542{
518- 'value' => >value<
543+ "value": >value<
519544}
545+ `
546+
520547
521- ``` shell
522- curl -X PATCH ' https://example.com/my_preferences/user/{scope_id}/general/{preference}' \
523- -d ' {"value": "new_value"}'
524- ```
525548
526549##### Enum Patching
527550
528551When creating your enum preference, add ` setAllowedClasses ` containing the possible enums to reconstruct the value
529- > ! if multiple cases are shared between enums, the first match is taken !
530- > -> consider having only one enum per preference to avoid overlaps
552+ > [ !CAUTION ]
553+ > if multiple cases are shared between enums, the first match is taken
531554
532555then, when sending the value it varies:
533556
534557- BackedEnum: send the value or the case
535- - UnitBacked: send the case
558+ - UnitEnum: send the case
559+
560+ Example:
536561
537- Example:
538562``` php
539563enum Theme
540564{
541565 case LIGHT;
542566 case DARK;
543567}
544- curl -X PATCH 'https://example.com/my_preferences /user/{scope_id}/general/{preference}' \
568+ curl -X PATCH 'https://your.domain/custom_prefix /user/{scope_id}/general/{preference}' \
545569 -d '{"value": "DARK"}'
546570```
547571
548572#### DELETE
549573
550- (my_preferences.user.general.delete)
551- equivalent to: ` $user->removePreference(General::{preference}) `
552-
553- ``` shell
554- curl -X DELETE ' https://example.com/my_preferences/user/{scope_id}/general/{preference}'
555- ```
574+ - Route Name: (custom_prefix.user.general.delete)
575+ - Url params: ` scope_id ` ,` preference `
576+ - Equivalent to: ` $user->removePreference(General::{preference}) `
577+ - Http method: DELETE
578+ - Endpoint: https://your.domain/custom_prefix/user/{scope_id}/general/{preference}
556579
557580### Middlewares
558581
@@ -567,9 +590,9 @@ in the config file
567590'user.general'=> 'verified' // scoped & grouped middleware only for a specific model + enum
568591],
569592```
570-
571- ** known Issues** : without the web middleware, you won't have access to the user via the Auth facade
572- since it's set by the middleware. Looking into an alternative
593+ > [ !CAUTION ]
594+ > ** known Issues** : without the web middleware, you won't have access to the user via the Auth facade
595+ > since it's set by the middleware. Looking into an alternative
573596
574597## Security
575598
0 commit comments