Skip to content

Commit b107bd6

Browse files
committed
added map function
1 parent 3db1717 commit b107bd6

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,28 @@ returns
157157
]
158158
```
159159

160+
### map
161+
162+
Returns an array of all enum values mapping to their label.
163+
164+
#### Usage
165+
166+
```php
167+
VolumeUnitEnum::map();
168+
```
169+
170+
returns
171+
172+
```php
173+
[
174+
'MILLIGRAM'=>'mg',
175+
'GRAM' =>'g',
176+
'KILOGRAM' =>'kg',
177+
'TONNE' =>'t',
178+
]
179+
```
180+
181+
160182
### labelFor
161183

162184
Returns the label for a given enum name.

src/IsBackedEnum.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ public static function values(): array
2727
return array_map(fn($enum) => $enum->name, self::cases());
2828
}
2929

30+
public static function map(): array
31+
{
32+
static::ensureImplementsInterface();
33+
$array = [];
34+
35+
foreach (self::cases() as $enum) {
36+
$array[$enum->name] = $enum->label();
37+
}
38+
39+
return $array;
40+
}
41+
3042
public static function labels(): array
3143
{
3244
static::ensureImplementsInterface();

0 commit comments

Comments
 (0)