### Install PHP Enum Helpers via Composer Source: https://github.com/archtechx/enums/blob/master/README.md This command installs the `archtechx/enums` package using Composer, requiring PHP 8.1+. ```Shell composer require archtechx/enums ``` -------------------------------- ### Illustrate IDE Support for PHP InvokableCases Source: https://github.com/archtechx/enums/blob/master/README.md Example demonstrating how IDEs provide autosuggestions for enum cases, and appending `()` retrieves their primitive value. ```PHP MyEnum::FOO; // => MyEnum instance MyEnum::FOO(); // => 1 ``` -------------------------------- ### PHP Enum: Creating Custom Meta Properties Source: https://github.com/archtechx/enums/blob/master/README.md Provides examples for defining custom meta properties by extending `MetaProperty` and using the `#[Attribute]` annotation. It shows how to create `Color` and `Description` classes for enum metadata. ```php #[Attribute] class Color extends MetaProperty {} #[Attribute] class Description extends MetaProperty {} ``` -------------------------------- ### Get PHP Enum Options Array with options() Method Source: https://github.com/archtechx/enums/blob/master/README.md Demonstrates how to use the `options()` method to retrieve an associative array of case names and values for backed enums, or a list of names for pure enums. ```PHP TaskStatus::options(); // ['INCOMPLETE' => 0, 'COMPLETED' => 1, 'CANCELED' => 2] Role::options(); // ['ADMINISTRATOR', 'SUBSCRIBER', 'GUEST'] ``` -------------------------------- ### Get PHP Enum Case Names with names() Method Source: https://github.com/archtechx/enums/blob/master/README.md Demonstrates how to use the `names()` method to retrieve an array of all case names for both backed and pure enums. ```PHP TaskStatus::names(); // ['INCOMPLETE', 'COMPLETED', 'CANCELED'] Role::names(); // ['ADMINISTRATOR', 'SUBSCRIBER', 'GUEST'] ``` -------------------------------- ### PHP Enum: Applying the From Trait Source: https://github.com/archtechx/enums/blob/master/README.md Demonstrates how to apply the `From` trait to PHP enums, enabling conversion methods. It shows examples for both backed enums (like `TaskStatus`) and pure enums (like `Role`). ```php use ArchTech\Enums\From; enum TaskStatus: int { use From; case INCOMPLETE = 0; case COMPLETED = 1; case CANCELED = 2; } enum Role { use From; case ADMINISTRATOR; case SUBSCRIBER; case GUEST; } ``` -------------------------------- ### PHP Enum: IDE Support with Metadata Annotations Source: https://github.com/archtechx/enums/blob/master/README.md Recommends using `@method` annotations within docblocks to provide better IDE auto-completion and type hinting for metadata getter methods on enums. It shows an example with `TaskStatus`. ```php /** * @method string description() * @method string color() */ #[Meta(Description::class, Color::class)] enum TaskStatus: int { use Metadata; #[Description('Incomplete Task')] #[Color('red')] case INCOMPLETE = 0; #[Description('Completed Task')] #[Color('green')] case COMPLETED = 1; #[Description('Canceled Task')] #[Color('gray')] case CANCELED = 2; } ``` -------------------------------- ### Get PHP Enum Case Values with values() Method Source: https://github.com/archtechx/enums/blob/master/README.md Demonstrates how to use the `values()` method to retrieve an array of case values for backed enums, or case names for pure enums. ```PHP TaskStatus::values(); // [0, 1, 2] Role::values(); // ['ADMINISTRATOR', 'SUBSCRIBER', 'GUEST'] ``` -------------------------------- ### Get PHP Enum Primitive Values with Static InvokableCalls Source: https://github.com/archtechx/enums/blob/master/README.md Demonstrates how to retrieve the primitive value of both backed and pure enum cases using static invocation (`Enum::CASE()`). ```PHP TaskStatus::INCOMPLETE(); // 0 TaskStatus::COMPLETED(); // 1 TaskStatus::CANCELED(); // 2 Role::ADMINISTRATOR(); // 'ADMINISTRATOR' Role::SUBSCRIBER(); // 'SUBSCRIBER' Role::GUEST(); // 'GUEST' ``` -------------------------------- ### Get PHP Enum Primitive Values from Instances with InvokableCases Source: https://github.com/archtechx/enums/blob/master/README.md Illustrates how to retrieve the primitive value of an enum instance by invoking it as a function (`$enum()`), useful when passing enum instances to methods. ```PHP public function updateStatus(TaskStatus $status, Role $role) { $this->record->setStatus($status(), $role()); } ``` -------------------------------- ### PHP Enum: Transforming Meta Property Values Source: https://github.com/archtechx/enums/blob/master/README.md Demonstrates how to apply transformations to meta property values using the `transform()` method within the `MetaProperty` class. This example shows wrapping a color name with a CSS class string. ```php #[Attribute] class Color extends MetaProperty { protected function transform(mixed $value): mixed { return "text-{$value}-500"; } } ``` -------------------------------- ### Use PHP Enums as Array Keys with InvokableCases Source: https://github.com/archtechx/enums/blob/master/README.md Demonstrates how to use `InvokableCases` to get the primitive value of an enum case, allowing enums to serve as array keys without explicitly calling `->value`. ```PHP 'statuses' => [ TaskStatus::INCOMPLETE() => ['some configuration'], TaskStatus::COMPLETED() => ['some configuration'], ], ``` -------------------------------- ### Run Local Development Checks Source: https://github.com/archtechx/enums/blob/master/README.md Shows the shell command to execute all local development checks for the ArchTechX Enums project, including automatic code style fixes by php-cs-fixer. ```sh ./check ``` -------------------------------- ### PHP Enum: Using the fromMeta() Method Source: https://github.com/archtechx/enums/blob/master/README.md Shows how to use the `fromMeta()` method to find an enum case based on a specific metadata property and its value. It throws a `ValueError` if no matching case is found. ```php TaskStatus::fromMeta(Color::make('green')); // TaskStatus::COMPLETED TaskStatus::fromMeta(Color::make('blue')); // Error: ValueError ``` -------------------------------- ### PHP Enum: Using the tryFromMeta() Method Source: https://github.com/archtechx/enums/blob/master/README.md Illustrates the `tryFromMeta()` method for safely finding an enum case by its metadata. It returns `null` if no enum case matches the provided metadata property and value. ```php TaskStatus::tryFromMeta(Color::make('green')); // TaskStatus::COMPLETED TaskStatus::tryFromMeta(Color::make('blue')); // null ``` -------------------------------- ### PHP Enum: Using the tryFrom() Method Source: https://github.com/archtechx/enums/blob/master/README.md Shows how to use the `tryFrom()` method to safely convert a string value to an enum case. It returns `null` for invalid values instead of throwing an error. ```php Role::tryFrom('GUEST'); // Role::GUEST Role::tryFrom('NEVER'); // null ``` -------------------------------- ### Generate Custom String Representations of PHP Enum Options Source: https://github.com/archtechx/enums/blob/master/README.md Shows how to use the `stringOptions()` method with a custom callback and glue to generate a formatted string representation of enum options. ```PHP TaskStatus::stringOptions(fn ($name, $value) => "$name => $value", ', '); ``` -------------------------------- ### PHP Enum: Applying the Metadata Trait Source: https://github.com/archtechx/enums/blob/master/README.md Shows how to apply the `Metadata` trait to an enum, enabling the attachment of custom metadata properties to each case using attributes like `Description` and `Color`. It also demonstrates the `#[Meta]` attribute for registering meta properties. ```php use ArchTech\Enums\Metadata; use ArchTech\Enums\Meta\Meta; use App\Enums\MetaProperties\{Description, Color}; #[Meta(Description::class, Color::class)] enum TaskStatus: int { use Metadata; #[Description('Incomplete Task')] #[Color('red')] case INCOMPLETE = 0; #[Description('Completed Task')] #[Color('green')] case COMPLETED = 1; #[Description('Canceled Task')] #[Color('gray')] case CANCELED = 2; } ``` -------------------------------- ### PHP Enum: Using the tryFromName() Method Source: https://github.com/archtechx/enums/blob/master/README.md Demonstrates the `tryFromName()` method for safely converting a string name to an enum case. It returns `null` if the name does not correspond to an existing enum case. ```php TaskStatus::tryFromName('COMPLETED'); // TaskStatus::COMPLETED TaskStatus::tryFromName('NOTHING'); // null Role::tryFromName('GUEST'); // Role::GUEST Role::tryFromName('TESTER'); // null ``` -------------------------------- ### PHP Enum: Using the from() Method Source: https://github.com/archtechx/enums/blob/master/README.md Illustrates the usage of the `from()` method to convert a string value to an enum case. It highlights that an invalid value will throw a `ValueError`. ```php Role::from('ADMINISTRATOR'); // Role::ADMINISTRATOR Role::from('NOBODY'); // Error: ValueError ``` -------------------------------- ### PHP Enum: Using the fromName() Method Source: https://github.com/archtechx/enums/blob/master/README.md Explains how to use the `fromName()` method to convert a string representing an enum case name to the corresponding enum case. It throws a `ValueError` for non-existent names. ```php TaskStatus::fromName('INCOMPLETE'); // TaskStatus::INCOMPLETE TaskStatus::fromName('MISSING'); // Error: ValueError Role::fromName('SUBSCRIBER'); // Role::SUBSCRIBER Role::fromName('HACKER'); // Error: ValueError ``` -------------------------------- ### Apply Options Trait to PHP Enums Source: https://github.com/archtechx/enums/blob/master/README.md To enable the `options()` method, the `Options` trait must be applied to your backed or pure PHP enums. ```PHP use ArchTech\Enums\Options; enum TaskStatus: int { use Options; case INCOMPLETE = 0; case COMPLETED = 1; case CANCELED = 2; } enum Role { use Options; case ADMINISTRATOR; case SUBSCRIBER; case GUEST; } ``` -------------------------------- ### Check PHP Enum Case Inequality with `isNot()` Method Source: https://github.com/archtechx/enums/blob/master/README.md Shows how to use the `isNot()` method to determine if an enum case is different from another. It returns `true` if the cases are not the same, `false` otherwise. ```php TaskStatus::INCOMPLETE->isNot(TaskStatus::INCOMPLETE); // false TaskStatus::INCOMPLETE->isNot(TaskStatus::COMPLETED); // true Role::ADMINISTRATOR->isNot(Role::ADMINISTRATOR); // false Role::ADMINISTRATOR->isNot(Role::NOBODY); // true ``` -------------------------------- ### Apply Comparable Trait to PHP Enums Source: https://github.com/archtechx/enums/blob/master/README.md Demonstrates how to apply the `Comparable` trait from `ArchTech\Enums` to PHP enums, enabling comparison methods. This trait can be used with both backed and pure enums. ```php use ArchTech\Enums\Comparable; enum TaskStatus: int { use Comparable; case INCOMPLETE = 0; case COMPLETED = 1; case CANCELED = 2; } enum Role { use Comparable; case ADMINISTRATOR; case SUBSCRIBER; case GUEST; } ``` -------------------------------- ### Include ArchTech Enums PHPStan Extension Source: https://github.com/archtechx/enums/blob/master/README.md Provides the YAML configuration snippet to include the `ArchTech\Enums` PHPStan extension in your `phpstan.neon` file, assisting static analysis for invokable enum cases. ```yaml includes: - ./vendor/archtechx/enums/extension.neon ``` -------------------------------- ### Generate Default HTML Option Tags from PHP Enums Source: https://github.com/archtechx/enums/blob/master/README.md Illustrates how `stringOptions()` can be called without arguments to generate default HTML `