GreaterThanOrEqualHandler::match()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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