Passed
Push — master ( fb6eb5...474c12 )
by Alexander
13:08
created

Between::match()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 13
nc 8
nop 3
dl 0
loc 22
ccs 12
cts 12
cp 1
crap 8
rs 8.4444
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Data\Reader\Iterable\Processor;
6
7
use DateTimeInterface;
8
use InvalidArgumentException;
9
use Yiisoft\Data\Reader\Filter\FilterProcessorInterface;
10
use Yiisoft\Data\Reader\FilterDataValidationHelper;
11
12
use function array_key_exists;
13
use function count;
14
15
class Between implements IterableProcessorInterface, FilterProcessorInterface
16
{
17 103
    public function getOperator(): string
18
    {
19 103
        return \Yiisoft\Data\Reader\Filter\Between::getOperator();
20
    }
21
22 24
    public function match(array $item, array $arguments, array $filterProcessors): bool
23
    {
24 24
        if (count($arguments) !== 3) {
25 4
            throw new InvalidArgumentException('$arguments should contain exactly three elements.');
26
        }
27
28
        /** @var string $field */
29 20
        [$field, $firstValue, $secondValue] = $arguments;
30 20
        FilterDataValidationHelper::assertFieldIsString($field);
31
32 12
        if (!array_key_exists($field, $item)) {
33 2
            return false;
34
        }
35
36 10
        if (!$item[$field] instanceof DateTimeInterface) {
37 5
            return $item[$field] >= $firstValue && $item[$field] <= $secondValue;
38
        }
39
40 5
        return $firstValue instanceof DateTimeInterface
41
            && $secondValue instanceof DateTimeInterface
42 5
            && $item[$field]->getTimestamp() >= $firstValue->getTimestamp()
43 5
            && $item[$field]->getTimestamp() <= $secondValue->getTimestamp();
44
    }
45
}
46