tkaratug / powered-enum
Some cool add-ons to PHP native enums.
0.0.1
2022-10-12 20:35 UTC
Requires
- php: ^8.1
Requires (Dev)
- phpunit/phpunit: ^9.5
README
This package offers a trait that contains some cool features for native PHP enums.
Requirements
- PHP
8.1
or higher.
Installation
composer require tkaratug/powered-enum
Declaration
All you need to do is use the PoweredEnum
trait in your native PHP enums.
use Tkaratug\PoweredEnum\PoweredEnum; enum MyEnum: int { use PoweredEnum; case ONE = 1; case TWO = 2; case THREE = 3; }
Jump To
Methods
is(), isNot()
- You can check the equality of a case against any name by passing it to the
is()
andisNot()
methods.
$myEnum = MyEnum::ONE; $myEnum->is(MyEnum::ONE); // true $myEnum->is(MyEnum::TWO); // false $myEnum->isNot(MyEnum::ONE); // false $myEnum->isNot(MyEnum::TWO); // true
Static Methods
hasName()
- You can check whether an enum has a case by given name via the
hasName()
method.
enum MyEnum: int { use PoweredEnum; case ONE = 1; case TWO = 2; } MyEnum::hasName('ONE'); // true MyEnum::hasName('THREE'); // false
hasValue()
- You can check whether an enum has a case by given value via the
hasValue()
method.
enum MyEnum: int { use PoweredEnum; case ONE = 1; case TWO = 2; } MyEnum::hasValue(1); // true MyEnum::hasValue(3); // false
getNames()
- You can get enum case names as an array by using the
getNames()
method.
enum MyEnum: int { use PoweredEnum; case ONE = 1; case TWO = 2; } MyEnum::getNames(); // ['ONE', 'TWO']
getValues()
- You can get enum case values as an array by using the
getValues()
method.
enum MyEnum: int { use PoweredEnum; case ONE = 1; case TWO = 2; } MyEnum::getValues(); // [1, 2]
toArray()
- You can get a combined array of the enum cases as
value => name
by using thetoArray()
method.
enum MyEnum: int { use PoweredEnum; case ONE = 1; case TWO = 2; } MyEnum::toArray(); // [1 => 'ONE', 2 => 'TWO']
getNamesExcept()
- You can get names of enum cases as an array except for given ones by using the
getNamesExcept()
method.
enum MyEnum: int { use PoweredEnum; case ONE = 1; case TWO = 2; case THREE = 3; } MyEnum::getNamesExcept([MyEnum::ONE]); // ['TWO', 'THREE']
getValuesExcept()
- You can get values of enum cases as an array except for given ones by using the
getValuesExcept()
method.
enum MyEnum: int { use PoweredEnum; case ONE = 1; case TWO = 2; case THREE = 3; } MyEnum::getValuesExcept([MyEnum::ONE]); // [2, 3]
toArrayExcept()
- You can get a combined array of the enum cases as
value => name
except for given ones by using thetoArrayExcept()
method.
enum MyEnum: int { use PoweredEnum; case ONE = 1; case TWO = 2; case THREE = 3; } MyEnum::toArrayExcept([MyEnum::ONE]); // [2 => 'TWO', 3 => 'THREE]
getRandomName()
- You can get a random name of the enum cases by using the
getRandomName()
method.
enum MyEnum: int { use PoweredEnum; case ONE = 1; } MyEnum::getRandomName(); // ['ONE']
getRandomValue()
- You can get a random value of the enum cases by using the
getRandomValue()
method.
enum MyEnum: int { use PoweredEnum; case ONE = 1; } MyEnum::getRandomValue(); // [1]
getRandomCase()
- You can get a random case of the enum by using the
getRandomCase()
method.
enum MyEnum: int { use PoweredEnum; case ONE = 1; } MyEnum::getRandomCase(); // MyEnum::ONE
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
If you've found a bug regarding security please mail tkaratug@hotmail.com.tr instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.