Skip to content

Commit 4cd3656

Browse files
committed
Filter: Implement hasValue() & matchHasValue() helper methods
1 parent 7ea27a0 commit 4cd3656

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/Filter.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use ipl\Stdlib\Filter\Equal;
1212
use ipl\Stdlib\Filter\GreaterThan;
1313
use ipl\Stdlib\Filter\GreaterThanOrEqual;
14+
use ipl\Stdlib\Filter\HasNotValue;
15+
use ipl\Stdlib\Filter\HasValue;
1416
use ipl\Stdlib\Filter\LessThan;
1517
use ipl\Stdlib\Filter\LessThanOrEqual;
1618
use ipl\Stdlib\Filter\None;
@@ -143,6 +145,43 @@ protected function matchNone(None $rules, $row)
143145
return true;
144146
}
145147

148+
/**
149+
* Create a rule that applies rows with a column having any values, i.e. matches
150+
* all rows where this column has been set
151+
*
152+
* Performs isset() | IS NOT NULL actions
153+
*
154+
* @param string $column
155+
*
156+
* @return HasValue
157+
*/
158+
public static function hasValue($column)
159+
{
160+
return new HasValue($column, '');
161+
}
162+
163+
/**
164+
* Get whether the given rule's column contains any values
165+
*
166+
* @param HasValue|HasNotValue $rule
167+
* @param object $row
168+
*
169+
* @return bool
170+
*/
171+
protected function matchHasValue($rule, $row)
172+
{
173+
if (! $rule instanceof HasValue && ! $rule instanceof HasNotValue) {
174+
throw new InvalidArgumentException(sprintf(
175+
'Rule must be of type %s or %s, got %s instead',
176+
HasValue::class,
177+
HasNotValue::class,
178+
get_php_type($rule)
179+
));
180+
}
181+
182+
return ! empty($this->extractValue($rule->getColumn(), $row));
183+
}
184+
146185
/**
147186
* Create a rule that matches rows with a column that **equals** the given value
148187
*
@@ -408,6 +447,8 @@ protected function performMatch(Rule $rule, $row)
408447
return $this->matchNone($rule, $row);
409448
case $rule instanceof Unequal:
410449
return $this->matchUnequal($rule, $row);
450+
case $rule instanceof HasValue:
451+
return $this->matchHasValue($rule, $row);
411452
default:
412453
throw new InvalidArgumentException(sprintf(
413454
'Unable to match filter. Rule type %s is unknown',

0 commit comments

Comments
 (0)