@@ -26,7 +26,7 @@ This Laravel package aims to store and manage user settings/preferences in a sim
26
26
* [ Preference Building] ( #preference-building )
27
27
* [ Routing] ( #routing )
28
28
* [ Anantomy] ( #anantomy )
29
- * [ Example] ( #example )
29
+ * [ Example] ( #example- )
30
30
* [ Actions] ( #actions )
31
31
* [ Middlewares] ( #middlewares )
32
32
* [ Security] ( #security )
@@ -61,8 +61,11 @@ You can install the package via composer:
61
61
composer require matteoc99/laravel-preference
62
62
```
63
63
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
66
69
67
70
You can publish the config file with:
68
71
@@ -96,6 +99,7 @@ php artisan vendor:publish --tag="laravel-preference-config"
96
99
]
97
100
```
98
101
102
+ > [ !NOTE]
99
103
> Consider changing the base table names before running the migrations, if needed
100
104
101
105
Run the migrations with:
@@ -109,6 +113,7 @@ php artisan migrate
109
113
### Concepts
110
114
111
115
Each preference has at least a name and a caster. For additional validation you can add you custom Rule object
116
+ > [ !TIP]
112
117
> The default caster supports all major primitives, enums, objects, as well as time/datetime/date and timestamp which
113
118
> get converted with ` Carbon/Carbon `
114
119
@@ -244,6 +249,7 @@ Signature:
244
249
- $user the logged in user
245
250
- PolicyAction enum: the action the user wants to perform index/get/update/delete
246
251
252
+ > [ !NOTE]
247
253
> this is just the bare minimum regarding Authorization.
248
254
> For more fine-grained authorization checks refer to [ Policies] ( #policies )
249
255
@@ -291,8 +297,11 @@ class User extends \Illuminate\Foundation\Auth\User implements PreferenceableMod
291
297
292
298
## Casting
293
299
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
+ ```
296
305
297
306
### Available Casts
298
307
@@ -318,7 +327,12 @@ class User extends \Illuminate\Foundation\Auth\User implements PreferenceableMod
318
327
319
328
### Custom Caster
320
329
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:
322
336
323
337
``` php
324
338
use Illuminate\Contracts\Validation\ValidationRule;
@@ -357,7 +371,9 @@ enum MyCast: string implements CastableEnum
357
371
358
372
## Custom Rules
359
373
360
- > rules need to implement ` ValidationRule `
374
+ implement ` ValidationRule `
375
+
376
+ #### Example:
361
377
362
378
``` php
363
379
class MyRule implements ValidationRule
@@ -433,7 +449,8 @@ implement `PreferencePolicy` and the 4 methods defined by the contract
433
449
434
450
off by default, enable it in the config
435
451
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
437
454
438
455
### Anantomy:
439
456
@@ -458,7 +475,7 @@ which can all be accessed via the route name: {prefix}.{scope}.{group}.{index/ge
458
475
` group ` : A mapping of group names to their corresponding Enum classes. See config below
459
476
` scope ` : A mapping of scope names to their corresponding Eloquent model. See config below
460
477
461
- ### Example
478
+ ### Example:
462
479
463
480
``` php
464
481
'routes' => [
@@ -467,7 +484,7 @@ which can all be accessed via the route name: {prefix}.{scope}.{group}.{index/ge
467
484
'auth',
468
485
'user'=> 'verified'
469
486
],
470
- 'prefix' => 'my_preferences ',
487
+ 'prefix' => 'custom_prefix ',
471
488
'groups' => [
472
489
'general'=>\Matteoc99\LaravelPreference\Tests\TestSubjects\Enums\General::class
473
490
'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
478
495
]
479
496
```
480
497
481
- will result in:
498
+ will result in the following ** route names ** :
482
499
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
491
508
492
509
### Actions
493
510
511
+ > [ !NOTE]
512
+ > Examples are with scope ` user ` and group ` general `
513
+
514
+
494
515
#### INDEX
495
516
496
- (my_preferences.user.general.index)
497
- equivalent to: ` $user->getPreferences(General::class) `
498
517
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 '
502
523
503
- #### GET
504
524
505
- (my_preferences.user.general.get)
506
- equivalent to: ` $user->getPreference(General::{preference}) `
525
+ #### GET
507
526
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}
511
532
512
533
#### UPDATE
513
534
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
+ `
517
542
{
518
- 'value' => >value<
543
+ "value": >value<
519
544
}
545
+ `
546
+
520
547
521
- ``` shell
522
- curl -X PATCH ' https://example.com/my_preferences/user/{scope_id}/general/{preference}' \
523
- -d ' {"value": "new_value"}'
524
- ```
525
548
526
549
##### Enum Patching
527
550
528
551
When 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
531
554
532
555
then, when sending the value it varies:
533
556
534
557
- BackedEnum: send the value or the case
535
- - UnitBacked: send the case
558
+ - UnitEnum: send the case
559
+
560
+ Example:
536
561
537
- Example:
538
562
``` php
539
563
enum Theme
540
564
{
541
565
case LIGHT;
542
566
case DARK;
543
567
}
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}' \
545
569
-d '{"value": "DARK"}'
546
570
```
547
571
548
572
#### DELETE
549
573
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}
556
579
557
580
### Middlewares
558
581
@@ -567,9 +590,9 @@ in the config file
567
590
'user.general'=> 'verified' // scoped & grouped middleware only for a specific model + enum
568
591
],
569
592
```
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
573
596
574
597
## Security
575
598
0 commit comments