Passed
Pull Request — master (#165)
by Sergei
02:53
created

LessThanHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 11
c 0
b 0
f 0
dl 0
loc 22
ccs 11
cts 11
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A match() 0 15 4
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 InvalidArgumentException;
9
use Yiisoft\Arrays\ArrayHelper;
10
use Yiisoft\Data\Reader\Filter\LessThan;
11
use Yiisoft\Data\Reader\FilterInterface;
12
use Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface;
13
14
/**
15
 * `LessThan` iterable filter handler checks that the item's field value is less than the given value.
16
 */
17
final class LessThanHandler implements IterableFilterHandlerInterface
18
{
19 143
    public function getFilterClass(): string
20
    {
21 143
        return LessThan::class;
22
    }
23
24 34
    public function match(object|array $item, FilterInterface $filter, array $iterableFilterHandlers): bool
25
    {
26 34
        if (!$filter instanceof LessThan) {
27 1
            throw new InvalidArgumentException('Incorrect filter.');
28
        }
29
30 33
        $itemValue = ArrayHelper::getValue($item, $filter->field);
31 33
        $argumentValue = $filter->getValue();
32
33 33
        if (!$itemValue instanceof DateTimeInterface) {
34 30
            return $itemValue < $argumentValue;
35
        }
36
37 3
        return $argumentValue instanceof DateTimeInterface
38 3
            && $itemValue->getTimestamp() < $argumentValue->getTimestamp();
39
    }
40
}
41