@@ -15,10 +15,10 @@ composer require webfox/laravel-backed-enums
1515
1616## Usage
1717
18- ### Using the trait
18+ ### Setup your enum
1919
20- The enum you create must implement BackedEnum, this interface allows the toArray and toJson functions from within IsBackEnum to be called on your
21- enum.
20+ The enum you create must implement the ` BackedEnum ` interface and also use the ` IsBackedEnum ` trait.
21+ The interface is required for Laravel to cast your enum correctly and the trait is what gives your enum its superpowers .
2222
2323``` php
2424use Webfox\LaravelBackedEnums\BackedEnum;
@@ -43,43 +43,53 @@ Create enums.php lang file and create labels for your enum values.
4343// resources/lang/en/enums.php
4444
4545return [
46- VolumeUnitEnum::class => [
46+ VolumeUnitEnum::class => [
4747 VolumeUnitEnum::MILLIGRAMS->name => "mg",
4848 VolumeUnitEnum::GRAMS->name => "g",
4949 VolumeUnitEnum::KILOGRAMS->name => "kg",
5050 VolumeUnitEnum::TONNE->name => "t"
5151 ]
5252];
5353```
54+ You may then access these localized values using the ` ->label() ` or ` ::labelFor() ` methods
55+ ``` php
56+ VolumeUnitEnum::MILLIGRAMS->label(); // "mg"
57+ VolumeUnitEnum::labelFor(VolumeUnitEnum::TONNE); // "t"
58+ ```
59+ If you do not specify a label in the lang file these methods will return the translation key e.g. ` enums.\\App\\Enums\\VolumeUnitEnum.GRAMS `
5460
5561### Meta data
5662
5763Adding metadata allows you to return additional values alongside the label and values.
5864
59- Create a withMeta function on your enum to add metadata.
65+ Create a withMeta method on your enum to add metadata.
6066
6167``` php
6268public function withMeta(): array
63- {
64- return match ($this) {
65- self::MILLIGRAMS => [
66- 'background_color' => 'bg-green-100',
67- 'text_color' => 'text-green-800',
68- ],
69- self::GRAMS => [
70- 'background_color' => 'bg-red-100',
71- 'text_color' => 'text-red-800',
72- ],
73- self::KILOGRAMS, self::TONNE => [
74- 'background_color' => 'bg-gray-100',
75- 'text_color' => 'text-gray-800',
76- ],
77- default => throw new \Exception('Unexpected match value'),
78- };
79- }
69+ {
70+ return match ($this) {
71+ self::MILLIGRAMS => [
72+ 'background_color' => 'bg-green-100',
73+ 'text_color' => 'text-green-800',
74+ ],
75+ self::GRAMS => [
76+ 'background_color' => 'bg-red-100',
77+ 'text_color' => 'text-red-800',
78+ ],
79+ self::KILOGRAMS, self::TONNE => [
80+ 'background_color' => 'bg-gray-100',
81+ 'text_color' => 'text-gray-800',
82+ ],
83+ default => [
84+ 'background_color' => 'bg-blue-100',
85+ 'text_color' => 'text-blue-800',
86+ ],
87+ };
88+ }
8089```
90+ If you do not specify a ` withMeta ` method, meta will be an empty array.
8191
82- ## Other functions
92+ ## Other methods
8393
8494### options
8595
@@ -100,15 +110,15 @@ returns
100110 'label' => 'mg',
101111 'meta' => [
102112 'background_color' => 'bg-green-100',
103- 'text_color' => 'text-green-800',
113+ 'text_color' => 'text-green-800',
104114 ],
105115 ],
106116 [
107117 'value' => 'Grams',
108118 'label' => 'g',
109119 'meta' => [
110120 'background_color' => 'bg-red-100',
111- 'text_color' => 'text-red-800',
121+ 'text_color' => 'text-red-800',
112122 ],
113123 ...
114124 ]
@@ -129,10 +139,10 @@ returns
129139
130140``` php
131141[
132- 'Milligrams ',
133- 'Grams ',
134- 'Kilograms ',
135- 'Tonne ',
142+ 'MILLIGRAMS ',
143+ 'GRAMS ',
144+ 'KILOGRAMS ',
145+ 'TONNE ',
136146]
137147```
138148
@@ -178,40 +188,6 @@ returns
178188]
179189```
180190
181-
182- ### labelFor
183-
184- Returns the label for a given enum name.
185-
186- #### Usage
187-
188- ``` php
189- VolumeUnitEnum::labelFor(VolumeUnitEnum::MILLIGRAMS);
190- ```
191-
192- returns
193-
194- ``` php
195- 'mg'
196- ```
197-
198- ### label
199-
200- Returns the label for the current enum.
201-
202- #### Usage
203-
204- ``` php
205- VolumeUnitEnum::MILLIGRAMS->label();
206- ```
207-
208- returns
209-
210- ``` php
211- 'mg'
212- ```
213-
214-
215191### toArray
216192
217193Returns an array of a single enum value with its label and metadata.
@@ -239,33 +215,49 @@ returns
239215
240216An alias for toArray.
241217
242- ### isA
243- Allows you to check if an enum is a given value. Returns a boolean.
218+ ### isA/isAn
219+ Allows you to check if an enum is a given value. Returns a boolean.
220+ > ** Note**
221+ > ` isAn ` is just an alias for ` isA ` .
244222
245223#### Usage
246224
247225``` php
248- VolumeUnitEnum::MILLIGRAMS->isA(VolumeUnitEnum::GRAMS);
226+ VolumeUnitEnum::MILLIGRAMS->isA(VolumeUnitEnum::GRAMS); //false
227+ VolumeUnitEnum::MILLIGRAMS->isA(VolumeUnitEnum::MILLIGRAMS); //true
249228```
250229
251- #### Negation
252- The negated method also exists. Being isNot
230+ ### isNotA/isNotAn
231+ Allows you to check if an enum is not a given value. Returns a boolean.
232+ > ** Note**
233+ > ` isNotAn ` is just an alias for ` isNotA ` .
253234
235+ #### Usage
254236
255- ### isAn
256- Alias for isA.
237+ ``` php
238+ VolumeUnitEnum::MILLIGRAMS->isA(VolumeUnitEnum::GRAMS); //true
239+ VolumeUnitEnum::MILLIGRAMS->isA(VolumeUnitEnum::MILLIGRAMS); //false
240+ ```
257241
258242### isAny
259243Allows you to check if an enum is contained in an array. Returns a boolean.
260244
261245#### Usage
262246
263247``` php
264- VolumeUnitEnum::MILLIGRAMS->isAny([VolumeUnitEnum::GRAMS, VolumeUnitEnum::TONNE]);
248+ VolumeUnitEnum::MILLIGRAMS->isAny([VolumeUnitEnum::GRAMS, VolumeUnitEnum::TONNE]); // false
249+ VolumeUnitEnum::MILLIGRAMS->isAny([VolumeUnitEnum::GRAMS, VolumeUnitEnum::MILLIGRAMS]); // true
265250```
266251
267- #### Negation
268- The negated method also exists. Being isNotAny
252+ ### isNotAny
253+ Allows you to check if an enum is not contained in an array. Returns a boolean.
254+
255+ #### Usage
256+
257+ ``` php
258+ VolumeUnitEnum::MILLIGRAMS->isAny([VolumeUnitEnum::GRAMS, VolumeUnitEnum::TONNE]); // true
259+ VolumeUnitEnum::MILLIGRAMS->isAny([VolumeUnitEnum::GRAMS, VolumeUnitEnum::MILLIGRAMS]); // false
260+ ```
269261
270262## Changelog
271263
0 commit comments