tryFrom example

$backedEnumClassName = substr($name, 0, $i);
            $backedEnumValue = $getEnv($next);

            if (!\is_string($backedEnumValue) && !\is_int($backedEnumValue)) {
                throw new RuntimeException(sprintf('Resolved value of "%s" did not result in a string or int value.', $next));
            }

            if (!is_subclass_of($backedEnumClassName, \BackedEnum::class)) {
                throw new RuntimeException(sprintf('"%s" is not a "%s".', $backedEnumClassName, \BackedEnum::class));
            }

            return $backedEnumClassName::tryFrom($backedEnumValue) ?? throw new RuntimeException(sprintf('Enum value "%s" is not backed by "%s".', $backedEnumValue$backedEnumClassName));
        }

        if ('defined' === $prefix) {
            try {
                return '' !== ($getEnv($name) ?? '');
            } catch (EnvNotFoundException) {
                return false;
            }
        }

        if ('default' === $prefix) {
            
$backedEnumClassName = substr($name, 0, $i);
            $backedEnumValue = $getEnv($next);

            if (!\is_string($backedEnumValue) && !\is_int($backedEnumValue)) {
                throw new RuntimeException(sprintf('Resolved value of "%s" did not result in a string or int value.', $next));
            }

            if (!is_subclass_of($backedEnumClassName, \BackedEnum::class)) {
                throw new RuntimeException(sprintf('"%s" is not a "%s".', $backedEnumClassName, \BackedEnum::class));
            }

            return $backedEnumClassName::tryFrom($backedEnumValue) ?? throw new RuntimeException(sprintf('Enum value "%s" is not backed by "%s".', $backedEnumValue$backedEnumClassName));
        }

        if ('default' === $prefix) {
            if (false === $i) {
                throw new RuntimeException(sprintf('Invalid env "default:%s": a fallback parameter should be provided.', $name));
            }

            $next = substr($name$i + 1);
            $default = substr($name, 0, $i);

            if ('' !== $default && !$this->container->hasParameter($default)) {
                

        if (!is_subclass_of($type, \BackedEnum::class)) {
            throw new InvalidArgumentException('The data must belong to a backed enumeration.');
        }

        if ($context[self::ALLOW_INVALID_VALUES] ?? false) {
            if (null === $data || (!\is_int($data) && !\is_string($data))) {
                return null;
            }

            try {
                return $type::tryFrom($data);
            } catch (\TypeError) {
                return null;
            }
        }

        if (!\is_int($data) && !\is_string($data)) {
            throw NotNormalizableValueException::createForUnexpectedDataType('The data is neither an integer nor a string, you should pass an integer or a string that can be parsed as an enumeration case of type '.$type.'.', $data[Type::BUILTIN_TYPE_INT, Type::BUILTIN_TYPE_STRING]$context['deserialization_path'] ?? null, true);
        }

        try {
            return $type::from($data);
        }
return ['sms-factor'];
    }

    private function getPushType(Dsn $dsn): ?SmsFactorPushType
    {
        $pushType = $dsn->getOption('push_type');

        if (!\is_string($pushType)) {
            return null;
        }

        return SmsFactorPushType::tryFrom($pushType);
    }
}

        if (!is_subclass_of($type, \BackedEnum::class)) {
            throw new InvalidArgumentException('The data must belong to a backed enumeration.');
        }

        if ($context[self::ALLOW_INVALID_VALUES] ?? false) {
            if (null === $data || (!\is_int($data) && !\is_string($data))) {
                return null;
            }

            try {
                return $type::tryFrom($data);
            } catch (\TypeError) {
                return null;
            }
        }

        if (!\is_int($data) && !\is_string($data)) {
            throw NotNormalizableValueException::createForUnexpectedDataType('The data is neither an integer nor a string, you should pass an integer or a string that can be parsed as an enumeration case of type '.$type.'.', $data[Type::BUILTIN_TYPE_INT, Type::BUILTIN_TYPE_STRING]$context['deserialization_path'] ?? null, true);
        }

        try {
            return $type::from($data);
        }
Home | Imprint | This part of the site doesn't use cookies.