FilterExpression: Give the row a chance to dynamically return a value
refs #10304
This commit is contained in:
parent
07e5664fbe
commit
65e7f7a8ca
|
@ -3,6 +3,8 @@
|
||||||
|
|
||||||
namespace Icinga\Data\Filter;
|
namespace Icinga\Data\Filter;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
class FilterExpression extends Filter
|
class FilterExpression extends Filter
|
||||||
{
|
{
|
||||||
protected $column;
|
protected $column;
|
||||||
|
@ -97,22 +99,24 @@ class FilterExpression extends Filter
|
||||||
|
|
||||||
public function matches($row)
|
public function matches($row)
|
||||||
{
|
{
|
||||||
if (! isset($row->{$this->column})) {
|
try {
|
||||||
|
$rowValue = $row->{$this->column};
|
||||||
|
} catch (Exception $e) {
|
||||||
// TODO: REALLY? Exception?
|
// TODO: REALLY? Exception?
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_array($this->expression)) {
|
if (is_array($this->expression)) {
|
||||||
return in_array($row->{$this->column}, $this->expression);
|
return in_array($rowValue, $this->expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
$expression = (string) $this->expression;
|
$expression = (string) $this->expression;
|
||||||
if (strpos($expression, '*') === false) {
|
if (strpos($expression, '*') === false) {
|
||||||
if (is_array($row->{$this->column})) {
|
if (is_array($rowValue)) {
|
||||||
return in_array($expression, $row->{$this->column});
|
return in_array($expression, $rowValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (string) $row->{$this->column} === $expression;
|
return (string) $rowValue === $expression;
|
||||||
}
|
}
|
||||||
|
|
||||||
$parts = array();
|
$parts = array();
|
||||||
|
@ -121,8 +125,8 @@ class FilterExpression extends Filter
|
||||||
}
|
}
|
||||||
$pattern = '/^' . implode('.*', $parts) . '$/';
|
$pattern = '/^' . implode('.*', $parts) . '$/';
|
||||||
|
|
||||||
if (is_array($row->{$this->column})) {
|
if (is_array($rowValue)) {
|
||||||
foreach ($row->{$this->column} as $candidate) {
|
foreach ($rowValue as $candidate) {
|
||||||
if (preg_match($pattern, $candidate)) {
|
if (preg_match($pattern, $candidate)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -131,7 +135,7 @@ class FilterExpression extends Filter
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (bool) preg_match($pattern, $row->{$this->column});
|
return (bool) preg_match($pattern, $rowValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function andFilter(Filter $filter)
|
public function andFilter(Filter $filter)
|
||||||
|
|
Loading…
Reference in New Issue