From 4cd3656de45f05df585c0150862d5e2a33971574 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Mon, 15 Nov 2021 15:01:54 +0100 Subject: [PATCH] `Filter`: Implement `hasValue()` & `matchHasValue()` helper methods --- src/Filter.php | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/Filter.php b/src/Filter.php index 402400c..654bcc2 100644 --- a/src/Filter.php +++ b/src/Filter.php @@ -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; @@ -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 * @@ -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',