natepisarski / unwrap-enums
Safely operate on mixed enums and values
v1.0.0
2024-11-24 03:27 UTC
Requires (Dev)
- phpunit/phpunit: ^11.4
This package is auto-updated.
Last update: 2024-11-24 03:35:13 UTC
README
Provides a function to safely unwrap Enums into their constituent values.
Installation
composer require natepisarski/unwrap-enums
Usage
enum Day: string { case Monday = 'monday'; case Tuesday = 'tuesday'; case Wednesday = 'wednesday'; } unwrap_enums(Day::Monday); // 'monday' unwrap_enums([Day::Monday, Day::Tuesday]); // ['monday', 'tuesday'] unwrap_enums([Day::Monday, [Day::Tuesday, Day::Wednesday]], recursive: true); // ['monday', ['tuesday', 'wednesday']] unwrap_enums('monday'); // 'monday' - already unwrapped unwrap_enums(null); // null - nothing to unwrap
Error Handling
- A
EnumUnwrapException
can be thrown if you attempt to unwrap a non-backed Unit enum.