Skip to content

Commit a40c7f2

Browse files
authored
Merge pull request #18 from alalavn/master
Add "is null" and "is not null" relations
2 parents 0f0490d + e91ca18 commit a40c7f2

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

src/Enum/Relation.php

+2
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ enum Relation
1919
case CONTAINS;
2020
case IN;
2121
case NOT_IN;
22+
case IS_NULL;
23+
case IS_NOT_NULL;
2224
}

src/IteratorFilter.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ private function evalString(Row $singleRow): bool
9595
Relation::STARTS_WITH => $result[$pos] && (str_starts_with(is_null($valueparam) ? "" : $valueparam, $value)),
9696
Relation::IN => $result[$pos] && in_array($valueparam, $value),
9797
Relation::NOT_IN => $result[$pos] && !in_array($valueparam, $value),
98+
Relation::IS_NULL => $result[$pos] && is_null($valueparam),
99+
Relation::IS_NOT_NULL => $result[$pos] && !is_null($valueparam),
98100
default => $result[$pos] && (str_contains(is_null($valueparam) ? "" : $valueparam, $value)),
99101
};
100102
}
@@ -125,7 +127,7 @@ public function addRelation(string $name, Relation $relation, mixed $value): sta
125127
* @return static
126128
* @desc Add a single string comparison to filter.
127129
*/
128-
public function and(string $name, Relation $relation, mixed $value): static
130+
public function and(string $name, Relation $relation, mixed $value = null): static
129131
{
130132
$this->filters[] = [" and ", $name, $relation, $value];
131133
return $this;

tests/IteratorFilterAnydatasetTest.php

+23-4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ public function testMatch()
6060
'val' => 10,
6161
]
6262
),
63+
64+
$row5 = new Row(
65+
[
66+
'id' => 5,
67+
'field' => 'ab',
68+
'field2' => 'ba',
69+
'val' => null,
70+
]
71+
),
6372
];
6473

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

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

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

106115
// Test Starts With
107116
$this->object = new IteratorFilter();
@@ -121,7 +130,17 @@ public function testMatch()
121130
// Test Not In
122131
$this->object = new IteratorFilter();
123132
$this->object->and('val', Relation::NOT_IN, [10, 30, 50]);
124-
$this->assertEquals([$row2], $this->object->match($collection));
133+
$this->assertEquals([$row2, $row5], $this->object->match($collection));
134+
135+
// Test Is Null
136+
$this->object = new IteratorFilter();
137+
$this->object->and('val', Relation::IS_NULL);
138+
$this->assertEquals([$row5], $this->object->match($collection));
139+
140+
// Test Is Not Null
141+
$this->object = new IteratorFilter();
142+
$this->object->and('val', Relation::IS_NOT_NULL);
143+
$this->assertEquals([$row1, $row2, $row3, $row4], $this->object->match($collection));
125144
}
126145

127146

0 commit comments

Comments
 (0)