Passed
Push — master ( 1f27ac...52075a )
by Alexander
04:53 queued 02:24
created

Between   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 27
ccs 14
cts 14
cp 1
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B match() 0 20 7
A getOperator() 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\FilterAssert;
11
use Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface;
12
13
use function count;
14
15
/**
16
 * `Between` iterable filter handler checks that the item's field value
17
 * is between minimal and maximal values.
18
 */
19
final class Between implements IterableFilterHandlerInterface
20
{
21 104
    public function getOperator(): string
22
    {
23 104
        return \Yiisoft\Data\Reader\Filter\Between::getOperator();
24
    }
25
26 22
    public function match(array|object $item, array $arguments, array $iterableFilterHandlers): bool
27
    {
28 22
        if (count($arguments) !== 3) {
29 4
            throw new InvalidArgumentException('$arguments should contain exactly three elements.');
30
        }
31
32
        /** @var string $field */
33 18
        [$field, $minValue, $maxValue] = $arguments;
34 18
        FilterAssert::fieldIsString($field);
35
36 10
        $value = ArrayHelper::getValue($item, $field);
37
38 10
        if (!$value instanceof DateTimeInterface) {
39 5
            return $value >= $minValue && $value <= $maxValue;
40
        }
41
42 5
        return $minValue instanceof DateTimeInterface
43 5
            && $maxValue instanceof DateTimeInterface
44 5
            && $value->getTimestamp() >= $minValue->getTimestamp()
45 5
            && $value->getTimestamp() <= $maxValue->getTimestamp();
46
    }
47
}
48