-
Notifications
You must be signed in to change notification settings - Fork 0
Overview
dvlpr1996 edited this page Apr 24, 2024
·
2 revisions
The EnumHelperTrait provides methods to perform various operations on enums, including generating enum cases, converting enums to arrays, JSON, and XML, checking enum existence, and more.
PHP 8.1 or higher
You can install the php-enum-helper-trait package via Composer:
composer require dvlpr1996/php-enum-helper-trait
<?php
use dvlpr1996\PhpEnumHelperTrait\EnumHelperTrait;
enum Foo: string {
use EnumHelperTrait;
// Define your enum cases
case VALUE1 = 'value1';
case VALUE2 = 'value2';
case VALUE3 = 'value3';
// Define additional methods or properties specific to your enum if needed
public function customMethod(): string {
return 'This is a custom method for the Foo enum.';
}
}
// Example usage
$allCases = Foo::getAll();
$values = Foo::values();
$randomValue = Foo::randomValue();
$randomName = Foo::randomName();
echo "All cases: " . implode(', ', $allCases) . "\n";
echo "Values: " . implode(', ', $values) . "\n";
echo "Random value: $randomValue\n";
echo "Random name: $randomName\n";