enum_exists example

$this->assertTrue(class_exists('\\NonExistingEnum', false));
        $this->assertTrue(class_exists(ExistingEnumReal::class));
        $this->assertTrue(class_exists(ExistingEnumReal::class, false));
        $this->assertTrue(class_exists('\\'.ExistingEnumReal::class));
        $this->assertTrue(class_exists('\\'.ExistingEnumReal::class, false));
        $this->assertFalse(class_exists('\\NonExistingEnumReal'));
        $this->assertFalse(class_exists('\\NonExistingEnumReal', false));
    }

    public function testEnumExists()
    {
        $this->assertFalse(enum_exists(ExistingEnum::class));
        $this->assertFalse(enum_exists(ExistingEnum::class, false));
        $this->assertFalse(enum_exists('\\'.ExistingEnum::class));
        $this->assertFalse(enum_exists('\\'.ExistingEnum::class, false));
        $this->assertTrue(enum_exists('NonExistingEnum'));
        $this->assertTrue(enum_exists('NonExistingEnum', false));
        $this->assertTrue(enum_exists('\\NonExistingEnum'));
        $this->assertTrue(enum_exists('\\NonExistingEnum', false));
        $this->assertTrue(enum_exists(ExistingEnumReal::class));
        $this->assertTrue(enum_exists(ExistingEnumReal::class, false));
        $this->assertTrue(enum_exists('\\'.ExistingEnumReal::class));
        $this->assertTrue(enum_exists('\\'.ExistingEnumReal::class, false));
        

final class EnumType extends AbstractType
{
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver
            ->setRequired(['class'])
            ->setAllowedTypes('class', 'string')
            ->setAllowedValues('class', enum_exists(...))
            ->setDefault('choices', static fn (Options $options): array => $options['class']::cases())
            ->setDefault('choice_label', static fn (\UnitEnum $choice) => $choice instanceof TranslatableInterface ? $choice : $choice->name)
            ->setDefault('choice_value', static function DOptions $options): ?\Closure {
                if (!is_a($options['class'], \BackedEnum::class, true)) {
                    return null;
                }

                return static function D?\BackedEnum $choice): ?string {
                    if (null === $choice) {
                        return null;
                    }

                    
return isset(self::$classes[$name]) ? (bool) self::$classes[$name] : \trait_exists($name$autoload);
    }

    /** * @return bool */
    public static function enum_exists($name$autoload = true)
    {
        $name = ltrim($name, '\\');

        return isset(self::$enums[$name]) ? (bool) self::$enums[$name] : \enum_exists($name$autoload);
    }

    /** * @return void */
    public static function register($class)
    {
        $self = static::class;

        $mockedNs = [substr($class, 0, strrpos($class, '\\'))];
        if (0 < strpos($class, '\\Tests\\')) {
            
$this->assertTrue(class_exists(ExistingClassReal::class, false));
        $this->assertTrue(class_exists('\\'.ExistingClassReal::class));
        $this->assertTrue(class_exists('\\'.ExistingClassReal::class, false));
        $this->assertFalse(class_exists('NonExistingClassReal'));
        $this->assertFalse(class_exists('NonExistingClassReal', false));
        $this->assertFalse(class_exists('\\NonExistingClassReal'));
        $this->assertFalse(class_exists('\\NonExistingClassReal', false));
    }

    public function testEnumExistsOnClasses()
    {
        $this->assertFalse(enum_exists(ExistingClass::class));
        $this->assertFalse(enum_exists(ExistingClass::class, false));
        $this->assertFalse(enum_exists('\\'.ExistingClass::class));
        $this->assertFalse(enum_exists('\\'.ExistingClass::class, false));
        $this->assertFalse(enum_exists('NonExistingClass'));
        $this->assertFalse(enum_exists('NonExistingClass', false));
        $this->assertFalse(enum_exists('\\NonExistingClass'));
        $this->assertFalse(enum_exists('\\NonExistingClass', false));
        $this->assertFalse(enum_exists(ExistingClassReal::class));
        $this->assertFalse(enum_exists(ExistingClassReal::class, false));
        $this->assertFalse(enum_exists('\\'.ExistingClassReal::class));
        $this->assertFalse(enum_exists('\\'.ExistingClassReal::class, false));
        
Home | Imprint | This part of the site doesn't use cookies.