BetweenHandler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

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