PHP code example of frameck / awesome-enums

1. Go to this page and download the library: Download frameck/awesome-enums library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

frameck / awesome-enums example snippets


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;
}

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';
}

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",
        },
    ],
}

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",
        ],
    ],
}

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",
    ],
]

DeclineCode::fromName('expired card')

// result
App\Enums\DeclineCode {
    name: "EXPIRED_CARD",
    value: "expired_card",
}

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

$declineCode = DeclineCode::EXPIRED_CARD;

$declineCode() // equivalent to $declineCode->value
$declineCode('name') // equivalent to $declineCode->name
$declineCode('value') // equivalent to $declineCode->value

DeclineCode::EXPIRED_CARD->getDetails();

// result
[
    "name" => "Expired Card",
    "value" => "expired_card",
]

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.',
    ];
}

DeclineCode::EXPIRED_CARD->getDetails('description');

// result
// The card was declined for an unknown reason.

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();
}

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"}"

DeclineCode::CARD_NOT_SUPPORTED->is(DeclineCode::EXPIRED_CARD); // false
DeclineCode::CARD_NOT_SUPPORTED->isNot(DeclineCode::EXPIRED_CARD); // true

DeclineCode::CARD_NOT_SUPPORTED->in([DeclineCode::EXPIRED_CARD, DeclineCode::GENERIC_DECLINE]); // false
DeclineCode::CARD_NOT_SUPPORTED->notIn([DeclineCode::EXPIRED_CARD, DeclineCode::GENERIC_DECLINE]); // true

class Payment extends Model
{
    protected $casts = [
        'decline_code' => DeclineCode::class
    ];
}
bash
php artisan make:enum DeclineCode