Passed
Push — master ( 951d77...1f27ac )
by Alexander
02:35
created

Between   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 27
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
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\Handler;
6
7
use DateTimeInterface;
8
use InvalidArgumentException;
9
use Yiisoft\Arrays\ArrayHelper;
10
use Yiisoft\Data\Reader\FilterDataValidationHelper;
11
12
use function count;
13
14
final class Between implements IterableHandlerInterface
15
{
16 104
    public function getOperator(): string
17
    {
18 104
        return \Yiisoft\Data\Reader\Filter\Between::getOperator();
19
    }
20
21 22
    public function match(array|object $item, array $arguments, array $filterHandlers): bool
22
    {
23 22
        if (count($arguments) !== 3) {
24 4
            throw new InvalidArgumentException('$arguments should contain exactly three elements.');
25
        }
26
27
        /** @var string $field */
28 18
        [$field, $firstValue, $secondValue] = $arguments;
29 18
        FilterDataValidationHelper::assertFieldIsString($field);
30
31 10
        $value = ArrayHelper::getValue($item, $field);
32
33 10
        if (!$value instanceof DateTimeInterface) {
34 5
            return $value >= $firstValue && $value <= $secondValue;
35
        }
36
37 5
        return $firstValue instanceof DateTimeInterface
38 5
            && $secondValue instanceof DateTimeInterface
39 5
            && $value->getTimestamp() >= $firstValue->getTimestamp()
40 5
            && $value->getTimestamp() <= $secondValue->getTimestamp();
41
    }
42
}
43