Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/othyn/php-enum-enhancements

Adds some helpful enum traits to the glorious new PHP Enum type, like value lists and value arrays.
https://github.com/othyn/php-enum-enhancements

php php-enum php-extension php-extensions php-library php8 php81

Last synced: 3 days ago
JSON representation

Adds some helpful enum traits to the glorious new PHP Enum type, like value lists and value arrays.

Awesome Lists containing this project

README

        

# PHP Enum Enhancements

[![Tests](https://github.com/othyn/php-enum-enhancements/actions/workflows/phpunit.yml/badge.svg)](https://github.com/othyn/php-enum-enhancements/actions/workflows/phpunit.yml)
[![Code Style](https://github.com/othyn/php-enum-enhancements/actions/workflows/phpcsfixer.yml/badge.svg)](https://github.com/othyn/php-enum-enhancements/actions/workflows/phpcsfixer.yml)
[![Downloads](https://img.shields.io/packagist/dt/othyn/php-enum-enhancements?color=green)](#installation)
[![GitHub license](https://img.shields.io/github/license/othyn/php-enum-enhancements)](https://github.com/othyn/php-enum-enhancements/blob/main/LICENSE)
[![Love](https://img.shields.io/badge/built%20with-love-red)](https://img.shields.io/badge/built%20with-love-red)

A [Composer](https://getcomposer.org/) package for [PHP](https://www.php.net/) that adds some helpful enum traits to the glorious new PHP [Enum](https://www.php.net/manual/en/language.enumerations.php) type.

The package so far provides;

- A handy trait that extends PHP's native Enum type
- Adds a new static `UnitEnum::valueArray(): array` method that returns all values within an Enum as an equally typed array of Enum values
- Adds a new static `UnitEnum::valueList(string $separator = ', '): string` method that returns all values within an Enum as a comma separated list string

The package is available on Packagist as [othyn/php-enum-enhancements](https://packagist.org/packages/othyn/php-enum-enhancements).

---

## Installation

Hop into your project that you wish to install it in and run the following Composer command to grab the latest version:

```sh
composer require othyn/php-enum-enhancements
```

---

## Usage

For more comprehensive usage examples, you can view the test suite. However I'll show some basic usage examples below.

### Enum: Value Array

```php

// string(5) "Alpha"
// [1]=>
// string(5) "Bravo"
// [2]=>
// string(7) "Charlie"
// [3]=>
// string(5) "Delta"
// [4]=>
// string(4) "Echo"
// }
```

### Enum: Value List

```php

// string(5) "alpha"
// [1]=>
// string(5) "bravo"
// [2]=>
// string(7) "charlie"
// [3]=>
// string(5) "delta"
// [4]=>
// string(4) "echo"
// }
```

### Backed String Enum: Value List

```php

// int(1)
// [1]=>
// int(2)
// [2]=>
// int(3)
// [3]=>
// int(4)
// [4]=>
// int(5)
// }
```

### Backed Int Enum: Value List

```php