https://github.com/ngmy/php-enum
The enumeration type for PHP
https://github.com/ngmy/php-enum
array enum enum-map enum-set enumeration library map php php-library set type
Last synced: 3 months ago
JSON representation
The enumeration type for PHP
- Host: GitHub
- URL: https://github.com/ngmy/php-enum
- Owner: ngmy
- License: mit
- Created: 2021-02-14T12:07:34.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-05-11T12:36:46.000Z (about 4 years ago)
- Last Synced: 2025-02-05T19:11:07.834Z (5 months ago)
- Topics: array, enum, enum-map, enum-set, enumeration, library, map, php, php-library, set, type
- Language: PHP
- Homepage:
- Size: 135 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# PHP Enum
[](//packagist.org/packages/ngmy/enum)
[](//packagist.org/packages/ngmy/enum)
[](//packagist.org/packages/ngmy/enum)
[](//packagist.org/packages/ngmy/enum)
[](//packagist.org/packages/ngmy/enum)
[](https://github.com/ngmy/php-typed-array/actions/workflows/php.yml)
[](https://coveralls.io/github/ngmy/php-enum?branch=master)
[](https://github.com/phpstan/phpstan)
[](https://shepherd.dev/github/ngmy/php-typed-array)
[](https://shepherd.dev/github/ngmy/php-typed-array)PHP Enum is the enumeration type for PHP.
- Interface like the enum type of Java
- Also provides the enum map and set like Java
- Supports the static analysis like PHPStan and Psalm. Please see [examples](docs/examples)```php
/**
* @method static self FOO()
* @method static self BAR()
* @method static self BAZ()
*/
class Enum1 extends Ngmy\Enum\Enum
{
/** @enum */
private static $FOO;
/** @enum */
private static $BAR;
/** @enum */
private static $BAZ;
}// Returns the enum constant of the specified name
$foo = Enum1::valueOf('FOO');
$bar = Enum1::valueOf('BAR');
$baz = Enum1::valueOf('BAZ');
// You can also use magic factory methods
$foo = Enum1::FOO();
$bar = Enum1::BAR();
$baz = Enum1::BAZ();// Returns the name of this enum constant, exactly as declared in its enum declaration
echo $foo->name() . PHP_EOL; // FOO
echo $bar->name() . PHP_EOL; // BAR
echo $baz->name() . PHP_EOL; // BAZ// Returns the name of this enum constant, as contained in the declaration
echo $foo . PHP_EOL; // FOO
echo $bar . PHP_EOL; // BAR
echo $baz . PHP_EOL; // BAZ// Returns the ordinal of this enum constant
echo $foo->ordinal() . PHP_EOL; // 0
echo $bar->ordinal() . PHP_EOL; // 1
echo $baz->ordinal() . PHP_EOL; // 2// Returns true if the specified object is equal to this enum constant
echo var_export($foo->equals($foo), true) . PHP_EOL; // true
echo var_export($foo->equals(Enum1::valueOf('FOO')), true) . PHP_EOL; // true
echo var_export($foo->equals($bar), true) . PHP_EOL; // false// You can also have the enum constant with a value
/**
* @method static self FOO()
* @method static self BAR()
* @method static self BAZ()
*/
class Enum2 extends Ngmy\Enum\Enum
{
/** @enum */
private static $FOO = 1;
/** @enum */
private static $BAR = 2;
/** @enum */
private static $BAZ = 3;public function getValue(): int
{
return self::${$this->name()};
}
}echo Enum2::valueOf('FOO')->getValue() . PHP_EOL; // 1
echo Enum2::valueOf('BAR')->getValue() . PHP_EOL; // 2
echo Enum2::valueOf('BAZ')->getValue() . PHP_EOL; // 3
```## Requirements
PHP Enum has the following requirements:* PHP >= 7.3
## Installation
Execute the Composer `require` command:
```console
composer require ngmy/enum
```## Documentation
Please see the [API documentation](https://ngmy.github.io/php-enum/api/).## License
PHP Enum is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).