Skip to content

Commit 2c73c39

Browse files
authored
Merge pull request #2 from webfox/name-in-to-array
Add enum value to array returned from toArray
2 parents f141818 + 35839ab commit 2c73c39

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

README.md

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,14 @@ return [
5151
]
5252
];
5353
```
54+
5455
You may then access these localized values using the `->label()` or `::labelFor()` methods
56+
5557
```php
5658
VolumeUnitEnum::MILLIGRAMS->label(); // "mg"
5759
VolumeUnitEnum::labelFor(VolumeUnitEnum::TONNE); // "t"
5860
```
61+
5962
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`
6063

6164
### Meta data
@@ -87,6 +90,7 @@ public function withMeta(): array
8790
};
8891
}
8992
```
93+
9094
If you do not specify a `withMeta` method, meta will be an empty array.
9195

9296
## Other methods
@@ -106,15 +110,17 @@ returns
106110
```php
107111
[
108112
[
109-
'value' => 'Milligrams',
113+
'name' => 'MILLIGRAMS'
114+
'value' => 'MILLIGRAMS',
110115
'label' => 'mg',
111116
'meta' => [
112117
'background_color' => 'bg-green-100',
113118
'text_color' => 'text-green-800',
114119
],
115120
],
116121
[
117-
'value' => 'Grams',
122+
'name' => 'GRAMS',
123+
'value' => 'GRAMS',
118124
'label' => 'g',
119125
'meta' => [
120126
'background_color' => 'bg-red-100',
@@ -125,6 +131,27 @@ returns
125131
]
126132
```
127133

134+
### names
135+
136+
Returns an array of all enum values.
137+
138+
#### Usage
139+
140+
```php
141+
VolumeUnitEnum::names();
142+
```
143+
144+
returns
145+
146+
```php
147+
[
148+
'MILLIGRAMS',
149+
'GRAMS',
150+
'KILOGRAMS',
151+
'TONNE',
152+
]
153+
```
154+
128155
### values
129156

130157
Returns an array of all enum values.
@@ -181,9 +208,9 @@ returns
181208

182209
```php
183210
[
184-
'MILLIGRAM'=>'mg',
185-
'GRAM' =>'g',
186-
'KILOGRAM' =>'kg',
211+
'MILLIGRAMS'=>'mg',
212+
'GRAMS' =>'g',
213+
'KILOGRAMS' =>'kg',
187214
'TONNE' =>'t',
188215
]
189216
```
@@ -202,7 +229,8 @@ returns
202229

203230
```php
204231
[
205-
'value' => 'Milligrams',
232+
'name' => 'MILLIGRAMS'
233+
'value' => 'MILLIGRAMS',
206234
'label' => 'mg',
207235
'meta' => [
208236
'color' => 'bg-green-100',
@@ -216,7 +244,8 @@ returns
216244
An alias for toArray.
217245

218246
### isA/isAn
219-
Allows you to check if an enum is a given value. Returns a boolean.
247+
248+
Allows you to check if an enum is a given value. Returns a boolean.
220249
> **Note**
221250
> `isAn` is just an alias for `isA`.
222251
@@ -228,6 +257,7 @@ VolumeUnitEnum::MILLIGRAMS->isA(VolumeUnitEnum::MILLIGRAMS); //true
228257
```
229258

230259
### isNotA/isNotAn
260+
231261
Allows you to check if an enum is not a given value. Returns a boolean.
232262
> **Note**
233263
> `isNotAn` is just an alias for `isNotA`.
@@ -240,6 +270,7 @@ VolumeUnitEnum::MILLIGRAMS->isA(VolumeUnitEnum::MILLIGRAMS); //false
240270
```
241271

242272
### isAny
273+
243274
Allows you to check if an enum is contained in an array. Returns a boolean.
244275

245276
#### Usage
@@ -250,6 +281,7 @@ VolumeUnitEnum::MILLIGRAMS->isAny([VolumeUnitEnum::GRAMS, VolumeUnitEnum::MILLIG
250281
```
251282

252283
### isNotAny
284+
253285
Allows you to check if an enum is not contained in an array. Returns a boolean.
254286

255287
#### Usage

src/IsBackedEnum.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,18 @@ public static function options(): array
2121
return array_map(fn($enum) => $enum->toArray(), self::cases());
2222
}
2323

24-
public static function values(): array
24+
public static function names(): array
2525
{
2626
static::ensureImplementsInterface();
2727
return array_map(fn($enum) => $enum->name, self::cases());
2828
}
2929

30+
public static function values(): array
31+
{
32+
static::ensureImplementsInterface();
33+
return array_map(fn($enum) => $enum->value, self::cases());
34+
}
35+
3036
public static function map(): array
3137
{
3238
static::ensureImplementsInterface();
@@ -72,7 +78,8 @@ public function toArray(): array
7278
{
7379
static::ensureImplementsInterface();
7480
return [
75-
'value' => $this->name,
81+
'name' => $this->name,
82+
'value' => $this->value,
7683
'label' => $this->label(),
7784
'meta' => $this->withMeta(),
7885
];

0 commit comments

Comments
 (0)