Skip to content

Commit bf75f7c

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

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/Filter.php

Lines changed: 43 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,45 @@ 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+
* Value is optional, as it is not used anyway when executing the query, rather
153+
* it is only being populated in the FilterEditor.
154+
*
155+
* @param string $column
156+
* @param string|null $value
157+
*
158+
* @return HasValue
159+
*/
160+
public static function hasValue($column, $value = '')
161+
{
162+
return new HasValue($column, $value);
163+
}
164+
165+
/**
166+
* Get whether the given rule's column contains any values
167+
*
168+
* @param HasValue|HasNotValue $rule
169+
* @param object $row
170+
*
171+
* @return bool
172+
*/
173+
protected function matchHasValue($rule, $row)
174+
{
175+
if (! $rule instanceof HasValue && ! $rule instanceof HasNotValue) {
176+
throw new InvalidArgumentException(sprintf(
177+
'Rule must be of type %s or %s, got %s instead',
178+
HasValue::class,
179+
HasNotValue::class,
180+
get_php_type($rule)
181+
));
182+
}
183+
184+
return ! empty($this->extractValue($rule->getColumn(), $row));
185+
}
186+
146187
/**
147188
* Create a rule that matches rows with a column that **equals** the given value
148189
*
@@ -408,6 +449,8 @@ protected function performMatch(Rule $rule, $row)
408449
return $this->matchNone($rule, $row);
409450
case $rule instanceof Unequal:
410451
return $this->matchUnequal($rule, $row);
452+
case $rule instanceof HasValue:
453+
return $this->matchHasValue($rule, $row);
411454
default:
412455
throw new InvalidArgumentException(sprintf(
413456
'Unable to match filter. Rule type %s is unknown',

0 commit comments

Comments
 (0)