This package provides a make:enum
command and a set of traits that extend the functionality of enums
You can install the package via composer:
composer require frameck/awesome-enums
php artisan make:enum DeclineCode
Possible options:
--type
: creates a backed enum, possible values are:int
orstring
--force
: overwrites if the enum already exists
If no --type
option is passed the command will prompt you to make a choice
This command generates the following code:
namespace App\Enums;
use Frameck\AwesomeEnums\Traits\Comparable;
use Frameck\AwesomeEnums\Traits\HasDetails;
use Frameck\AwesomeEnums\Traits\HasHelpers;
enum DeclineCode: string
{
use Comparable;
use HasDetails;
use HasHelpers;
}
Then we add the cases
enum DeclineCode: string
{
use Comparable;
use HasDetails;
use HasHelpers;
case DEFAULT = 'default';
case CARD_NOT_SUPPORTED = 'card_not_supported';
case DO_NOT_HONOR = 'do_not_honor';
case EXPIRED_CARD = 'expired_card';
case GENERIC_DECLINE = 'generic_decline';
}
This is already enough to use this package:
The all()
method provides a collection of all cases
DeclineCode::all();
// result
Illuminate\Support\Collection {
all: [
App\Enums\DeclineCode {
+name: "DEFAULT",
+value: "default",
},
App\Enums\DeclineCode {
+name: "CARD_NOT_SUPPORTED",
+value: "card_not_supported",
},
App\Enums\DeclineCode {
+name: "DO_NOT_HONOR",
+value: "do_not_honor",
},
App\Enums\DeclineCode {
+name: "EXPIRED_CARD",
+value: "expired_card",
},
App\Enums\DeclineCode {
+name: "GENERIC_DECLINE",
+value: "generic_decline",
},
],
}
The details()
method provides a collection of all cases with their details
DeclineCode::details();
// result
Illuminate\Support\Collection {
all: [
[
"name" => "Default",
"value" => "default",
],
[
"name" => "Card Not Supported",
"value" => "card_not_supported",
],
[
"name" => "Do Not Honor",
"value" => "do_not_honor",
],
[
"name" => "Expired Card",
"value" => "expired_card",
],
[
"name" => "Generic Decline",
"value" => "generic_decline",
],
],
}
The toArray()
method provides the array representation of the details()
method
DeclineCode::toArray();
// result
[
[
"name" => "Default",
"value" => "default",
],
[
"name" => "Card Not Supported",
"value" => "card_not_supported",
],
[
"name" => "Do Not Honor",
"value" => "do_not_honor",
],
[
"name" => "Expired Card",
"value" => "expired_card",
],
[
"name" => "Generic Decline",
"value" => "generic_decline",
],
]
The fromName()
method matches the case name and returns you the enum instance
DeclineCode::fromName('expired card')
// result
App\Enums\DeclineCode {
name: "EXPIRED_CARD",
value: "expired_card",
}
You can call the enum case as a static function
DeclineCode::EXPIRED_CARD() // equivalent to DeclineCode::EXPIRED_CARD->value
DeclineCode::EXPIRED_CARD('name') // equivalent to DeclineCode::EXPIRED_CARD->name
DeclineCode::EXPIRED_CARD('value') // equivalent to DeclineCode::EXPIRED_CARD->value
or from an instance
$declineCode = DeclineCode::EXPIRED_CARD;
$declineCode() // equivalent to $declineCode->value
$declineCode('name') // equivalent to $declineCode->name
$declineCode('value') // equivalent to $declineCode->value
The details()
method gives you the the array of details for that specific case
DeclineCode::EXPIRED_CARD->getDetails();
// result
[
"name" => "Expired Card",
"value" => "expired_card",
]
Additionally you can create a function with the camel cased version of the case name (+ Details) that returns an array of details that will be used instead of the default one:
private function defaultDetails(): array
{
return [
'name' => 'Call Issuer',
'select' => 'Call issuer',
'description' => 'The card was declined for an unknown reason.',
'next_steps' => 'The customer needs to contact their card issuer for more information.',
];
}
private function cardNotSupportedDetails(): array
{
return [
'name' => 'Card Not Supported',
'description' => 'The card was declined for an unknown reason.',
'next_steps' => 'The customer needs to contact their card issuer for more information.',
];
}
private function doNotHonorDetails(): array
{
return [
'name' => 'Do Not Honor',
'description' => 'The card was declined for an unknown reason.',
'next_steps' => 'The customer needs to contact their card issuer for more information.',
];
}
private function expiredCardDetails(): array
{
return [
'name' => 'Expired Card',
'description' => 'The card was declined for an unknown reason.',
'next_steps' => 'The customer needs to contact their card issuer for more information.',
];
}
private function genericDeclineDetails(): array
{
return [
'name' => 'Generic Decline',
'description' => 'The card was declined for an unknown reason.',
'next_steps' => 'The customer needs to contact their card issuer for more information.',
];
}
You can also pass an optional key to retrieve only that value
DeclineCode::EXPIRED_CARD->getDetails('description');
// result
// The card was declined for an unknown reason.
The toSelect()
method provides an array of key value pair useful in html selects
DeclineCode::toSelect();
[
"default" => "Call issuer",
"card_not_supported" => "Card Not Supported",
"do_not_honor" => "Do Not Honor",
"expired_card" => "Expired Card",
"generic_decline" => "Generic Decline",
]
// the toSelect() method is based on the details() array so you can specify a custom label for the select
// in order the package searches for a 'select' key then 'label' and 'name'
// so you can have a different value for 'name' and 'select'
public static function toSelect(): array
{
return collect(self::cases())
->mapWithKeys(function (self $case) {
$caseDetails = $case->getDetails();
$selectLabel = $caseDetails['select']
?? $caseDetails['label']
?? $caseDetails['name'];
return [
$case->value => $selectLabel,
];
})
->toArray();
}
The toJson()
method provides the json representation of the toSelect()
method useful when you have to share data with a frontend in vue, react ecc... or an api:
DeclineCode::toJson();
// "{"default":"Call issuer","card_not_supported":"Card Not Supported","do_not_honor":"Do Not Honor","expired_card":"Expired Card","generic_decline":"Generic Decline"}"
The is()
and isNot()
methods provide a fluent way to check if an enum instance is equal to another:
DeclineCode::CARD_NOT_SUPPORTED->is(DeclineCode::EXPIRED_CARD); // false
DeclineCode::CARD_NOT_SUPPORTED->isNot(DeclineCode::EXPIRED_CARD); // true
The in()
and notIn()
methods provide a fluent way to check if an enum instance is present in an array of enums:
DeclineCode::CARD_NOT_SUPPORTED->in([DeclineCode::EXPIRED_CARD, DeclineCode::GENERIC_DECLINE]); // false
DeclineCode::CARD_NOT_SUPPORTED->notIn([DeclineCode::EXPIRED_CARD, DeclineCode::GENERIC_DECLINE]); // true
To better integrate the enum in a laravel ecosystem you can add it inside the $casts
property of the model
class Payment extends Model
{
protected $casts = [
'decline_code' => DeclineCode::class
];
}
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.