/**
* Get the integer value (useful for thresholds) of the severity.
*
- * @param int|const $severity
+ * @param int|string $severity
* the value to look up
* @param bool $reverse
* whether to find the constant from the integer
- * @return bool
+ * @return string|int
+ * @throws \CRM_Core_Exception
*/
public static function severityMap($severity, $reverse = FALSE) {
- // Lowercase string-based severities
- if (!$reverse) {
+ if ($reverse) {
+ if (isset(self::$severityList[$severity])) {
+ return self::$severityList[$severity];
+ }
+ }
+ else {
+ // Lowercase string-based severities
$severity = strtolower($severity);
+ if (in_array($severity, self::$severityList)) {
+ return array_search($severity, self::$severityList);
+ }
}
-
- return ($reverse) ? self::$severityList[$severity] : array_search($severity, self::$severityList);
+ throw new CRM_Core_Exception('Invalid PSR Severity Level');
}
/**
* The severity of the message. Use PSR-3 log levels.
*
* @see Psr\Log\LogLevel
+ *
+ * @throws \CRM_Core_Exception
*/
public function __construct($name, $message, $title, $level = \Psr\Log\LogLevel::WARNING, $icon = NULL) {
$this->name = $name;
$this->message = $message;
$this->title = $title;
- // Handle non-integer severity levels.
- if (!CRM_Utils_Rule::integer($level)) {
+ // Convert level to integer
+ if (!CRM_Utils_Rule::positiveInteger($level)) {
$level = CRM_Utils_Check::severityMap($level);
}
+ else {
+ // Validate numeric input - this will throw an exception if invalid
+ CRM_Utils_Check::severityMap($level, TRUE);
+ }
$this->level = $level;
$this->icon = $icon;
}