From 809861cb5305832605f50096575319e4dba9931f Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 11 Feb 2016 11:48:50 +0100 Subject: [PATCH] FilterExpression: make case insensitive matching possible refs #11051 --- .../Icinga/Data/Filter/FilterExpression.php | 60 ++++++++++++++++++- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/library/Icinga/Data/Filter/FilterExpression.php b/library/Icinga/Data/Filter/FilterExpression.php index bb4cdd8b4..fe10bdc76 100644 --- a/library/Icinga/Data/Filter/FilterExpression.php +++ b/library/Icinga/Data/Filter/FilterExpression.php @@ -11,12 +11,20 @@ class FilterExpression extends Filter protected $sign; protected $expression; + /** + * Does this filter compare case sensitive? + * + * @var bool + */ + protected $caseSensitive; + public function __construct($column, $sign, $expression) { $column = trim($column); $this->column = $column; $this->sign = $sign; $this->expression = $expression; + $this->caseSensitive = true; } public function isExpression() @@ -55,6 +63,14 @@ class FilterExpression extends Filter return $this->expression; } + /** + * @return bool + */ + public function getCaseSensitive() + { + return $this->caseSensitive; + } + public function setExpression($expression) { $this->expression = $expression; @@ -69,6 +85,17 @@ class FilterExpression extends Filter return $this; } + /** + * @param bool $caseSensitive + * + * @return $this + */ + public function setCaseSensitive($caseSensitive) + { + $this->caseSensitive = $caseSensitive; + return $this; + } + public function listFilteredColumns() { return array($this->getColumn()); @@ -97,6 +124,26 @@ class FilterExpression extends Filter return $this->column . $this->sign . $expression; } + /** + * If $var is a scalar, do the same as strtolower() would do. + * If $var is an array, map $this->strtolowerRecursive() to its elements. + * Otherwise, return $var unchanged. + * + * @param mixed $var + * + * @return mixed + */ + protected function strtolowerRecursive($var) + { + if ($var === null || is_scalar($var)) { + return strtolower($var); + } + if (is_array($var)) { + return array_map(array($this, 'strtolowerRecursive'), $var); + } + return $var; + } + public function matches($row) { try { @@ -106,11 +153,18 @@ class FilterExpression extends Filter return false; } - if (is_array($this->expression)) { - return in_array($rowValue, $this->expression); + if ($this->caseSensitive) { + $expression = $this->expression; + } else { + $rowValue = $this->strtolowerRecursive($rowValue); + $expression = $this->strtolowerRecursive($this->expression); } - $expression = (string) $this->expression; + if (is_array($expression)) { + return in_array($rowValue, $expression); + } + + $expression = (string) $expression; if (strpos($expression, '*') === false) { if (is_array($rowValue)) { return in_array($expression, $rowValue);