Skip to content

Commit

Permalink
Filter: Implement hasValue() & matchHasValue() helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Nov 15, 2021
1 parent 7ea27a0 commit 4cd3656
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use ipl\Stdlib\Filter\Equal;
use ipl\Stdlib\Filter\GreaterThan;
use ipl\Stdlib\Filter\GreaterThanOrEqual;
use ipl\Stdlib\Filter\HasNotValue;
use ipl\Stdlib\Filter\HasValue;
use ipl\Stdlib\Filter\LessThan;
use ipl\Stdlib\Filter\LessThanOrEqual;
use ipl\Stdlib\Filter\None;
Expand Down Expand Up @@ -143,6 +145,43 @@ protected function matchNone(None $rules, $row)
return true;
}

/**
* Create a rule that applies rows with a column having any values, i.e. matches
* all rows where this column has been set
*
* Performs isset() | IS NOT NULL actions
*
* @param string $column
*
* @return HasValue
*/
public static function hasValue($column)
{
return new HasValue($column, '');
}

/**
* Get whether the given rule's column contains any values
*
* @param HasValue|HasNotValue $rule
* @param object $row
*
* @return bool
*/
protected function matchHasValue($rule, $row)
{
if (! $rule instanceof HasValue && ! $rule instanceof HasNotValue) {
throw new InvalidArgumentException(sprintf(
'Rule must be of type %s or %s, got %s instead',
HasValue::class,
HasNotValue::class,
get_php_type($rule)
));
}

return ! empty($this->extractValue($rule->getColumn(), $row));
}

/**
* Create a rule that matches rows with a column that **equals** the given value
*
Expand Down Expand Up @@ -408,6 +447,8 @@ protected function performMatch(Rule $rule, $row)
return $this->matchNone($rule, $row);
case $rule instanceof Unequal:
return $this->matchUnequal($rule, $row);
case $rule instanceof HasValue:
return $this->matchHasValue($rule, $row);
default:
throw new InvalidArgumentException(sprintf(
'Unable to match filter. Rule type %s is unknown',
Expand Down

0 comments on commit 4cd3656

Please sign in to comment.