I receive as input an array of values.
Example : [3,2,3,1]
I need as output an array of enums like this : [Fruits::Banana, Fruits::Pear, Fruits::Banana, Fruits::Apple ]
I Try to do a generic function to do this but I got an error because $enumClassName::class does not work...
function get_ennum_array_from_values( array $values, string $enumClassName ){
$array_of_enum = [];
foreach ( $values as $value ){
if( is_numeric( $value ) ){
$fvalue = (int) $value;
}else{
$fvalue = $value;
}
$enum = new ReflectionEnum($enumClassName::class);
if (enum_exists($enum)) {
$array_of_enum[] = $enum::tryFrom($fvalue);
}
}
return $array_of_enum;
}
This how I would like to use it :
enum Fruits: int {
case Apple = 1;
case Pear = 2;
case Banana = 3;
}
$array_of_enums = get_ennum_array_from_values( [3,2,3,1], 'Fruits' );
How can I do this or fix this function please ?