https://github.com/morebec/orkestra-enum
[READ ONLY] Orkestra component providing enums
https://github.com/morebec/orkestra-enum
enum orkestra php
Last synced: about 1 month ago
JSON representation
[READ ONLY] Orkestra component providing enums
- Host: GitHub
- URL: https://github.com/morebec/orkestra-enum
- Owner: Morebec
- License: apache-2.0
- Created: 2021-04-21T00:53:44.000Z (about 5 years ago)
- Default Branch: 2.x
- Last Pushed: 2023-03-31T18:46:11.000Z (about 3 years ago)
- Last Synced: 2025-07-08T14:54:38.166Z (11 months ago)
- Topics: enum, orkestra, php
- Language: PHP
- Homepage:
- Size: 10.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Enum
This Orkestra component provides typed enumerations to PHP.
## Installation
```shell
composer require morebec/orkestra-orkestra-enum
```
## Usage
### Creating an Enum
To create a new Enum, one needs to extend the `Enum` class.
As an example, lets pretend we want to create a `CardinalPoint` Class.
Since there are strictly 4 cardinal points, this is a good candidate for an Enum:
```php
class CardinalPoint extends Enum
{
const NORTH = 'NORTH';
const EAST = 'EAST';
const WEST = 'WEST';
const SOUTH = 'SOUTH';
}
```
Simply doing this, will allow us to use our class in the following way:
```php
// Instantiate a new CardinalPoint instance
$direction = new CardinalPoint(CardinalPoint::NORTH);
// Since Enums have builtin validation,
// the following line would throw an InvalidArgumentException:
$direction = new CardinalPoint('North');
// However the following would work:
$direction = new CardinalPoint('NORTH');
// Using in functions or class methods
public function changeDirection(CardinalPoint $direction)
{
// Testing equlity with string
if(!$direction->isEqualTo(new CardinalPoint(CardinalPoint::EAST))) {
echo 'Not going East!';
}
// Since the constants are strings, it is also possible to compare
// using loose comparison
if($direction == CardinalPoint::NORTH) {
echo 'Definitely going North!';
}
}
```
> For easier IDE integration we can even go further adding `@method` annotations to the Enum class:
> ```php
> /**
> * @method static self NORTH()
> * @method static self EAST()
> * @method static self WEST()
> * @method static self SOUTH()
> */
> class CardinalPoint extends Enum
> {
> const NORTH = 'NORTH';
> const EAST = 'EAST';
> const WEST = 'WEST';
> const SOUTH = 'SOUTH';
> }
> ```
> This will allow us to do the following in code:
> ```php
> $direction = CardinalPoint::NORTH();
> ```
### Getting all possible values
In order to get all the possible values as an array you can use the static method `getValues`:
```php
CardinalPoint::getValues();
// Returns an array as:
// [ 'NORTH', 'EAST', 'WEST', 'SOUTH' ]
```