diff --git a/src/ncc/CLI/Main.php b/src/ncc/CLI/Main.php index a243cc3..dc60114 100644 --- a/src/ncc/CLI/Main.php +++ b/src/ncc/CLI/Main.php @@ -91,28 +91,11 @@ if(isset(self::$args['l']) || isset(self::$args['log-level'])) { - switch(strtolower(self::$args['l'] ?? self::$args['log-level'])) - { - // TODO: Fix this, it's not casting correctly to the enum type but rather as a string - case LogLevel::SILENT->value: - case LogLevel::FATAL->value: - case LogLevel::ERROR->value: - case LogLevel::WARNING->value: - case LogLevel::INFO->value: - case LogLevel::DEBUG->value: - case LogLevel::VERBOSE->value: - self::$log_level = strtolower(self::$args['l'] ?? self::$args['log-level']); - break; - - default: - Console::outWarning('Unknown log level: ' . (self::$args['l'] ?? self::$args['log-level']) . ', using \'info\''); - self::$log_level = LogLevel::INFO->value; - break; - } + self::$log_level = LogLevel::fromOrDefault(strtolower(self::$args['l'] ?? self::$args['log-level'])); } else { - self::$log_level = LogLevel::INFO->value; + self::$log_level = LogLevel::INFO; } if(Resolver::checkLogLevel(self::$log_level, LogLevel::DEBUG)) diff --git a/src/ncc/Enums/LogLevel.php b/src/ncc/Enums/LogLevel.php index 45637e0..1fca8dc 100644 --- a/src/ncc/Enums/LogLevel.php +++ b/src/ncc/Enums/LogLevel.php @@ -37,4 +37,23 @@ case ERROR = 'error'; case FATAL = 'fatal'; + + /** + * Converts the given string input to a LogLevel. + * If the input is invalid or not found, it defaults to LogLevel::INFO. + * + * @param string $input The input string to be converted to a LogLevel. + * @return LogLevel Returns the corresponding LogLevel for the input string or LogLevel::INFO if not found. + */ + public static function fromOrDefault(string $input): LogLevel + { + $value = self::tryFrom($input); + + if($value === null) + { + return self::INFO; + } + + return $value; + } } \ No newline at end of file