├── .github └── workflows │ └── ci.yml ├── .gitignore ├── README.md ├── composer.json ├── phpunit.xml ├── src └── Enum.php └── tests ├── EnumTest.php └── ExampleEnum.php /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: default 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ main ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | strategy: 15 | matrix: 16 | php-version: [ '7.1', '7.2', '7.3', '7.4', '8.0' ] 17 | 18 | name: Tests on PHP ${{ matrix.php-version }} 19 | 20 | steps: 21 | - name: Checkout 22 | uses: actions/checkout@v2 23 | 24 | - name: Setup PHP 25 | uses: shivammathur/setup-php@v2 26 | with: 27 | php-version: ${{ matrix.php-version }} 28 | 29 | - name: Validate composer.json and composer.lock 30 | run: composer validate 31 | 32 | - name: Cache Composer packages 33 | id: composer-cache 34 | uses: actions/cache@v2 35 | with: 36 | path: vendor 37 | key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} 38 | restore-keys: | 39 | ${{ runner.os }}-php- 40 | - name: Install dependencies 41 | if: steps.composer-cache.outputs.cache-hit != 'true' 42 | run: composer install --prefer-dist --no-progress --no-suggest 43 | 44 | - name: Run test suite 45 | run: vendor/bin/phpunit --verbose -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | /vendor 3 | .phpunit.result.cache 4 | composer.lock -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | Latest Version on Packagist 3 | Build Status 4 |

5 | 6 | ## Introduction 7 | 8 | The package offers strongly typed enums in PHP. In this package, enums are always objects, never constant values on their own. This allows for proper static analysis and refactoring in IDEs. 9 | 10 | ## Installation 11 | 12 | You can install the package via composer: 13 | 14 | ```bash 15 | composer require tailflow/enum 16 | ``` 17 | 18 | ## Usage 19 | 20 | Here is how enums are defined: 21 | 22 | ```php 23 | use Tailflow\Enum\Enum; 24 | 25 | class Status extends Enum 26 | { 27 | public const Inactive = 0; 28 | public const Active = 1; 29 | public const OnHold = 3; 30 | } 31 | ``` 32 | 33 | This is how they are used: 34 | 35 | ```php 36 | $class->setStatus(Status::Inactive); 37 | ``` 38 | 39 | ### Custom enum labels 40 | 41 | By default, enum labels are derived from the constant names. To get enum label, you can use `::getLabel` method on enum class: 42 | 43 | ```php 44 | // $label will be equal to "OnHold" - the name of the constant 45 | $label = Status::getLabel(Status::OnHold); 46 | ``` 47 | 48 | Optionally, you can provide a different label for any given enum value: 49 | 50 | ```php 51 | use Tailflow\Enum\Enum; 52 | 53 | class Status extends Enum 54 | { 55 | public const Inactive = 0; 56 | public const Active = 1; 57 | public const OnHold = 3; 58 | 59 | public static function labels(): array 60 | { 61 | return [ 62 | self::OnHold => 'waiting' 63 | ]; 64 | } 65 | } 66 | 67 | // $label will be equal to "waiting" - the custom label defined in "labels" method 68 | $label = Status::getLabel(Status::OnHold); 69 | ``` 70 | 71 | ## Listing all enum labels 72 | 73 | To get a list of all labels of the enum, you can use `::getLabels` method: 74 | 75 | ```php 76 | // $labels will contain the array - ['Inactive', 'Active', 'waiting'] 77 | $labels = Status::getLabels(); 78 | ``` 79 | 80 | ## Listing all enum values 81 | 82 | To get a list of all values of the enum, you can use `::getValues` method: 83 | 84 | ```php 85 | // $labels will contain the array - [0, 1, 3] 86 | $labels = Status::getValues(); 87 | ``` 88 | 89 | ## License 90 | 91 | The Laravel Orion is open-source software licensed under the [MIT license](https://opensource.org/licenses/MIT). 92 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tailflow/enum", 3 | "keywords": [ 4 | "tailflow", 5 | "enum", 6 | "enumerable" 7 | ], 8 | "homepage": "https://github.com/tailflow/enum", 9 | "description": "PHP Enums", 10 | "type": "library", 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Aleksei Zarubin", 15 | "email": "alex@tailflow.org" 16 | } 17 | ], 18 | "require": { 19 | "php": ">=7.1" 20 | }, 21 | "require-dev": { 22 | "phpunit/phpunit": "^7.5" 23 | }, 24 | "config": { 25 | "sort-packages": true 26 | }, 27 | "autoload": { 28 | "psr-4": { 29 | "Tailflow\\Enum\\": "src" 30 | } 31 | }, 32 | "autoload-dev": { 33 | "psr-4": { 34 | "Tailflow\\Enum\\Tests\\": "tests" 35 | } 36 | }, 37 | "support": { 38 | "issues": "https://github.com/tailflow/enum/issues", 39 | "source": "https://github.com/tailflow/enum" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | tests 16 | 17 | 18 | 19 | 20 | src/ 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/Enum.php: -------------------------------------------------------------------------------- 1 | getConstants(); 36 | 37 | foreach ($constants as $constantName => $constantValue) { 38 | if ($value === $constantValue) { 39 | return $constantName; 40 | } 41 | } 42 | 43 | throw new BadMethodCallException("There is no value {$value} defined for enum {$enumClass}, consider adding it in the class definition."); 44 | } 45 | 46 | public static function getLabels(): array 47 | { 48 | $reflectionClass = new ReflectionClass(static::class); 49 | $constants = $reflectionClass->getConstants(); 50 | 51 | $labels = array_keys($constants); 52 | 53 | return array_map(function (string $label) use ($constants) { 54 | $value = $constants[$label]; 55 | 56 | if (array_key_exists($value, static::labels())) { 57 | return static::labels()[$value]; 58 | } 59 | 60 | return $label; 61 | }, $labels); 62 | } 63 | 64 | public static function getValues(): array 65 | { 66 | $reflectionClass = new ReflectionClass(static::class); 67 | $constants = $reflectionClass->getConstants(); 68 | 69 | return array_values($constants); 70 | } 71 | 72 | } -------------------------------------------------------------------------------- /tests/EnumTest.php: -------------------------------------------------------------------------------- 1 | expectException(BadMethodCallException::class); 37 | $this->expectExceptionMessage('There is no value 4 defined for enum Tailflow\Enum\Tests\ExampleEnum, consider adding it in the class definition.'); 38 | ExampleEnum::getLabel(4); 39 | } 40 | 41 | /** @test */ 42 | public function getting_labels(): void 43 | { 44 | self::assertSame(['Inactive', 'Active', 'waiting'], ExampleEnum::getLabels()); 45 | } 46 | 47 | /** @test */ 48 | public function getting_values(): void 49 | { 50 | self::assertSame([0, 1, 3], ExampleEnum::getValues()); 51 | } 52 | } -------------------------------------------------------------------------------- /tests/ExampleEnum.php: -------------------------------------------------------------------------------- 1 | 'waiting' 19 | ]; 20 | } 21 | } --------------------------------------------------------------------------------