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

LessThanOrEqualHandler::match()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.9666
cc 4
nc 4
nop 3
crap 4
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\LessThanOrEqual;
11
use Yiisoft\Data\Reader\FilterInterface;
12
use Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface;
13
14
/**
15
 * `LessThanOrEqual` iterable filter handler checks that the item's field value
16
 * is less than or equal to the given value.
17
 */
18
final class LessThanOrEqualHandler implements IterableFilterHandlerInterface
19
{
20 143
    public function getFilterClass(): string
21
    {
22 143
        return LessThanOrEqual::class;
23
    }
24
25 38
    public function match(object|array $item, FilterInterface $filter, array $iterableFilterHandlers): bool
26
    {
27 38
        if (!$filter instanceof LessThanOrEqual) {
28 1
            throw new InvalidArgumentException('Incorrect filter.');
29
        }
30
31 37
        $itemValue = ArrayHelper::getValue($item, $filter->field);
32 37
        $argumentValue = $filter->getValue();
33
34 37
        if (!$itemValue instanceof DateTimeInterface) {
35 34
            return $itemValue <= $argumentValue;
36
        }
37
38 3
        return $argumentValue instanceof DateTimeInterface
39 3
            && $itemValue->getTimestamp() <= $argumentValue->getTimestamp();
40
    }
41
}
42