Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue #22: Replace 'self' with 'static' #24

Merged
merged 6 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/art export-ignore
/docs export-ignore
/tests export-ignore
/workbench export-ignore
/.editorconfig export-ignore
/.php_cs.dist.php export-ignore
/psalm.xml export-ignore
Expand Down
13 changes: 4 additions & 9 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ jobs:
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.1]
laravel: [10.*]
os: [ubuntu-latest]
php: [8.2]
laravel: [11.*]
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 10.*
testbench: 8.*
carbon: ^2.63

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

Expand All @@ -41,8 +37,7 @@ jobs:

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
composer install

- name: Execute tests
run: vendor/bin/pest
46 changes: 19 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ enum VolumeUnitEnum: string implements BackedEnum
{
use IsBackedEnum;

case MILLIGRAMS = "MILLIGRAMS";
case GRAMS = "GRAMS";
case KILOGRAMS = "KILOGRAMS";
case TONNE = "TONNE";
case MILLIGRAMS = "milligrams";
case GRAMS = "grams";
case KILOGRAMS = "kilograms";
case TONNE = "tonne";
}
```

Expand Down Expand Up @@ -112,7 +112,7 @@ returns
[
[
'name' => 'MILLIGRAMS'
'value' => 'MILLIGRAMS',
'value' => 'milligrams',
'label' => 'mg',
'meta' => [
'background_color' => 'bg-green-100',
Expand All @@ -121,7 +121,7 @@ returns
],
[
'name' => 'GRAMS',
'value' => 'GRAMS',
'value' => 'grams',
'label' => 'g',
'meta' => [
'background_color' => 'bg-red-100',
Expand Down Expand Up @@ -167,10 +167,10 @@ returns

```php
[
'MILLIGRAMS',
'GRAMS',
'KILOGRAMS',
'TONNE',
'milligrams',
'grams',
'killograms',
'tonne',
]
```

Expand Down Expand Up @@ -209,10 +209,10 @@ returns

```php
[
'MILLIGRAMS'=>'mg',
'GRAMS' =>'g',
'KILOGRAMS' =>'kg',
'TONNE' =>'t',
'MILLIGRAMS' => 'mg',
'GRAMS' => 'g',
'KILOGRAMS' => 'kg',
'TONNE' => 't',
]
```

Expand All @@ -231,10 +231,10 @@ returns
```php
[
'name' => 'MILLIGRAMS'
'value' => 'MILLIGRAMS',
'value' => 'milligrams',
'label' => 'mg',
'meta' => [
'color' => 'bg-green-100',
'color' => 'bg-green-100',
'text_color' => 'text-green-800',
],
]
Expand All @@ -253,20 +253,12 @@ VolumeUnitEnum::MILLIGRAMS->toHtml();
returns

```php
[
'name' => 'MILLIGRAMS'
'value' => 'MILLIGRAMS',
'label' => 'mg',
'meta' => [
'color' => 'bg-green-100',
'text_color' => 'text-green-800',
],
]
mg
```

### toJson

An alias for toArray.
Returns a json string represention of the toArray return value.

### is/isA/isAn

Expand Down Expand Up @@ -336,7 +328,7 @@ public function rules(): array
## Other Classes

### AsFullEnumCollection
This cast is similar to the Laravel built in `AsEnumCollection` cast but unlike the built in will maintain the full `toArray` structure
This cast is similar to the Laravel built in `AsEnumCollection` cast but unlike the built-in will maintain the full `toArray` structure
when converting to json.

E.g. the Laravel built in `AsEnumCollection` cast will return the following json:
Expand Down
27 changes: 23 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"require-dev": {
"laravel/pint": "^1.0",
"nunomaduro/collision": "^6.1",
"nunomaduro/larastan": "^2.0.1",
"larastan/larastan": "^v2.9.2",
"orchestra/testbench": "^8.0",
"pestphp/pest": "^1.21",
"pestphp/pest-plugin-laravel": "^1.1",
Expand All @@ -39,14 +39,33 @@
},
"autoload-dev": {
"psr-4": {
"Webfox\\LaravelBackedEnums\\Tests\\": "tests"
"Webfox\\LaravelBackedEnums\\Tests\\": "tests",
"Workbench\\App\\": "workbench/app/",
"Workbench\\Database\\Factories\\": "workbench/database/factories/",
"Workbench\\Database\\Seeders\\": "workbench/database/seeders/"
}
},
"scripts": {
"analyse": "vendor/bin/phpstan analyse",
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage",
"format": "vendor/bin/pint"
"format": "vendor/bin/pint",
"post-autoload-dump": [
"@clear",
"@prepare"
],
"clear": "@php vendor/bin/testbench package:purge-skeleton --ansi",
"prepare": "@php vendor/bin/testbench package:discover --ansi",
"build": "@php vendor/bin/testbench workbench:build --ansi",
"serve": [
"Composer\\Config::disableProcessTimeout",
"@build",
"@php vendor/bin/testbench serve"
],
"lint": [
"@php vendor/bin/pint",
"@php vendor/bin/phpstan analyse"
]
},
"config": {
"sort-packages": true,
Expand All @@ -64,4 +83,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
8 changes: 8 additions & 0 deletions src/Casts/AsFullEnumCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public function __construct(protected string $enumClass)
{
}

/**
* @param class-string<T> $class
*/
public static function of(string $class): string
{
return static::class.':'.$class;
}

/**
* @return \Illuminate\Support\Collection<T>|null
*/
Expand Down
10 changes: 5 additions & 5 deletions src/IsBackedEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,27 @@ protected static function ensureImplementsInterface(): void
public static function options(): array
{
static::ensureImplementsInterface();
return array_map(fn($enum) => $enum->toArray(), self::cases());
return array_map(fn($enum) => $enum->toArray(), static::cases());
}

public static function names(): array
{
static::ensureImplementsInterface();
return array_map(fn($enum) => $enum->name, self::cases());
return array_map(fn($enum) => $enum->name, static::cases());
}

public static function values(): array
{
static::ensureImplementsInterface();
return array_map(fn($enum) => $enum->value, self::cases());
return array_map(fn($enum) => $enum->value, static::cases());
}

public static function map(): array
{
static::ensureImplementsInterface();
$array = [];

foreach (self::cases() as $enum) {
foreach (static::cases() as $enum) {
$array[$enum->value] = $enum->label();
}

Expand All @@ -55,7 +55,7 @@ public static function map(): array
public static function labels(): array
{
static::ensureImplementsInterface();
return array_map(fn($enum) => self::getLabel($enum->name), self::cases());
return array_map(fn($enum) => static::labelFor($enum), static::cases());
}

public static function labelFor(self $value): string
Expand Down
Loading