Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/oskarstark/enum-helper
https://github.com/oskarstark/enum-helper
Last synced: 5 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/oskarstark/enum-helper
- Owner: OskarStark
- License: mit
- Created: 2022-10-17T06:05:04.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-24T07:38:19.000Z (7 months ago)
- Last Synced: 2024-05-02T01:38:44.960Z (6 months ago)
- Language: PHP
- Size: 57.6 KB
- Stars: 4
- Watchers: 5
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# enum-helper
This library provides helpers for several enum operations:
* compare
* to arrayIt also provides an abstract `EnumTestCase`.
[![CI][ci_badge]][ci_link]
## Installation
```
composer require oskarstark/enum-helper
```## Usage
For example, you have the following Enum:
```php
equals(App\Enum\Color::BLUE); // returns false
``````php
App\Enum\Color::RED->notEquals(App\Enum\Color::RED); // returns false
``````php
App\Enum\Color::RED->equalsOneOf([
App\Enum\Color::BLUE,
App\Enum\Color::RED,
]); // returns true
```For example, you want to check if a color is a nice color:
```php
isNice(); // returns false
App\Enum\Color::BLUE->isNice(); // returns true
```### `ToArray` Trait
This `trait` gives you the possibility to get an enum as array like the following:
#### For Backed Enum
```php
App\Enum\Color::toArray(); // returns ['RED' => 'red', 'BLUE' => 'blue']
```#### For Non-Backed Enum
```php
App\Enum\NonBackedEnum::toArray(); // returns ['RED' => 'RED', 'BLUE' => 'BLUE']
```[ci_badge]: https://github.com/OskarStark/enum-helper/workflows/CI/badge.svg?branch=main
[ci_link]: https://github.com/OskarStark/enum-helper/actions?query=workflow:ci+branch:main