Skip to content

Commit eee2ab6

Browse files
committed
- added definition of description(), descriptions(), descriptionsByName(), descriptionsByValue() to improve code completion and static analysis
- removed all methods `***AsSelect() - removed `NotBackedEnum` exception - added support on `***ByValue()` methods also for pure enum using name instead value
1 parent 23f83d1 commit eee2ab6

File tree

7 files changed

+112
-105
lines changed

7 files changed

+112
-105
lines changed

README.md

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
![Enum Helper-Dark](branding/laravel-dark.png#gh-dark-mode-only)![Enum Helper-Light](branding/laravel-light.png#gh-light-mode-only)
22
# Laravel Enum Helper
33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/datomatic/laravel-enum-helper.svg?style=for-the-badge)](https://packagist.org/packages/datomatic/laravel-enum-helper)
4-
[![Pest Tests number](https://img.shields.io/static/v1?label=%23tests&message=92&color=FF88FA&style=for-the-badge&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAMAAABF0y+mAAABiVBMVEUAAAD/iPv9yP3Xm+j/mP//wfVj67Je6bP/h/pVx6p6175d57WQycf+iPn/iPrsnezArd3+t/qpvNJd6LP/jPpu6rv/lPr/kPpc57T/rvtc57Np6rj3oPl37cL/tfn/wv9d6brX//L/g/rYn+n/gvrWm+di6LX+jPrskfGWzMpt6bln4bdd57Jk6LWSycj+vPquwNVo6rde6bP7nvvYnup91b/+vfv/lvtc57OqvNTFs9//t/td57L9t/r/iPpd6LPapej/ovp26bxy67v9lfld6LJr4Ljwsvb/xv3/jv39zv1t6buG5cTDreH5ivlc5rJy676V4cxb57D/y/h50MOy4OCUxcVa77X/iPpe6LP/jP+pu9L8t///tvuQycfArNxp6LzArd151r7/i/9n4bb/j/9e6rT/ifr7ifrskvLYnuhi87tg8blg7bf/vv//lP+wxNtj9b3/qv//oP/+ivz/l/r8ifryn/fvlPTfpPDeofDKtujHtOWX1NF/4seC3cR82sFu7cBo5LiMwPMrAAAAWHRSTlMA/Wv8FAIC/dME/Wj+3tEG/Pv798G1oHRjS0k1LBsWDgsJ/v36+fTy8ezn4+Lh29XNzMzLysLAwLSwr66opJqakY+Ni4J7end0bGlpY11XU048KicmIR8fizl+vwAAAVdJREFUKM9tz2VXAlEQgOFBBURpkE67u7u7E1YFYQl1SbvrlztDiLvss+fc/fCemXMvAEhhKqU759P1rLoxUDUyEh9fPH0z7ALiVrEY+SSNtxNS2upouYv7hOL191aKVsZHUTgbnQPQgDkq4ctHdoQmTWmW4WFzlVUDVpNKXf2fWpWbZIwUq/hcmjWGYnSa1pZZjEoomrEdVAisD7CX6GEb40rqTODxCj21OjDOvjRV8l2jhudBDchg/FUbDIZCITzwQyH6a9+2AMDbm9GfltFnxgAdtQWUgQJl4VQq37uPcSnsfYZzav6Ew18fQ4fUYPM7Qn4uSiIdyx5saJ6T+/3+5KSltshicwI2UpfAKE/aoARTvnn7KMYMdlAUyWRSyHN2JeU42HlCi4TszTHcmuj3iMVdP5JzoyAWNzi6T3ZGrMFCliK3BAqRSC/B2+6IxvYYNcO+2Npfv+yFi10LfBUAAAAASUVORK5CYII=)](https://github.com/datomatic/laravel-enum-helper/tree/main/tests)
4+
[![Pest Tests number](https://img.shields.io/static/v1?label=%23tests&message=90&color=FF88FA&style=for-the-badge&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAMAAABF0y+mAAABiVBMVEUAAAD/iPv9yP3Xm+j/mP//wfVj67Je6bP/h/pVx6p6175d57WQycf+iPn/iPrsnezArd3+t/qpvNJd6LP/jPpu6rv/lPr/kPpc57T/rvtc57Np6rj3oPl37cL/tfn/wv9d6brX//L/g/rYn+n/gvrWm+di6LX+jPrskfGWzMpt6bln4bdd57Jk6LWSycj+vPquwNVo6rde6bP7nvvYnup91b/+vfv/lvtc57OqvNTFs9//t/td57L9t/r/iPpd6LPapej/ovp26bxy67v9lfld6LJr4Ljwsvb/xv3/jv39zv1t6buG5cTDreH5ivlc5rJy676V4cxb57D/y/h50MOy4OCUxcVa77X/iPpe6LP/jP+pu9L8t///tvuQycfArNxp6LzArd151r7/i/9n4bb/j/9e6rT/ifr7ifrskvLYnuhi87tg8blg7bf/vv//lP+wxNtj9b3/qv//oP/+ivz/l/r8ifryn/fvlPTfpPDeofDKtujHtOWX1NF/4seC3cR82sFu7cBo5LiMwPMrAAAAWHRSTlMA/Wv8FAIC/dME/Wj+3tEG/Pv798G1oHRjS0k1LBsWDgsJ/v36+fTy8ezn4+Lh29XNzMzLysLAwLSwr66opJqakY+Ni4J7end0bGlpY11XU048KicmIR8fizl+vwAAAVdJREFUKM9tz2VXAlEQgOFBBURpkE67u7u7E1YFYQl1SbvrlztDiLvss+fc/fCemXMvAEhhKqU759P1rLoxUDUyEh9fPH0z7ALiVrEY+SSNtxNS2upouYv7hOL191aKVsZHUTgbnQPQgDkq4ctHdoQmTWmW4WFzlVUDVpNKXf2fWpWbZIwUq/hcmjWGYnSa1pZZjEoomrEdVAisD7CX6GEb40rqTODxCj21OjDOvjRV8l2jhudBDchg/FUbDIZCITzwQyH6a9+2AMDbm9GfltFnxgAdtQWUgQJl4VQq37uPcSnsfYZzav6Ew18fQ4fUYPM7Qn4uSiIdyx5saJ6T+/3+5KSltshicwI2UpfAKE/aoARTvnn7KMYMdlAUyWRSyHN2JeU42HlCi4TszTHcmuj3iMVdP5JzoyAWNzi6T3ZGrMFCliK3BAqRSC/B2+6IxvYYNcO+2Npfv+yFi10LfBUAAAAASUVORK5CYII=)](https://github.com/datomatic/laravel-enum-helper/tree/main/tests)
55
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/datomatic/laravel-enum-helper/run-tests?label=tests&color=5FE8B3&style=for-the-badge&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAMAAABF0y+mAAABiVBMVEUAAAD/iPv9yP3Xm+j/mP//wfVj67Je6bP/h/pVx6p6175d57WQycf+iPn/iPrsnezArd3+t/qpvNJd6LP/jPpu6rv/lPr/kPpc57T/rvtc57Np6rj3oPl37cL/tfn/wv9d6brX//L/g/rYn+n/gvrWm+di6LX+jPrskfGWzMpt6bln4bdd57Jk6LWSycj+vPquwNVo6rde6bP7nvvYnup91b/+vfv/lvtc57OqvNTFs9//t/td57L9t/r/iPpd6LPapej/ovp26bxy67v9lfld6LJr4Ljwsvb/xv3/jv39zv1t6buG5cTDreH5ivlc5rJy676V4cxb57D/y/h50MOy4OCUxcVa77X/iPpe6LP/jP+pu9L8t///tvuQycfArNxp6LzArd151r7/i/9n4bb/j/9e6rT/ifr7ifrskvLYnuhi87tg8blg7bf/vv//lP+wxNtj9b3/qv//oP/+ivz/l/r8ifryn/fvlPTfpPDeofDKtujHtOWX1NF/4seC3cR82sFu7cBo5LiMwPMrAAAAWHRSTlMA/Wv8FAIC/dME/Wj+3tEG/Pv798G1oHRjS0k1LBsWDgsJ/v36+fTy8ezn4+Lh29XNzMzLysLAwLSwr66opJqakY+Ni4J7end0bGlpY11XU048KicmIR8fizl+vwAAAVdJREFUKM9tz2VXAlEQgOFBBURpkE67u7u7E1YFYQl1SbvrlztDiLvss+fc/fCemXMvAEhhKqU759P1rLoxUDUyEh9fPH0z7ALiVrEY+SSNtxNS2upouYv7hOL191aKVsZHUTgbnQPQgDkq4ctHdoQmTWmW4WFzlVUDVpNKXf2fWpWbZIwUq/hcmjWGYnSa1pZZjEoomrEdVAisD7CX6GEb40rqTODxCj21OjDOvjRV8l2jhudBDchg/FUbDIZCITzwQyH6a9+2AMDbm9GfltFnxgAdtQWUgQJl4VQq37uPcSnsfYZzav6Ew18fQ4fUYPM7Qn4uSiIdyx5saJ6T+/3+5KSltshicwI2UpfAKE/aoARTvnn7KMYMdlAUyWRSyHN2JeU42HlCi4TszTHcmuj3iMVdP5JzoyAWNzi6T3ZGrMFCliK3BAqRSC/B2+6IxvYYNcO+2Npfv+yFi10LfBUAAAAASUVORK5CYII=)](https://github.com/datomatic/laravel-enum-helper/actions/workflows/run-tests.yml)
66
[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/datomatic/laravel-enum-helper/Check%20&%20fix%20styling?label=code%20style&color=5FE8B3&style=for-the-badge)](https://github.com/datomatic/laravel-enum-helper/actions/workflows/php-cs-fixer.yml)
77
[![Total Downloads](https://img.shields.io/packagist/dt/datomatic/laravel-enum-helper.svg?style=for-the-badge)](https://packagist.org/packages/datomatic/laravel-enum-helper)
88

99
This is an extension of the [datomatic/enum-helper](https://github.com/datomatic/enum-helper) package based on Laravel Framework.
10-
The package consists on a `LaravelEnumHelper` trait that extends `EnumHelper` (`EnumInvokable`, `EnumFroms`, `EnumNames`,
11-
`EnumValues`, `EnumEquality`) and add dynamic methods to return a translation or "property" method and
10+
The package consists on a `LaravelEnumHelper` trait that extends `EnumInvokable`, `EnumFroms`, `EnumNames`,
11+
`EnumValues`, `EnumEquality`, `EnumDescription` and add dynamic methods to return a translation or "property" method and
1212
relative helper methods.
1313

1414
You can think about it as an [`EnumDescription` trait](https://github.com/datomatic/enum-helper#descriptions-and-translations) but completely dynamic.
1515

16-
So you can define a custom method and have these functions available: `[method]s()`, `[method]sByName()`,
17-
`[method]sByValue()`, `[method]sAsSelect()`. For example with `color()` you obtain `colors()`, `colorsByName()`, `colorsByValue()`,
18-
`colorsAsSelect()` methods.
16+
So you can define a custom `method()` and have these functions available: `[method]s()`, `[method]sByName()`,
17+
`[method]sByValue()`. For example with `color()` you obtain `colors()`, `colorsByName()`, `colorsByValue()` methods.
1918

2019
The cool thing is you can also avoid writing the method and write only the translations.
21-
For example, you can define the property `excerpt` by writing the translations on enums.php [(see below for explanation)](#translations)
22-
and obtain `excerpt()`, `excerpts()`, `excerptsByName()`, `excerptsByValue()`, `excerptsAsSelect()` methods.
20+
For example, you can define the property `excerpt` by writing only the translations on enums.php [(see below for explanation)](#translations)
21+
and obtain `excerpt()`, `excerpts()`, `excerptsByName()`, `excerptsByValue()` methods.
2322

2423
The package use the [Laravel `Pluralizer` component](https://laravel.com/docs/localization#pluralization-language) to get the singular method to call or to translate.
2524

@@ -30,10 +29,10 @@ You should see the `datomatic/enum-helper` details on his [repository](https://g
3029
- **Invokable cases**: get the value of enum "invoking" it statically
3130
- **Construct enum by name or value**: `from()`, `tryFrom()`, `fromName()`, `tryFromName()` for all enums
3231
- **Enums Equality**: `is()`, `isNot()`, `in()`, `notIn()` methods
33-
- **Names**: methods to have a list of case names (`names()`, `namesByValue()`, `namesAsSelect()`)
32+
- **Names**: methods to have a list of case names (`names()`, `namesByValue()`)
3433
- **Values**: methods to have a list of case values (`values()`, `valuesByName()`)
3534
- **Unique ID**: get an unique identifier from instance or instance from identifier (`uniqueId()`, `fromUniqueId()`)
36-
- **Descriptions & Translations**: add description to enum with optional translation (`description()`,`descriptions()`,`descriptionsByName()`,`descriptionsByValue()`,`descriptionsAsSelect()`)
35+
- **Descriptions & Translations**: add description to enum with optional translation (`description()`,`descriptions()`,`descriptionsByName()`,`descriptionsByValue()`)
3736

3837

3938
## Installation
@@ -202,38 +201,21 @@ Status::descriptionsByName([Status::DISCARDED, Status::NO_RESPONSE], 'it'); //
202201
```
203202

204203
## static `[property]sByValue()` method
205-
This dynamic method returns an associative array of [case value => `property()` result] on `BackedEnum`, throw `NotBackedEnum` exception otherwise.
204+
This dynamic method returns an associative array of [case value => `property()` result] on `BackedEnum`, [case name => `property()` result] otherwise.
206205
The name of the method is the plural of the property so if you are using `property` it will be `propertiesByValue()`.
207206

208207
```php
208+
Status::descriptionsByValue(); // ['PENDING' => 'Await decision', 'ACCEPTED' => 'Recognized valid',...
209209
StatusString::descriptionsByValue(); // ['P' => 'Await decision', 'A' => 'Recognized valid',...
210210
StatusString::colorsByValue(); // ['P' => '#000000','A' => '#0000FF',...
211-
Status::descriptionsByValue(); // throw `NotBackedEnum` exception
212211
// Subset
212+
Status::descriptionsByValue([Status::PENDING, Status::DISCARDED]); // ['PENDING' => 'Await decision', 'DISCARDED' => 'No longer useful']
213213
StatusString::descriptionsByValue([StatusString::DISCARDED, StatusString::ACCEPTED]); // ['D' => 'No longer useful', 'A' => 'Recognized valid']
214214
StatusString::colorsByValue([[Status::PENDING, Status::DISCARDED]); // ['P' => '#000000', 'D' => '#FF0000']
215215
// Forcing language
216216
StatusString::descriptionsByValue(null, 'it'); // 🇮🇹 ['P' => 'In attesa','A' => 'Accettato',...
217217
// Subset and language
218-
StatusString::descriptionsByValue([StatusString::DISCARDED, StatusString::NO_RESPONSE], 'it'); // 🇮🇹 ['D' => 'Rifiutato','N' => 'Nessuna Risposta',...
219-
```
220-
221-
## static `[property]sAsSelect()` method
222-
This dynamic method returns an associative array of [case value => `property()` result] on `BackedEnum`, [case name => `property()` result] otherwise.
223-
The name of the method is the plural of the property so if you are using `property` it will be `propertiesAsSelect()`.
224-
225-
```php
226-
StatusString::descriptionsAsSelect(); // ['P' => 'Await decision', 'A' => 'Recognized valid',...
227-
StatusString::colorsAsSelect(); // ['P' => '#000000','A' => '#0000FF',...
228-
Status::descriptionsAsSelect(); // ['PENDING' => 'Await decision', 'ACCEPTED' => 'Recognized valid',...
229-
// Subset
230-
Status::descriptionsAsSelect([Status::PENDING, Status::DISCARDED]); // ['PENDING' => 'Await decision', 'DISCARDED' => 'No longer useful']
231-
StatusString::colorsAsSelect([StatusString::PENDING, StatusString::DISCARDED]); // ['P' => '#000000', 'D' => '#FF0000']
232-
// Forcing language
233-
StatusString::descriptionsAsSelect(null, 'it'); // 🇮🇹 ['P' => 'In attesa','A' => 'Accettato',...
234-
Status::descriptionsAsSelect(null, 'it'); // 🇮🇹 ['PENDING' => 'In attesa', 'ACCEPTED' => 'Accettato',...
235-
// Subset and language
236-
Status::descriptionsAsSelect([Status::DISCARDED, Status::NO_RESPONSE], 'it'); // 🇮🇹 ['DISCARDED' => 'Rifiutato','NO_RESPONSE' => 'Nessuna Risposta',...
218+
Status::descriptionsByValue([StatusString::DISCARDED, StatusString::NO_RESPONSE], 'it'); // 🇮🇹 ['DISCARDED' => 'Rifiutato','NO_RESPONSE' => 'Nessuna Risposta',...
237219
```
238220

239221
## Laravel Nova Enum Field

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
},
2222
"require": {
2323
"php": "^8.1",
24-
"datomatic/enum-helper": "^0.6.0",
24+
"datomatic/enum-helper": "^0.7.0",
2525
"illuminate/translation": "^8.0|^9.0",
2626
"illuminate/support": "^8.0|^9.0"
2727
},

src/LaravelEnumHelper.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
use Datomatic\EnumHelper\EnumHelper;
88
use Datomatic\EnumHelper\Exceptions\UndefinedCase;
9-
use Datomatic\EnumHelper\Traits\EnumProperties;
9+
use Datomatic\EnumHelper\Traits\EnumDescription;
1010
use Datomatic\LaravelEnumHelper\Exceptions\TranslationMissing;
1111
use Illuminate\Support\Str;
1212

1313
trait LaravelEnumHelper
1414
{
15-
use EnumProperties;
15+
use EnumDescription;
1616
use EnumHelper {
1717
__callStatic as callStatic;
1818
}
@@ -22,20 +22,28 @@ trait LaravelEnumHelper
2222
*/
2323
public function __call(string $method, array $parameters): string
2424
{
25-
$translateUniquePath = self::translateUniquePath($method, $this());
25+
$translation = __(self::translateUniquePath($method, $this()), [], $parameters[0] ?? null);
2626

27-
$translation = __($translateUniquePath, [], $parameters[0] ?? null);
28-
29-
if ($method === 'description'
30-
&& Str::of($translation)->startsWith($translateUniquePath)) {
31-
$translation = __(self::translateUniqueFallbackPath($this()), [], $parameters[0] ?? null);
27+
if (Str::of($translation)->startsWith(self::translateBaseUniquePath())) {
28+
throw new TranslationMissing($this, $method);
3229
}
3330

34-
if (Str::of($translation)->startsWith(self::translateBaseUniquePath())) {
35-
if ($method === 'description') {
31+
return $translation;
32+
}
33+
34+
/**
35+
* Description translation method with double fallback.
36+
*/
37+
public function description(?string $lang = null): string
38+
{
39+
try {
40+
$translation = $this->__call('description', [$lang]);
41+
} catch (TranslationMissing) {
42+
$translation = __(self::translateUniqueFallbackPath($this()), [], $lang);
43+
44+
if (Str::of($translation)->startsWith(self::translateBaseUniquePath())) {
3645
return self::humanize($this->name);
3746
}
38-
throw new TranslationMissing($this, $method);
3947
}
4048

4149
return $translation;
@@ -83,10 +91,6 @@ public static function __callStatic(string $method, array $parameters): int|stri
8391
return static::dynamicByKey('value', $singularMethod, ...$args);
8492
}
8593

86-
if ($singularMethod = self::getSingularIfEndsWith($method, 'AsSelect')) {
87-
return static::dynamicAsSelect($singularMethod, ...$args);
88-
}
89-
9094
if ($singularMethod = Str::singular($method)) {
9195
return static::dynamicList($singularMethod, ...$args);
9296
}

tests/Feature/LaravelEnumHelperTest.php

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Datomatic\LaravelEnumHelper\Exceptions\TranslationMissing;
66
use Datomatic\LaravelEnumHelper\Tests\Support\Enums\Status;
7+
use Datomatic\LaravelEnumHelper\Tests\Support\Enums\StatusFallbackLocale;
78
use Datomatic\LaravelEnumHelper\Tests\Support\Enums\StatusInt;
89
use Datomatic\LaravelEnumHelper\Tests\Support\Enums\StatusPascalCase;
910
use Datomatic\LaravelEnumHelper\Tests\Support\Enums\StatusPascalCaseWithoutTranslation;
@@ -43,8 +44,8 @@
4344
expect($enum->description())->toBe($result);
4445
})->with([
4546
'translation' => [StatusString::NO_RESPONSE, 'No response'],
46-
'fallback translation' => [Status::PENDING, 'Await decision'],
47-
'method definition' => [StatusInt::NO_RESPONSE, 'No response'],
47+
'fallback translation' => [StatusFallbackLocale::PENDING, 'FB Await decision'],
48+
'method definition' => [StatusPascalCase::NoResponse, 'PC No response'],
4849
]);
4950

5051
it('has correct translation text passing lang', function ($enum, $lang, $result) {
@@ -54,11 +55,11 @@
5455
'ita translation string backed enum' => [StatusString::NO_RESPONSE, 'it', 'Nessuna Risposta'],
5556
'eng translation int backed enum' => [StatusInt::PENDING, 'en', 'Await decision'],
5657
'ita translation int backed enum' => [StatusInt::NO_RESPONSE, 'it', 'Nessuna Risposta'],
57-
'ita fallback locale translation' => [StatusPascalCase::NoResponse, 'it', 'No response'],
58+
'ita fallback locale translation' => [StatusFallbackLocale::NO_RESPONSE, 'it', 'FB No response'],
5859
'eng fallback translation' => [Status::PENDING, 'en', 'Await decision'],
5960
'ita fallback translation' => [Status::PENDING, 'it', 'In attesa'],
60-
'eng method' => [StatusPascalCase::Pending, 'en', 'Await decision'],
61-
'ita method' => [StatusPascalCase::Pending, 'it', 'Await decision'],
61+
'eng method' => [StatusPascalCase::Pending, 'en', 'PC Await decision'],
62+
'ita method' => [StatusPascalCase::Pending, 'it', 'PC Await decision'],
6263
]);
6364

6465
it('can has correct translation with method name both singular and plural', function ($enum, $lang, $result) {
@@ -259,15 +260,27 @@
259260
it('can return an associative array of translations [value => translations] with params', function ($enumClass, $cases, $lang, $result) {
260261
expect($enumClass::descriptionsByValue($cases, $lang))->toBe($result);
261262
})->with([
262-
'All string backed Enum cases into it ' => [StatusString::class, null, 'it', [
263-
'P' => 'In attesa',
264-
'A' => 'Accettato',
265-
'D' => 'Rifiutato',
266-
'N' => 'Nessuna Risposta',
263+
'Pure Enum' => [Status::class, null, null, [
264+
'PENDING' => 'Await decision',
265+
'ACCEPTED' => 'Recognized valid',
266+
'DISCARDED' => 'No longer useful',
267+
'NO_RESPONSE' => 'No response',
267268
]],
268-
'Some string backed Enum cases into it ' => [StatusString::class, [StatusString::NO_RESPONSE, StatusString::NO_RESPONSE], 'it', [
269-
'N' => 'Nessuna Risposta',
269+
'Pure Enum subset' => [Status::class, [Status::PENDING, Status::DISCARDED], 'it', [
270+
'PENDING' => 'In attesa',
271+
'DISCARDED' => 'Rifiutato',
270272
]],
273+
'Backed Enum' => [StatusString::class, null, null, [
274+
'P' => 'Await decision',
275+
'A' => 'Recognized valid',
276+
'D' => 'No longer useful',
277+
'N' => 'No response',
278+
]],
279+
'Backed Enum subset' => [StatusInt::class,
280+
[StatusInt::DISCARDED, StatusInt::ACCEPTED], 'it', [
281+
2 => 'Rifiutato',
282+
1 => 'Accettato',
283+
], ],
271284
]);
272285

273286
it('can return an associative array of magic translations [value => translations] with method name both singular and plural', function ($enumClass, $cases, $lang, $result) {
@@ -339,34 +352,8 @@
339352
]],
340353
]);
341354

342-
it('can return an associative array [value/name => translation]', function ($className, $cases, $lang, $values) {
343-
expect($className::descriptionsAsSelect($cases, $lang))->toBe($values);
344-
})->with([
345-
'Pure Enum' => [Status::class, null, null, [
346-
'PENDING' => 'Await decision',
347-
'ACCEPTED' => 'Recognized valid',
348-
'DISCARDED' => 'No longer useful',
349-
'NO_RESPONSE' => 'No response',
350-
]],
351-
'Pure Enum subset' => [Status::class, [Status::PENDING, Status::DISCARDED], 'it', [
352-
'PENDING' => 'In attesa',
353-
'DISCARDED' => 'Rifiutato',
354-
]],
355-
'Backed Enum' => [StatusString::class, null, null, [
356-
'P' => 'Await decision',
357-
'A' => 'Recognized valid',
358-
'D' => 'No longer useful',
359-
'N' => 'No response',
360-
]],
361-
'Backed Enum subset' => [StatusString::class,
362-
[StatusString::DISCARDED, StatusString::ACCEPTED], 'it', [
363-
'D' => 'Rifiutato',
364-
'A' => 'Accettato',
365-
], ],
366-
]);
367-
368355
it('can return an associative array of magic translations [value/name => translations] with method name both singular and plural', function ($enumClass, $cases, $lang, $result) {
369-
expect($enumClass::newsAsSelect($cases, $lang))->toBe($result);
356+
expect($enumClass::newsByValue($cases, $lang))->toBe($result);
370357
})->with([
371358
'translations' => [Status::class, null, null, [
372359
'PENDING' => 'news PENDING',
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Datomatic\LaravelEnumHelper\Tests\Support\Enums;
6+
7+
use Datomatic\LaravelEnumHelper\LaravelEnumHelper;
8+
9+
/**
10+
* @method static string pending()
11+
* @method static string accepted()
12+
* @method static string discarded()
13+
* @method static string noResponse()
14+
*/
15+
enum StatusFallbackLocale
16+
{
17+
use LaravelEnumHelper;
18+
19+
case PENDING;
20+
21+
case ACCEPTED;
22+
23+
case DISCARDED;
24+
25+
case NO_RESPONSE;
26+
27+
}

tests/Support/Enums/StatusPascalCase.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ enum StatusPascalCase
2727
public function description(?string $lang = null): string
2828
{
2929
return match ($this) {
30-
self::Pending => 'Await decision',
31-
self::Accepted => 'Recognized valid',
32-
self::Discarded => 'No longer useful',
33-
self::NoResponse => 'No response',
30+
self::Pending => 'PC Await decision',
31+
self::Accepted => 'PC Recognized valid',
32+
self::Discarded => 'PC No longer useful',
33+
self::NoResponse => 'PC No response',
3434
};
3535
}
3636
}

0 commit comments

Comments
 (0)