An open API service indexing awesome lists of open source software.

https://github.com/morilog/php-enum

A Library for easy working with Enums in php programming language
https://github.com/morilog/php-enum

enum morilog php

Last synced: 9 months ago
JSON representation

A Library for easy working with Enums in php programming language

Awesome Lists containing this project

README

          

### PHP-ENUM
There is not any `ENUM` in php programming language. BTW every programmer may need to this feature.

In this library i provide the simple and easy way for working with enums.

##### Installation
Run bellow command:
```bash
composer require morilog/php-enum
```

#### Usage
##### Writing new Enums
Create a class that extends the `\Morilog\PhpEnum\Enum` class

> Constants keys must be all UPPER_CASE and separate words by `_` (underscore)

> Constants values can be all scalar values such as `string`, `integer`, `boolean`, etc.
```php
key(); // APPROVED
$status->value(); // 1
$status->isApproved(); // true
$status->isRejected(); // false
```

##### By `camelCase` constant name
```php
key(); // REJECTED
$status->value(); // 2
$status->isApproved(); // false
$status->isRejected(); // true

$other = CommentStatus::somethingElse();
$other->key(); // SOMETHING_ELSE
$other->value(); // 3
$other->isApproved(); // false
$other->isRejected(); // false
$other->isSomethingElse(); // true
```

##### By `fromKey()` method
```php
1, REJECTED => 2, SOMETHING_ELSE => 3]
```

#### Comparison enums
```php
equalsTo($second); // false
$first->equalsTo($third); // true
```