FilterExpression: render boolean true as such

fixes #12299
This commit is contained in:
Thomas Gelf 2016-08-03 13:03:08 +00:00
parent 42991b0756
commit 2b50f6ff10
2 changed files with 27 additions and 0 deletions

View File

@ -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.

View File

@ -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');