Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "is null" and "is not null" relations #18

Merged
merged 2 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Enum/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ enum Relation
case CONTAINS;
case IN;
case NOT_IN;
case IS_NULL;
case IS_NOT_NULL;
}
4 changes: 3 additions & 1 deletion src/IteratorFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ private function evalString(Row $singleRow): bool
Relation::STARTS_WITH => $result[$pos] && (str_starts_with(is_null($valueparam) ? "" : $valueparam, $value)),
Relation::IN => $result[$pos] && in_array($valueparam, $value),
Relation::NOT_IN => $result[$pos] && !in_array($valueparam, $value),
Relation::IS_NULL => $result[$pos] && is_null($valueparam),
Relation::IS_NOT_NULL => $result[$pos] && !is_null($valueparam),
default => $result[$pos] && (str_contains(is_null($valueparam) ? "" : $valueparam, $value)),
};
}
Expand Down Expand Up @@ -125,7 +127,7 @@ public function addRelation(string $name, Relation $relation, mixed $value): sta
* @return static
* @desc Add a single string comparison to filter.
*/
public function and(string $name, Relation $relation, mixed $value): static
public function and(string $name, Relation $relation, mixed $value = null): static
{
$this->filters[] = [" and ", $name, $relation, $value];
return $this;
Expand Down
27 changes: 23 additions & 4 deletions tests/IteratorFilterAnydatasetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public function testMatch()
'val' => 10,
]
),

$row5 = new Row(
[
'id' => 5,
'field' => 'ab',
'field2' => 'ba',
'val' => null,
]
),
];

$this->assertEquals($collection, $this->object->match($collection));
Expand All @@ -86,7 +95,7 @@ public function testMatch()
// Test Less Than
$this->object = new IteratorFilter();
$this->object->and('val', Relation::LESS_THAN, 50);
$this->assertEquals([$row3, $row4], $this->object->match($collection));
$this->assertEquals([$row3, $row4, $row5], $this->object->match($collection));

// Test Greater or Equal Than
$this->object = new IteratorFilter();
Expand All @@ -96,12 +105,12 @@ public function testMatch()
// Test Less or Equal Than
$this->object = new IteratorFilter();
$this->object->and('val', Relation::LESS_OR_EQUAL_THAN, 50);
$this->assertEquals([$row1, $row3, $row4], $this->object->match($collection));
$this->assertEquals([$row1, $row3, $row4, $row5], $this->object->match($collection));

// Test Not Equal
$this->object = new IteratorFilter();
$this->object->and('val', Relation::NOT_EQUAL, 50);
$this->assertEquals([$row2, $row3, $row4], $this->object->match($collection));
$this->assertEquals([$row2, $row3, $row4, $row5], $this->object->match($collection));

// Test Starts With
$this->object = new IteratorFilter();
Expand All @@ -121,7 +130,17 @@ public function testMatch()
// Test Not In
$this->object = new IteratorFilter();
$this->object->and('val', Relation::NOT_IN, [10, 30, 50]);
$this->assertEquals([$row2], $this->object->match($collection));
$this->assertEquals([$row2, $row5], $this->object->match($collection));

// Test Is Null
$this->object = new IteratorFilter();
$this->object->and('val', Relation::IS_NULL);
$this->assertEquals([$row5], $this->object->match($collection));

// Test Is Not Null
$this->object = new IteratorFilter();
$this->object->and('val', Relation::IS_NOT_NULL);
$this->assertEquals([$row1, $row2, $row3, $row4], $this->object->match($collection));
}


Expand Down