diff --git a/library/Icingadb/Common/ServiceStates.php b/library/Icingadb/Common/ServiceStates.php index 4733a372..89929957 100644 --- a/library/Icingadb/Common/ServiceStates.php +++ b/library/Icingadb/Common/ServiceStates.php @@ -17,6 +17,40 @@ class ServiceStates const PENDING = 99; + /** + * Get the integer value of the given textual service state + * + * @param string $state + * + * @return int + * + * @throws \InvalidArgumentException If the given service state is invalid, i.e. not known + */ + public static function int($state) + { + switch (strtolower($state)) { + case 'ok': + $int = self::OK; + break; + case 'warning': + $int = self::WARNING; + break; + case 'critical': + $int = self::CRITICAL; + break; + case 'unknown': + $int = self::UNKNOWN; + break; + case 'pending': + $int = self::PENDING; + break; + default: + throw new \InvalidArgumentException(sprintf('Invalid service state %d', $state)); + } + + return $int; + } + /** * Get the textual representation of the passed service state *