<?php

namespace App\Traits;

trait EnumHelper
{
    public static function isValid(string $value): bool
    {
        return in_array($value, self::toArray());
    }

    public static function toArray(): array
    {
        return array_column(self::cases(), 'value');
    }

    public static function fromString(string $value): ?static
    {
        $cases = array_combine(array_map(fn ($case) => $case->value, self::cases()), self::cases());

        return data_get($cases, $value);
    }
}
