Skip to content

Commit

Permalink
Merge b9782f9 into 8ece481
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Nelson committed Jan 6, 2017
2 parents 8ece481 + b9782f9 commit 8331adc
Showing 1 changed file with 41 additions and 29 deletions.
70 changes: 41 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,66 @@ also forces enums to be singletons.
Installation
------------

composer require tebru/enum
```bash
composer require tebru/enum
```

Usage
-----

To use, extend `AbstractEnum` and implement the getConstants() method.

class DirectionEnum extends AbstractEnum
```php
class DirectionEnum extends AbstractEnum
{
const NORTH = 'north';
const EAST = 'east';
const SOUTH = 'south';
const WEST = 'west';

/**
* Return an array of enum class constants
*
* @return array
*/
public static function getConstants()
{
const NORTH = 'north';
const EAST = 'east';
const SOUTH = 'south';
const WEST = 'west';

/**
* Return an array of enum class constants
*
* @return array
*/
public static function getConstants()
{
return [
self::NORTH,
self::EAST,
self::SOUTH,
self::WEST,
];
}
return [
self::NORTH,
self::EAST,
self::SOUTH,
self::WEST,
];
}
}
```

Now you can create a new instance using the static method.

DirectionEnum::create('north');
```php
DirectionEnum::create('north');
```

You can also create an instance using the __callStatic magic method.

DirectionEnum::NORTH();

```php
DirectionEnum::NORTH();
```

Add a hint to the enum doc block

/**
* @method static $this NORTH()
*/
```php
/**
* @method static $this NORTH()
*/
```

You can also iterate over the enum

$enum = new DirectionEnum('north');
foreach ($enum as $key => $value) {}
```php
$enum = new DirectionEnum('north');
foreach ($enum as $key => $value) {}
```

Reference
---------
Expand Down

0 comments on commit 8331adc

Please sign in to comment.