Skip to content

Commit a19a2f3

Browse files
committed
improve
1 parent 107d572 commit a19a2f3

10 files changed

+91
-51
lines changed

src/Reader/Iterable/FilterHandler/BetweenHandler.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Yiisoft\Data\Reader\Iterable\FilterHandler;
66

7-
use DateTimeInterface;
87
use Yiisoft\Data\Reader\Filter\Between;
98
use Yiisoft\Data\Reader\FilterInterface;
109
use Yiisoft\Data\Reader\Iterable\Context;
@@ -29,13 +28,6 @@ public function match(object|array $item, FilterInterface $filter, Context $cont
2928
$min = $filter->minValue;
3029
$max = $filter->maxValue;
3130

32-
if (!$value instanceof DateTimeInterface) {
33-
return $value >= $min && $value <= $max;
34-
}
35-
36-
return $min instanceof DateTimeInterface
37-
&& $max instanceof DateTimeInterface
38-
&& $value->getTimestamp() >= $min->getTimestamp()
39-
&& $value->getTimestamp() <= $max->getTimestamp();
31+
return $value >= $min && $value <= $max;
4032
}
4133
}

src/Reader/Iterable/FilterHandler/EqualsHandler.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Yiisoft\Data\Reader\Iterable\FilterHandler;
66

7-
use DateTimeInterface;
87
use Yiisoft\Data\Reader\Filter\Equals;
98
use Yiisoft\Data\Reader\FilterInterface;
109
use Yiisoft\Data\Reader\Iterable\Context;
@@ -27,11 +26,6 @@ public function match(object|array $item, FilterInterface $filter, Context $cont
2726
$itemValue = $context->readValue($item, $filter->field);
2827
$argumentValue = $filter->value;
2928

30-
if (!$itemValue instanceof DateTimeInterface) {
31-
return $itemValue == $argumentValue;
32-
}
33-
34-
return $argumentValue instanceof DateTimeInterface
35-
&& $itemValue->getTimestamp() === $argumentValue->getTimestamp();
29+
return $itemValue == $argumentValue;
3630
}
3731
}

src/Reader/Iterable/FilterHandler/GreaterThanHandler.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Yiisoft\Data\Reader\Iterable\FilterHandler;
66

7-
use DateTimeInterface;
87
use Yiisoft\Data\Reader\Filter\GreaterThan;
98
use Yiisoft\Data\Reader\FilterInterface;
109
use Yiisoft\Data\Reader\Iterable\Context;
@@ -27,11 +26,6 @@ public function match(object|array $item, FilterInterface $filter, Context $cont
2726
$itemValue = $context->readValue($item, $filter->field);
2827
$argumentValue = $filter->value;
2928

30-
if (!$itemValue instanceof DateTimeInterface) {
31-
return $itemValue > $argumentValue;
32-
}
33-
34-
return $argumentValue instanceof DateTimeInterface
35-
&& $itemValue->getTimestamp() > $argumentValue->getTimestamp();
29+
return $itemValue > $argumentValue;
3630
}
3731
}

src/Reader/Iterable/FilterHandler/GreaterThanOrEqualHandler.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Yiisoft\Data\Reader\Iterable\FilterHandler;
66

7-
use DateTimeInterface;
87
use Yiisoft\Data\Reader\Filter\GreaterThanOrEqual;
98
use Yiisoft\Data\Reader\FilterInterface;
109
use Yiisoft\Data\Reader\Iterable\Context;
@@ -28,11 +27,6 @@ public function match(object|array $item, FilterInterface $filter, Context $cont
2827
$itemValue = $context->readValue($item, $filter->field);
2928
$argumentValue = $filter->value;
3029

31-
if (!$itemValue instanceof DateTimeInterface) {
32-
return $itemValue >= $argumentValue;
33-
}
34-
35-
return $argumentValue instanceof DateTimeInterface
36-
&& $itemValue->getTimestamp() >= $argumentValue->getTimestamp();
30+
return $itemValue >= $argumentValue;
3731
}
3832
}

src/Reader/Iterable/FilterHandler/LessThanHandler.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Yiisoft\Data\Reader\Iterable\FilterHandler;
66

7-
use DateTimeInterface;
87
use Yiisoft\Data\Reader\Filter\LessThan;
98
use Yiisoft\Data\Reader\FilterInterface;
109
use Yiisoft\Data\Reader\Iterable\Context;
@@ -27,11 +26,10 @@ public function match(object|array $item, FilterInterface $filter, Context $cont
2726
$itemValue = $context->readValue($item, $filter->field);
2827
$argumentValue = $filter->value;
2928

30-
if (!$itemValue instanceof DateTimeInterface) {
31-
return $itemValue < $argumentValue;
29+
if ($itemValue === null) {
30+
return false;
3231
}
3332

34-
return $argumentValue instanceof DateTimeInterface
35-
&& $itemValue->getTimestamp() < $argumentValue->getTimestamp();
33+
return $itemValue < $argumentValue;
3634
}
3735
}

src/Reader/Iterable/FilterHandler/LessThanOrEqualHandler.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@ public function match(object|array $item, FilterInterface $filter, Context $cont
2828
$itemValue = $context->readValue($item, $filter->field);
2929
$argumentValue = $filter->value;
3030

31-
if (!$itemValue instanceof DateTimeInterface) {
32-
return $itemValue <= $argumentValue;
31+
if ($itemValue === null) {
32+
return false;
3333
}
3434

35-
return $argumentValue instanceof DateTimeInterface
36-
&& $itemValue->getTimestamp() <= $argumentValue->getTimestamp();
35+
return $itemValue <= $argumentValue;
3736
}
3837
}

tests/Common/Reader/ReaderWithFilter/BaseReaderWithGreaterThanOrEqualTestCase.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,31 @@
44

55
namespace Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter;
66

7+
use DateTimeImmutable;
8+
use PHPUnit\Framework\Attributes\DataProvider;
79
use Yiisoft\Data\Reader\Filter\GreaterThanOrEqual;
810
use Yiisoft\Data\Tests\Common\Reader\BaseReaderTestCase;
911

1012
abstract class BaseReaderWithGreaterThanOrEqualTestCase extends BaseReaderTestCase
1113
{
12-
public function testWithReader(): void
14+
public static function dataWithReader(): iterable
1315
{
14-
$reader = $this->getReader()->withFilter(new GreaterThanOrEqual('balance', 500));
15-
$this->assertFixtures([3], $reader->read());
16+
yield 'integer' => [new GreaterThanOrEqual('number', 3), [3, 4, 5]];
17+
yield 'float' => [new GreaterThanOrEqual('balance', 100.0), [3, 4]];
18+
yield 'datetime' => [new GreaterThanOrEqual('born_at', new DateTimeImmutable('1990-01-01')), [5]];
19+
yield 'datetime 2' => [new GreaterThanOrEqual('born_at', new DateTimeImmutable('1991-01-01')), []];
20+
}
21+
22+
#[DataProvider('dataWithReader')]
23+
public function testWithReader(GreaterThanOrEqual $filter, array $expectedFixtureNumbers): void
24+
{
25+
$expectedFixtureIndexes = array_map(
26+
static fn(int $number): int => $number - 1,
27+
$expectedFixtureNumbers,
28+
);
29+
$this->assertFixtures(
30+
$expectedFixtureIndexes,
31+
$this->getReader()->withFilter($filter)->read(),
32+
);
1633
}
1734
}

tests/Common/Reader/ReaderWithFilter/BaseReaderWithGreaterThanTestCase.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,31 @@
44

55
namespace Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter;
66

7+
use DateTimeImmutable;
8+
use PHPUnit\Framework\Attributes\DataProvider;
79
use Yiisoft\Data\Reader\Filter\GreaterThan;
810
use Yiisoft\Data\Tests\Common\Reader\BaseReaderTestCase;
911

1012
abstract class BaseReaderWithGreaterThanTestCase extends BaseReaderTestCase
1113
{
12-
public function testWithReader(): void
14+
public static function dataWithReader(): iterable
1315
{
14-
$reader = $this->getReader()->withFilter(new GreaterThan('balance', 499));
15-
$this->assertFixtures([3], $reader->read());
16+
yield 'integer' => [new GreaterThan('number', 3), [4, 5]];
17+
yield 'float' => [new GreaterThan('balance', 50.0), [3, 4]];
18+
yield 'datetime' => [new GreaterThan('born_at', new DateTimeImmutable('1989-01-01')), [5]];
19+
yield 'datetime 2' => [new GreaterThan('born_at', new DateTimeImmutable('1990-01-01')), []];
20+
}
21+
22+
#[DataProvider('dataWithReader')]
23+
public function testWithReader(GreaterThan $filter, array $expectedFixtureNumbers): void
24+
{
25+
$expectedFixtureIndexes = array_map(
26+
static fn(int $number): int => $number - 1,
27+
$expectedFixtureNumbers,
28+
);
29+
$this->assertFixtures(
30+
$expectedFixtureIndexes,
31+
$this->getReader()->withFilter($filter)->read(),
32+
);
1633
}
1734
}

tests/Common/Reader/ReaderWithFilter/BaseReaderWithLessThanOrEqualTestCase.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,32 @@
44

55
namespace Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter;
66

7+
use DateTimeImmutable;
8+
use PHPUnit\Framework\Attributes\DataProvider;
79
use Yiisoft\Data\Reader\Filter\LessThanOrEqual;
810
use Yiisoft\Data\Tests\Common\Reader\BaseReaderTestCase;
911

1012
abstract class BaseReaderWithLessThanOrEqualTestCase extends BaseReaderTestCase
1113
{
12-
public function testWithReader(): void
14+
public static function dataWithReader(): iterable
1315
{
14-
$reader = $this->getReader()->withFilter(new LessThanOrEqual('balance', 1.0));
15-
$this->assertFixtures([1], $reader->read());
16+
yield 'integer' => [new LessThanOrEqual('number', 3), [1, 2, 3]];
17+
yield 'float' => [new LessThanOrEqual('balance', 42.0), [1, 2, 5]];
18+
yield 'datetime' => [new LessThanOrEqual('born_at', new DateTimeImmutable('1990-01-01')), [5]];
19+
yield 'datetime 2' => [new LessThanOrEqual('born_at', new DateTimeImmutable('1990-01-02')), [5]];
20+
yield 'datetime 3' => [new LessThanOrEqual('born_at', new DateTimeImmutable('1989-01-01')), []];
21+
}
22+
23+
#[DataProvider('dataWithReader')]
24+
public function testWithReader(LessThanOrEqual $filter, array $expectedFixtureNumbers): void
25+
{
26+
$expectedFixtureIndexes = array_map(
27+
static fn(int $number): int => $number - 1,
28+
$expectedFixtureNumbers,
29+
);
30+
$this->assertFixtures(
31+
$expectedFixtureIndexes,
32+
$this->getReader()->withFilter($filter)->read(),
33+
);
1634
}
1735
}

tests/Common/Reader/ReaderWithFilter/BaseReaderWithLessThanTestCase.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,31 @@
44

55
namespace Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter;
66

7+
use DateTimeImmutable;
8+
use PHPUnit\Framework\Attributes\DataProvider;
79
use Yiisoft\Data\Reader\Filter\LessThan;
810
use Yiisoft\Data\Tests\Common\Reader\BaseReaderTestCase;
911

1012
abstract class BaseReaderWithLessThanTestCase extends BaseReaderTestCase
1113
{
12-
public function testWithReader(): void
14+
public static function dataWithReader(): iterable
1315
{
14-
$reader = $this->getReader()->withFilter(new LessThan('balance', 1.1));
15-
$this->assertFixtures([1], $reader->read());
16+
yield 'integer' => [new LessThan('number', 3), [1, 2]];
17+
yield 'float' => [new LessThan('balance', 50.0), [1, 2, 5]];
18+
yield 'datetime' => [new LessThan('born_at', new DateTimeImmutable('1991-01-01')), [5]];
19+
yield 'datetime 2' => [new LessThan('born_at', new DateTimeImmutable('1990-01-01')), []];
20+
}
21+
22+
#[DataProvider('dataWithReader')]
23+
public function testWithReader(LessThan $filter, array $expectedFixtureNumbers): void
24+
{
25+
$expectedFixtureIndexes = array_map(
26+
static fn(int $number): int => $number - 1,
27+
$expectedFixtureNumbers,
28+
);
29+
$this->assertFixtures(
30+
$expectedFixtureIndexes,
31+
$this->getReader()->withFilter($filter)->read(),
32+
);
1633
}
1734
}

0 commit comments

Comments
 (0)