Skip to content

Commit c8724ca

Browse files
committed
Currently, Laravel would render the value as is.
This update will render the label provided from the translation files.
1 parent a543e7e commit c8724ca

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,14 @@ return [
5252
];
5353
```
5454

55-
You may then access these localized values using the `->label()` or `::labelFor()` methods
55+
You may then access these localized values using the `->label()` or `::labelFor()` methods.
56+
Additionally rendering the enum in a blade template will render the label.
5657

5758
```php
5859
VolumeUnitEnum::MILLIGRAMS->label(); // "mg"
5960
VolumeUnitEnum::labelFor(VolumeUnitEnum::TONNE); // "t"
61+
// in blade
62+
{{ VolumeUnitEnum::KILOGRAMS }} // "kg"
6063
```
6164

6265
If you do not specify a label in the lang file these methods will return the value assigned to the enum inside the enum file. e.g MILLIGRAMS label will be MILLIGRAMS
@@ -239,6 +242,30 @@ returns
239242
]
240243
```
241244

245+
### toHtml
246+
247+
An alias of ::label(). Used to satisfy Laravel's Htmlable interface.
248+
249+
#### Usage
250+
251+
```php
252+
VolumeUnitEnum::MILLIGRAMS->toHtml();
253+
```
254+
255+
returns
256+
257+
```php
258+
[
259+
'name' => 'MILLIGRAMS'
260+
'value' => 'MILLIGRAMS',
261+
'label' => 'mg',
262+
'meta' => [
263+
'color' => 'bg-green-100',
264+
'text_color' => 'text-green-800',
265+
],
266+
]
267+
```
268+
242269
### toJson
243270

244271
An alias for toArray.

phpstan.neon.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ parameters:
66
paths:
77
- src
88
- config
9-
- database
109
tmpDir: build/phpstan
1110
checkOctaneCompatibility: true
1211
checkModelProperties: true

src/BackedEnum.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Webfox\LaravelBackedEnums;
44

5-
interface BackedEnum extends \Illuminate\Contracts\Support\Arrayable, \Illuminate\Contracts\Support\Jsonable
5+
interface BackedEnum extends \Illuminate\Contracts\Support\Arrayable, \Illuminate\Contracts\Support\Htmlable, \Illuminate\Contracts\Support\Jsonable
66
{
77

88
}

src/IsBackedEnum.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
/**
7-
* @implements \Webfox\LaravelBackedEnums\BackedEnum;
7+
* @implements \Webfox\LaravelBackedEnums\BackedEnum<string,string>
88
*/
99
trait IsBackedEnum
1010
{
@@ -85,6 +85,12 @@ public function toArray(): array
8585
];
8686
}
8787

88+
public function toHtml(): string
89+
{
90+
static::ensureImplementsInterface();
91+
return $this->label();
92+
}
93+
8894
public function toJson($options = 0): array
8995
{
9096
static::ensureImplementsInterface();

0 commit comments

Comments
 (0)