diff --git a/library/Icinga/Data/Filter/FilterExpression.php b/library/Icinga/Data/Filter/FilterExpression.php index ad6105cdf..87cd73392 100644 --- a/library/Icinga/Data/Filter/FilterExpression.php +++ b/library/Icinga/Data/Filter/FilterExpression.php @@ -107,6 +107,10 @@ class FilterExpression extends Filter public function __toString() { + if ($this->isBooleanTrue()) { + return $this->column; + } + $expression = is_array($this->expression) ? '( ' . implode(' | ', $this->expression) . ' )' : $this->expression; @@ -121,6 +125,10 @@ class FilterExpression extends Filter public function toQueryString() { + if ($this->isBooleanTrue()) { + return $this->column; + } + $expression = is_array($this->expression) ? '(' . implode('|', array_map('rawurlencode', $this->expression)) . ')' : rawurlencode($this->expression); @@ -128,6 +136,11 @@ class FilterExpression extends Filter return $this->column . $this->sign . $expression; } + protected function isBooleanTrue() + { + return $this->sign === '=' && $this->expression === true; + } + /** * If $var is a scalar, do the same as strtolower() would do. * If $var is an array, map $this->strtolowerRecursive() to its elements. diff --git a/test/php/library/Icinga/Data/Filter/FilterTest.php b/test/php/library/Icinga/Data/Filter/FilterTest.php index 8b9336081..ef64b5a21 100644 --- a/test/php/library/Icinga/Data/Filter/FilterTest.php +++ b/test/php/library/Icinga/Data/Filter/FilterTest.php @@ -198,6 +198,20 @@ class FilterTest extends BaseTestCase $this->assertNotEquals((string) $c, (string) $d); } + public function testBooleanExpressionIsRenderedCorrectly() + { + $filter = Filter::fromQueryString('a&!b'); + $this->assertEquals( + $filter->toQueryString(), + 'a&!b' + ); + $this->assertEquals( + (string) $filter, + // TODO: I'd prefer to see 'a & !b' here: + 'a & (! b)' + ); + } + public function testLeadingAndTrailingWhitespaces() { $columnWithWhitespaces = Filter::where(' host ', 'localhost');