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

BetweenHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 10
dl 0
loc 21
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilterClass() 0 3 1
A match() 0 14 6
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\Between;
11
use Yiisoft\Data\Reader\FilterInterface;
12
use Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface;
13
14
/**
15
 * `Between` iterable filter handler checks that the item's field value
16
 * is between minimal and maximal values.
17
 */
18
final class BetweenHandler implements IterableFilterHandlerInterface
19
{
20 143
    public function getFilterClass(): string
21
    {
22 143
        return Between::class;
23
    }
24
25 11
    public function match(array|object $item, FilterInterface $filter, array $iterableFilterHandlers): bool
26
    {
27
        /** @var Between $filter */
28
29 11
        $value = ArrayHelper::getValue($item, $filter->field);
30
31 11
        if (!$value instanceof DateTimeInterface) {
32 6
            return $value >= $filter->minValue && $value <= $filter->maxValue;
33
        }
34
35 5
        return $filter->minValue instanceof DateTimeInterface
36 5
            && $filter->maxValue instanceof DateTimeInterface
37 5
            && $value->getTimestamp() >= $filter->minValue->getTimestamp()
38 5
            && $value->getTimestamp() <= $filter->maxValue->getTimestamp();
39
    }
40
}
41