tourze / enum-extra
PHP枚举增强
Installs: 24 404
Dependents: 86
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/tourze/enum-extra
Requires
- php: ^8.2
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
- tourze/phpunit-enum: 1.*
README
A PHP package that enhances PHP 8.1+ enums with additional functionality, providing commonly used enum extensions for easier data handling and UI integration.
Features
- Convert enum cases to select options with custom labels
- Environment-based option filtering with enum-display:{enum}-{value}
- Array conversion utilities for easy data transformation
- Interface support for flexible implementation
- Type-safe enum operations
- Boolean enum implementation with useful helpers
- Tree data structure support for hierarchical data
- Data fetcher interfaces for standardized data retrieval
- Badge interface for EasyAdminBundle integration
Requirements
- PHP 8.1 or higher
Installation
composer require tourze/enum-extra
Quick Start
use Tourze\EnumExtra\Itemable; use Tourze\EnumExtra\ItemTrait; use Tourze\EnumExtra\Labelable; use Tourze\EnumExtra\Selectable; use Tourze\EnumExtra\SelectTrait; enum Status: string implements Labelable, Itemable, Selectable { use ItemTrait; use SelectTrait; case ACTIVE = 'active'; case INACTIVE = 'inactive'; public function getLabel(): string { return match($this) { self::ACTIVE => 'Active', self::INACTIVE => 'Inactive', }; } } // Generate select options $options = Status::genOptions(); // Result: [['label' => 'Active', 'text' => 'Active', 'value' => 'active', 'name' => 'Active'], ...] // Convert single case to array $array = Status::ACTIVE->toArray(); // Result: ['value' => 'active', 'label' => 'Active']
Available Interfaces
Labelable
This interface provides a way to add human-readable labels to enum cases.
interface Labelable { public function getLabel(): string; }
Itemable
This interface allows converting enum cases to select option items.
interface Itemable { public function toSelectItem(): array; }
Selectable
This interface provides methods to generate select options from enum cases.
interface Selectable { public static function genOptions(): array; }
SelectDataFetcher
This interface standardizes data fetching for select components.
interface SelectDataFetcher { public function genSelectData(): iterable; }
TreeDataFetcher
This interface provides methods to generate hierarchical tree data.
interface TreeDataFetcher { public function genTreeData(): array; }
BadgeInterface
This interface provides badge constants for EasyAdminBundle integration.
interface BadgeInterface { public const SUCCESS = 'success'; public const WARNING = 'warning'; public const DANGER = 'danger'; public const INFO = 'info'; public const PRIMARY = 'primary'; public const SECONDARY = 'secondary'; public const LIGHT = 'light'; public const DARK = 'dark'; public const OUTLINE = 'outline'; public function getBadge(): string; }
Features in Detail
Select Options Generation
Convert enum cases to select options format for UI components:
$options = Status::genOptions();
You can filter options using environment variables:
// In your .env file or server configuration $_ENV['enum-display:App\\Enums\\Status-inactive'] = false; // Now Status::genOptions() will exclude the INACTIVE option
BoolEnum
A ready-to-use boolean enum implementation:
use Tourze\EnumExtra\BoolEnum; $value = BoolEnum::YES; $boolValue = $value->toBool(); // true // Generate boolean options $options = BoolEnum::genBoolOptions(); // Result: [['label' => '是', 'text' => '是', 'value' => true, 'name' => '是'], ...]
Array Conversion
Convert enum cases to array format for easy serialization:
$array = Status::ACTIVE->toArray(); // Result: ['value' => 'active', 'label' => 'Active']
Badge Integration
Use badge interface for EasyAdminBundle styling:
use Tourze\EnumExtra\BadgeInterface; enum UserStatus: string implements BadgeInterface, Labelable { case ACTIVE = 'active'; case INACTIVE = 'inactive'; case BANNED = 'banned'; public function getLabel(): string { return match($this) { self::ACTIVE => 'Active', self::INACTIVE => 'Inactive', self::BANNED => 'Banned', }; } public function getBadge(): string { return match($this) { self::ACTIVE => self::SUCCESS, self::INACTIVE => self::WARNING, self::BANNED => self::DANGER, }; } }
Contributing
Please see CONTRIBUTING for details.
License
The MIT License (MIT). Please see License File for more information.