PHP code example of josezenem / php-enums-extended

1. Go to this page and download the library: Download josezenem/php-enums-extended 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/ */

    

josezenem / php-enums-extended example snippets


enum StatusEnum:int
{
    case Closed = 0;
    case Open = 1;
    case PENDING_APPROVAL = 2;
}

// Given a new Blog() that uses the enum trait, you can do things like:
$blog->status->isOpen() // Will return boolean
$blog->status->equals(StatusEnum::Open, StatusEnum::Closed)

// Normalization happens in the background allowing these scenarios 
$blog->status->isPendingApproval();
$blog->status->isPENDING_APPROVAL();

StatusEnum::Open() // Will return ->value, vs doing StatusEnum::Open->value
StatusEnum::PendingApproval()
StatusEnum::PENDING_APPROVAL()

$blog->status->equals(StatusEnum::Closed, StatusEnum::Draft);

$blog->status->doesNotEqual(StatusEnum::Closed, StatusEnum::Draft);

$blog->status->isDraft();

// Given StatusEnum::OPEN_ISSUE = 4;
// the following is acceptable.
$blog->status->isOpenIssue();
$blog->status->isOPEN_ISSUE();

$options = self::options()

// returns
$options = [
    'open' => 'Open',
    'closed' => 'Closed',
    'draft' => 'Draft',
]

$options = self::optionsFlipped()

// returns
$options = [
    'Open' => 'open',
    'Closed' => 'closed',
    'Draft' => 'draft',
]

$options = self::names()

// returns
$options = [
    'Open' => 'Open',
    'Closed' => 'Closed',
    'Draft' => 'Draft',
]

App\MyEnums\Type::hasName('Closed');
// Returns true

App\MyEnums\Type::hasName('close');
// Returns false

$options = self::values()

// returns
$options = [
    'open' => 'open',
    'closed' => 'closed',
    'draft' => 'draft',
]

App\MyEnums\Type::hasValue('closed');
// Returns true

App\MyEnums\Type::hasValue('not a valid value for the enum');
// Returns false

// Consider the following scenario, to get the value you would do:
// StatusEnum::Open->value
enum StatusEnum:int
{
    case Closed = 0;
    case Open = 1;
    case Draft = 2;
}

// You can instead get value directy by calling it statically
// Case Insensitive
StatusEnum::OPEN()
StatusEnum::Open()

// StatusEnum.php
// StatusEnum:int is used for the example, but supports :string and default of just StatusEnum
use Josezenem\PhpEnumsExtended\Traits\PhpEnumsExtendedTrait;

enum StatusEnum:int
{
    use PhpEnumsExtendedTrait;

    case Closed = 0;
    case Open = 1;
    case Draft = 2;
}

// Blog.php
class Blog
{
    public function __construct(
        public StatusEnum $status = StatusEnum::Open,
    ) {
    }
}



// Usage
$blog = new Blog();


// ->equals()
$blog->status->equals(StatusEnum::Open); // will return true if it matches
$blog->status->equals(StatusEnum::Closed, StatusEnum::Open); // Pass any number of params, will return true if it matches any of the parameters

// ->doesNotEqual()
$blog->status->doesNotEqual(StatusEnum::Closed); // will return true if it does not match
$blog->status->doesNotEqual(StatusEnum::Closed, StatusEnum::Draft)  // Pass any number of params, will return true if it does not match any of the parameters

// ->is** magic method
// the magic method takes camelCase allowing you to do boolean check against any field.
$blog->status->isOpen() // will return true or false

// ::options()
$options = StatusEnum::options();

// will output
//$options = [
//    0 => 'Closed',
//    1 => 'Open',
//    2 => 'Closed',
//];

// ::optionsFlipped()
$options = StatusEnum::optionsFlipped();

// will output
//$options = [
//    'Closed' => 0,
//    'Open' => 1,
//    'Closed' => 2,
//];
bash
composer