Passed
Pull Request — master (#165)
by Alexander
05:02 queued 02:25
created

GreaterThanHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 9
dl 0
loc 20
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A match() 0 13 3
A getFilterClass() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Data\Reader\Iterable\FilterHandler;
6
7
use DateTimeInterface;
8
use Yiisoft\Arrays\ArrayHelper;
9
use Yiisoft\Data\Reader\Filter\GreaterThan;
10
use Yiisoft\Data\Reader\FilterInterface;
11
use Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface;
12
13
/**
14
 * `GreaterThan` iterable filter handler checks that the item's field value is greater than the given value.
15
 */
16
final class GreaterThanHandler implements IterableFilterHandlerInterface
17
{
18 143
    public function getFilterClass(): string
19
    {
20 143
        return GreaterThan::class;
21
    }
22
23 33
    public function match(object|array $item, FilterInterface $filter, array $iterableFilterHandlers): bool
24
    {
25
        /** @var GreaterThan $filter */
26
27 33
        $itemValue = ArrayHelper::getValue($item, $filter->field);
28 33
        $argumentValue = $filter->getValue();
29
30 33
        if (!$itemValue instanceof DateTimeInterface) {
31 30
            return $itemValue > $argumentValue;
32
        }
33
34 3
        return $argumentValue instanceof DateTimeInterface
35 3
            && $itemValue->getTimestamp() > $argumentValue->getTimestamp();
36
    }
37
}
38