|
11 | 11 | use ipl\Stdlib\Filter\Equal; |
12 | 12 | use ipl\Stdlib\Filter\GreaterThan; |
13 | 13 | use ipl\Stdlib\Filter\GreaterThanOrEqual; |
| 14 | +use ipl\Stdlib\Filter\HasNotValue; |
| 15 | +use ipl\Stdlib\Filter\HasValue; |
14 | 16 | use ipl\Stdlib\Filter\LessThan; |
15 | 17 | use ipl\Stdlib\Filter\LessThanOrEqual; |
16 | 18 | use ipl\Stdlib\Filter\None; |
@@ -143,6 +145,45 @@ protected function matchNone(None $rules, $row) |
143 | 145 | return true; |
144 | 146 | } |
145 | 147 |
|
| 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 | + |
146 | 187 | /** |
147 | 188 | * Create a rule that matches rows with a column that **equals** the given value |
148 | 189 | * |
@@ -408,6 +449,8 @@ protected function performMatch(Rule $rule, $row) |
408 | 449 | return $this->matchNone($rule, $row); |
409 | 450 | case $rule instanceof Unequal: |
410 | 451 | return $this->matchUnequal($rule, $row); |
| 452 | + case $rule instanceof HasValue: |
| 453 | + return $this->matchHasValue($rule, $row); |
411 | 454 | default: |
412 | 455 | throw new InvalidArgumentException(sprintf( |
413 | 456 | 'Unable to match filter. Rule type %s is unknown', |
|
0 commit comments