https://github.com/josezenem/php-enums-extended
PHP 8.1 Enums Extended, gives you the ability to use additional methods to work with PHP 8.1 Enums.
https://github.com/josezenem/php-enums-extended
enums laravel php
Last synced: 6 months ago
JSON representation
PHP 8.1 Enums Extended, gives you the ability to use additional methods to work with PHP 8.1 Enums.
- Host: GitHub
- URL: https://github.com/josezenem/php-enums-extended
- Owner: josezenem
- License: mit
- Created: 2022-02-02T01:38:28.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2026-01-12T07:14:29.000Z (6 months ago)
- Last Synced: 2026-01-12T17:19:48.873Z (6 months ago)
- Topics: enums, laravel, php
- Language: PHP
- Homepage:
- Size: 75.2 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# PHP 8.1 Enums Extended
[](https://packagist.org/packages/josezenem/php-enums-extended)
[](https://github.com/josezenem/php-enums-extended/actions/workflows/run-tests.yml)
[](https://www.codacy.com/gh/josezenem/php-enums-extended/dashboard?utm_source=github.com&utm_medium=referral&utm_content=josezenem/php-enums-extended&utm_campaign=Badge_Grade)
[](https://packagist.org/packages/josezenem/php-enums-extended)
[](https://packagist.org/packages/josezenem/php-enums-extended)
PHP 8.1 Enums Extended, gives you the ability to use additional methods to work with PHP 8.1 Enums.
```php
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()
```
## Installation
You can install the package via composer:
```bash
composer require josezenem/php-enums-extended
```
## Usage
## [`Available Methods`](#available-methods)
- [`equals()`](#method-equals)
- [`doesNotEqual()`](#method-does-not-equal)
- [`isCall**()`](#method-is-call)
## [`Available Static Methods`](#available-static-methods)
- [`options()`](#static-method-to-options-array)
- [`optionsFlipped()`](#static-method-to-options-flipped-array)
- [`names()`](#static-method-to-names-array)
- [`hasName()`](#static-method-has-name)
- [`values()`](#static-method-to-values-array)
- [`hasValue()`](#static-method-has-value)
- [`call**()`](#static-method-call)
### `equals()`
Pass one or multiple Enum cases, will return boolean if one matches.
```php
$blog->status->equals(StatusEnum::Closed, StatusEnum::Draft);
```
### `doesNotEqual()`
Pass one or multiple Enum cases, will return boolean if it does not match.
```php
$blog->status->doesNotEqual(StatusEnum::Closed, StatusEnum::Draft);
```
### `isCall**()`
Returns boolean if the current value matches the desired case. Methods with underscores can be accessed via camel case, as well as their regular name.
```php
$blog->status->isDraft();
// Given StatusEnum::OPEN_ISSUE = 4;
// the following is acceptable.
$blog->status->isOpenIssue();
$blog->status->isOPEN_ISSUE();
```
### `options()`
Will return an array of $val => $key.
```php
$options = self::options()
// returns
$options = [
'open' => 'Open',
'closed' => 'Closed',
'draft' => 'Draft',
]
```
### `optionsFlipped()`
Will return an array of $key => $val.
```php
$options = self::optionsFlipped()
// returns
$options = [
'Open' => 'open',
'Closed' => 'closed',
'Draft' => 'draft',
]
```
### `names()`
Will return an array of only names
```php
$options = self::names()
// returns
$options = [
'Open' => 'Open',
'Closed' => 'Closed',
'Draft' => 'Draft',
]
```
### `hasName()`
Pass variable and confirm if the name is valid for the Enum
```php
App\MyEnums\Type::hasName('Closed');
// Returns true
App\MyEnums\Type::hasName('close');
// Returns false
```
### `values()`
Will return an array of only values
```php
$options = self::values()
// returns
$options = [
'open' => 'open',
'closed' => 'closed',
'draft' => 'draft',
]
```
### `hasValue()`
Pass variable and confirm if the value is valid for the Enum
```php
App\MyEnums\Type::hasValue('closed');
// Returns true
App\MyEnums\Type::hasValue('not a valid value for the enum');
// Returns false
```
### `call**()`
Will allow you to grab the value of a field by calling it statically.
```php
// 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()
```
### Exception Handler
When using the magic methods, if the method calls do not exist, the system will throw
```
Josezenem\PhpEnumsExtended\Exceptions\EnumsExtendedException
```
```php
// 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,
//];
```
## Testing
```bash
composer test
```
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [Jose Jimenez](https://github.com/josezenem)
- [All Contributors](../../contributors)
- Special Thanks to [Shocm](https://twitter.com/shocm) for pushing me to make this, and answering my late replies.
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.