Skip to content

Commit 6c3abe6

Browse files
committed
OXDEV-9583: Add matches method to FloatFilter
1 parent af4f349 commit 6c3abe6

4 files changed

Lines changed: 83 additions & 18 deletions

File tree

src/DataType/Filter/AbstractNumberFilter.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,28 @@ protected function atLeastOneIsNotNull(mixed ...$values): bool
6464
return false;
6565
}
6666

67+
protected function matches(mixed $value): bool
68+
{
69+
if ($this->equals() !== null && $value !== $this->equals()) {
70+
return false;
71+
}
72+
73+
if ($this->lessThan() !== null && $value >= $this->lessThan()) {
74+
return false;
75+
}
76+
77+
if ($this->greaterThan() !== null && $value <= $this->greaterThan()) {
78+
return false;
79+
}
80+
81+
if ($this->between() !== null && !($value >= $this->between()[0] && $value <= $this->between()[1])) {
82+
return false;
83+
}
84+
85+
return true;
86+
87+
}
88+
6789
protected static function checkRangeOfBetween(?array $between, callable $checkMethod): void
6890
{
6991
if (

src/DataType/Filter/FloatFilter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public static function fromUserInput(
7575

7676
public function matches(mixed $value): bool
7777
{
78-
// TODO: Implement matches() method.
78+
if (!is_float($value)) {
79+
return false;
80+
}
81+
82+
return parent::matches($value);
7983
}
8084
}

src/DataType/Filter/IntegerFilter.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,7 @@ public function matches(mixed $value): bool
5757
return false;
5858
}
5959

60-
if ($this->equals !== null && $value !== $this->equals) {
61-
return false;
62-
}
63-
64-
if ($this->lessThan !== null && $value >= $this->lessThan) {
65-
return false;
66-
}
67-
68-
if ($this->greaterThan !== null && $value <= $this->greaterThan) {
69-
return false;
70-
}
71-
72-
if ($this->between !== null && !($value >= $this->between[0] && $value <= $this->between[1])) {
73-
return false;
74-
}
75-
76-
return true;
60+
return parent::matches($value);
7761
}
7862

7963
/**

tests/Unit/DataType/Filter/FloatFilterTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,59 @@ public function testAddQueryPartWithAlias(): void
226226

227227
$this->assertEquals('db_table_alias.DB_FIELD = :db_field_eq', (string)$where);
228228
}
229+
230+
/** @dataProvider matchesDataProvider */
231+
public function testMatches(
232+
float $stringForTrueCase,
233+
mixed $stringForFalseCase,
234+
FloatFilter $filter
235+
): void {
236+
$this->assertTrue($filter->matches($stringForTrueCase));
237+
$this->assertFalse($filter->matches($stringForFalseCase));
238+
}
239+
240+
public static function matchesDataProvider(): \Generator
241+
{
242+
yield "test match equals" => [
243+
'stringForTrueCase' => 1.0,
244+
'stringForFalseCase' => 2.0,
245+
'filter' => new FloatFilter(equals: 1.0)
246+
];
247+
248+
yield "test match less than" => [
249+
'stringForTrueCase' => 1.3,
250+
'stringForFalseCase' => 5.4,
251+
'filter' => new FloatFilter(lessThan: 3.0)
252+
];
253+
254+
yield "test match greater than" => [
255+
'stringForTrueCase' => 6.6,
256+
'stringForFalseCase' => 2.3,
257+
'filter' => new FloatFilter(greaterThan: 4.5)
258+
];
259+
260+
yield "test match between" => [
261+
'stringForTrueCase' => 5.3,
262+
'stringForFalseCase' => 2.7,
263+
'filter' => new FloatFilter(between: [3.1, 5.3])
264+
];
265+
266+
yield "test match less and greater than" => [
267+
'stringForTrueCase' => 7.8,
268+
'stringForFalseCase' => 3.3,
269+
'filter' => new FloatFilter(lessThan: 10.5, greaterThan: 4.5)
270+
];
271+
272+
yield "test not matches with float" => [
273+
'stringForTrueCase' => 2.5,
274+
'stringForFalseCase' => 2,
275+
'filter' => new FloatFilter(equals: 2.5)
276+
];
277+
278+
yield "test not matches with string" => [
279+
'stringForTrueCase' => 3.6,
280+
'stringForFalseCase' => '3,6',
281+
'filter' => new FloatFilter(equals: 3.6)
282+
];
283+
}
229284
}

0 commit comments

Comments
 (0)