Skip to content

PHP 8.1 Enums Extended, gives you the ability to use additional methods to work with PHP 8.1 Enums.

License

Notifications You must be signed in to change notification settings

josezenem/php-enums-extended

Repository files navigation

PHP 8.1 Enums Extended

Latest Version on Packagist Tests Total Downloads

PHP 8.1 Enums Extended, gives you the ability to use additional methods to work with PHP 8.1 Enums.

Installation

You can install the package via composer:

composer require josezenem/php-enums-extended

Usage

  • ->equals(...$params) Pass one or multiple parameters, will return true if one matches.
  • ->toOptionsArray() Will return an array of a $key => $val pair.
  • ->toOptionsInverseArray() Will return an array of a $val => $key pair.
// Blog.php
class Blog
{
    public function __construct(
        public StatusEnum $status = StatusEnum::Open,
    ) {
    }
}

// StatusEnum.php
use Josezenem\PhpEnumsExtended\Traits\PhpEnumsExtendedTrait;

enum StatusEnum:int
{
    use PhpEnumsExtendedTrait;

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



// Usage
$blog = new Blog();


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



// ->toOptionsArray()
$options = $blog->toOptionsArray();

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



// ->toOptionsInverseArray()
$options = $blog->toOptionsInversedArray();

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

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

PHP 8.1 Enums Extended, gives you the ability to use additional methods to work with PHP 8.1 Enums.

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Languages