-
-
Notifications
You must be signed in to change notification settings - Fork 1
Add EnumHelpers trait for utility methods on backed enums #13
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3f1a52a
Add EnumHelpers trait for utility methods on backed enums
iqbalhasandev d364368
Add ignoreErrors configuration for EnumHelpers trait in PHPStan
iqbalhasandev 5acfc84
Add EnumHelpers trait to CampusStatus and PaymentMethod enums; create…
iqbalhasandev eef03bc
Enhance EnumHelpers trait with fallback for missing label methods and…
iqbalhasandev ac13c58
Fix styling
iqbalhasandev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,3 +31,5 @@ testbench.yaml | |
| /docs | ||
| /coverage | ||
| auth.json | ||
|
|
||
| .DS_Store | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace DevWizardHQ\Enumify\Concerns; | ||
|
|
||
| /** | ||
| * Provides common utility methods for backed enums. | ||
| * | ||
| * Use this trait on any string-backed or int-backed enum to gain | ||
| * helpers for listing options, values, names, and checking existence. | ||
| * | ||
| * @mixin \BackedEnum | ||
| */ | ||
| trait EnumHelpers | ||
| { | ||
| /** | ||
| * Get all enum cases as an array of value => label pairs. | ||
| * | ||
| * Calls the given method name on each case (defaults to "label"). | ||
| * Falls back to a humanized version of the case name when the method does not exist. | ||
| * | ||
| * Note: PHP coerces numeric-string keys to integers in arrays. | ||
| * Use {@see selectOptions()} if your enum has numeric-like string values. | ||
| * | ||
| * @return array<string|int, string> | ||
| */ | ||
| public static function options(string $label = 'label'): array | ||
| { | ||
| return collect(self::cases()) | ||
| ->mapWithKeys(fn (self $case): array => [ | ||
| $case->value => method_exists($case, $label) | ||
| ? $case->{$label}() | ||
| : self::humanize($case->name), | ||
| ]) | ||
| ->all(); | ||
| } | ||
|
iqbalhasandev marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Get all enum cases as an array of {value, label} objects for frontend selects. | ||
| * | ||
| * Falls back to a humanized version of the case name when the method does not exist. | ||
| * | ||
| * @return array<int, array{value: string|int, label: string}> | ||
| */ | ||
| public static function selectOptions(string $label = 'label'): array | ||
| { | ||
| return collect(self::cases()) | ||
| ->map(fn (self $case): array => [ | ||
| 'value' => $case->value, | ||
| 'label' => method_exists($case, $label) | ||
| ? $case->{$label}() | ||
| : self::humanize($case->name), | ||
| ]) | ||
|
iqbalhasandev marked this conversation as resolved.
|
||
| ->values() | ||
| ->all(); | ||
| } | ||
|
|
||
| /** | ||
| * Get all enum values as an array. | ||
| * | ||
| * @return array<int, string|int> | ||
| */ | ||
| public static function values(): array | ||
| { | ||
| return array_column(self::cases(), 'value'); | ||
| } | ||
|
|
||
| /** | ||
| * Get all enum names as an array. | ||
| * | ||
| * @return array<int, string> | ||
| */ | ||
| public static function names(): array | ||
| { | ||
| return array_column(self::cases(), 'name'); | ||
| } | ||
|
|
||
| /** | ||
| * Check if a value exists in this enum. | ||
| */ | ||
| public static function hasValue(string|int $value): bool | ||
| { | ||
| return self::tryFrom($value) !== null; | ||
| } | ||
|
|
||
| /** | ||
| * Convert a SCREAMING_SNAKE_CASE name to a human-readable title. | ||
| */ | ||
| private static function humanize(string $name): string | ||
| { | ||
| return ucwords(strtolower(str_replace('_', ' ', $name))); | ||
| } | ||
|
iqbalhasandev marked this conversation as resolved.
|
||
| } | ||
|
iqbalhasandev marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use DevWizardHQ\Enumify\Tests\Fixtures\CampusStatus; | ||
| use DevWizardHQ\Enumify\Tests\Fixtures\OrderStatus; | ||
| use DevWizardHQ\Enumify\Tests\Fixtures\PaymentMethod; | ||
|
|
||
| it('returns options as value => label pairs', function () { | ||
| $options = PaymentMethod::options(); | ||
|
|
||
| expect($options)->toBe([ | ||
| 'credit_card' => 'Credit Card', | ||
| 'debit_card' => 'Debit Card', | ||
| 'bank_transfer' => 'Bank Transfer', | ||
| 'paypal' => 'PayPal', | ||
| 'crypto' => 'Cryptocurrency', | ||
| ]); | ||
| }); | ||
|
|
||
| it('returns options using a custom method name', function () { | ||
| $options = CampusStatus::options('color'); | ||
|
|
||
| expect($options)->toBe([ | ||
| 'active' => 'green', | ||
| 'suspended' => 'red', | ||
| 'inactive' => 'gray', | ||
| ]); | ||
| }); | ||
|
|
||
| it('falls back to humanized case name when label method does not exist', function () { | ||
| $options = OrderStatus::options(); | ||
|
|
||
| expect($options)->toBe([ | ||
| 'pending' => 'Pending', | ||
| 'processing' => 'Processing', | ||
| 'shipped' => 'Shipped', | ||
| 'delivered' => 'Delivered', | ||
| 'cancelled' => 'Cancelled', | ||
| ]); | ||
| }); | ||
|
|
||
| it('returns select options as value/label arrays', function () { | ||
| $options = PaymentMethod::selectOptions(); | ||
|
|
||
| expect($options)->toBe([ | ||
| ['value' => 'credit_card', 'label' => 'Credit Card'], | ||
| ['value' => 'debit_card', 'label' => 'Debit Card'], | ||
| ['value' => 'bank_transfer', 'label' => 'Bank Transfer'], | ||
| ['value' => 'paypal', 'label' => 'PayPal'], | ||
| ['value' => 'crypto', 'label' => 'Cryptocurrency'], | ||
| ]); | ||
| }); | ||
|
|
||
| it('returns select options using a custom method name', function () { | ||
| $options = CampusStatus::selectOptions('color'); | ||
|
|
||
| expect($options)->toBe([ | ||
| ['value' => 'active', 'label' => 'green'], | ||
| ['value' => 'suspended', 'label' => 'red'], | ||
| ['value' => 'inactive', 'label' => 'gray'], | ||
| ]); | ||
| }); | ||
|
|
||
| it('falls back to humanized case name in select options when method does not exist', function () { | ||
| $options = OrderStatus::selectOptions(); | ||
|
|
||
| expect($options)->toBe([ | ||
| ['value' => 'pending', 'label' => 'Pending'], | ||
| ['value' => 'processing', 'label' => 'Processing'], | ||
| ['value' => 'shipped', 'label' => 'Shipped'], | ||
| ['value' => 'delivered', 'label' => 'Delivered'], | ||
| ['value' => 'cancelled', 'label' => 'Cancelled'], | ||
| ]); | ||
| }); | ||
|
|
||
| it('returns all enum values', function () { | ||
| expect(PaymentMethod::values())->toBe([ | ||
| 'credit_card', | ||
| 'debit_card', | ||
| 'bank_transfer', | ||
| 'paypal', | ||
| 'crypto', | ||
| ]); | ||
| }); | ||
|
|
||
| it('returns all enum names', function () { | ||
| expect(PaymentMethod::names())->toBe([ | ||
| 'CREDIT_CARD', | ||
| 'DEBIT_CARD', | ||
| 'BANK_TRANSFER', | ||
| 'PAYPAL', | ||
| 'CRYPTO', | ||
| ]); | ||
| }); | ||
|
|
||
| it('checks if a value exists', function () { | ||
| expect(PaymentMethod::hasValue('credit_card'))->toBeTrue(); | ||
| expect(PaymentMethod::hasValue('nonexistent'))->toBeFalse(); | ||
| }); | ||
|
|
||
| it('uses strict comparison for hasValue', function () { | ||
| expect(PaymentMethod::hasValue(''))->toBeFalse(); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.