generated from rawilk/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
121 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
title: Enums | ||
sort: 4 | ||
--- | ||
|
||
## Introduction | ||
|
||
PHP 8.1 introduced native enums, which can be a great way to define setting keys to use with this package. | ||
Using an enum instead of hard-coding your setting keys can be helpful for both keeping track of which | ||
settings are available to the application and for consistency. | ||
As of version `3.1.0` of the package, the settings service will support enums as setting keys in the `get()`, | ||
`set()`, `forget()`, and `has()` methods, as well as `isTrue()`, `isFalse()`, and `cacheKeyForSetting()`. | ||
|
||
To use an enum as a setting key, you must use a string backed enum, like the example below: | ||
|
||
```php | ||
namespace App\Enums; | ||
|
||
enum SettingKey: string | ||
{ | ||
case Timezone = 'app.timezone'; | ||
case DefaultRole = 'default-role'; | ||
} | ||
``` | ||
|
||
Now, you can use that enum when interacting with settings: | ||
|
||
```php | ||
settings()->get(SettingKey::Timezone); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rawilk\Settings\Exceptions; | ||
|
||
use InvalidArgumentException; | ||
|
||
final class InvalidEnumType extends InvalidArgumentException | ||
{ | ||
public static function make(string $enumClass): self | ||
{ | ||
return new self("Only string backed enums are supported. `{$enumClass}` is not a string backed enum."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rawilk\Settings\Tests\Support\Enums; | ||
|
||
enum IntBackedEnum: int | ||
{ | ||
case Foo = 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rawilk\Settings\Tests\Support\Enums; | ||
|
||
enum SettingKey: string | ||
{ | ||
case Timezone = 'app.timezone'; | ||
} |