typiqally / enum
PHP Enum Types
Requires
- php: ^7.4 || ^8.0
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^9.4
This package is auto-updated.
Last update: 2021-10-29 15:28:35 UTC
README
In the current version of PHP, enum
types are not supported. This package will introduce the enum
type into PHP. The use-case is built around Java Enum Types. An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.
Note
This project is currently under development and still in an alpha stage.
Installation
The package can be installed by executing the following command:
composer require typiqally/enum
Basic
Basic enums can be created like this:
use Typiqally\Enum\Enum; /** * @method static self NORTH() * @method static self EAST() * @method static self SOUTH() * @method static self WEST() */ class Direction extends Enum { }
Usage
And this is how they are used:
public function walk(Direction $direction): void { //Walk logic } $this->walk(Direction::NORTH());
Values
Enum constants can have a mixed
value, this means it can have dynamic values like Java Enum Types. Values are assigned like this:
use Typiqally\Enum\Enum; /** * @method static self NORTH() * @method static self EAST() * @method static self SOUTH() * @method static self WEST() */ class Direction extends Enum { protected static function values(): array { return [ 'NORTH' => [ 'degrees' => 0 | 360 ], 'EAST' => [ 'degrees' => 90 ], 'SOUTH' => [ 'degrees' => 180 ], 'WEST' => [ 'degrees' => 270 ] ]; } public function getDegrees(): int { return $this->value['degrees']; } }
Usage
And this is how they are used:
public function walk(Direction $direction): void { $degrees = $direction->getDegrees(); } $this->walk(Direction::NORTH());
License
This project is licensed under the GPL-3.0 license. See LICENSE.md for the full license text.