natepisarski / random-enum
Trait to pseudo-randomly choose Enum elements
v1.0.0
2024-11-23 04:08 UTC
Requires (Dev)
- phpunit/phpunit: ^11.4
This package is auto-updated.
Last update: 2024-11-23 04:21:31 UTC
README
Utilities for selecting random elements from a PHP Enum.
Installation
composer install natepisarski/random-enum
Usage
use Natepisarski\RandomEnum\RandomEnum enum Days: string { use RandomEnum; case Sunday = 'sunday'; case Monday = 'monday'; case Tuesday = 'tuesday'; } Days::randomCase(); // === Days::Sunday Days::randomValue(); // === 'sunday' Days::randomCaseArray(count: 3, allowRepeats: false); // === [Days::Tuesday, Days::Monday, Days::Sunday] Days::randomValueArray(count: 3, allowRepeats: true); // === ['tuesday', 'monday', 'sunday']
Error Handling
The package provides 2 exceptions to handle caveats:
RandomEnumSizeException
can be thrown if::randomCase()
orrandomValue()
is called on an empty Enum- A
count
greater than the number of cases is passed to::randomCaseArray()
RandomEnumValueException
::randomValue()
/::randomValueArray
is called on an Enum with no backing values
LICENSE
MIT license, see LICENSE for more information.