PHP 8.1 Enums Extended, gives you the ability to use additional methods to work with PHP 8.1 Enums.
You can install the package via composer:
composer require josezenem/php-enums-extended
- ->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,
//];
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.
- Jose Jimenez
- All Contributors
- Special Thanks to Shocm for pushing me to make this, and answering my late replies.
- Special thanks to Spatie & Brendt for their packages and knowledge.
The MIT License (MIT). Please see License File for more information.