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

EqualsHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 9
dl 0
loc 20
ccs 9
cts 9
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 13 3
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\Equals;
11
use Yiisoft\Data\Reader\FilterInterface;
12
use Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface;
13
14
/**
15
 * `Equals` iterable filter handler checks that the item's field value matches given value.
16
 */
17
final class EqualsHandler implements IterableFilterHandlerInterface
18
{
19 143
    public function getFilterClass(): string
20
    {
21 143
        return Equals::class;
22
    }
23
24 29
    public function match(object|array $item, FilterInterface $filter, array $iterableFilterHandlers): bool
25
    {
26
        /** @var Equals $filter */
27
28 29
        $itemValue = ArrayHelper::getValue($item, $filter->field);
29 29
        $argumentValue = $filter->getValue();
30
31 29
        if (!$itemValue instanceof DateTimeInterface) {
32 26
            return $itemValue == $argumentValue;
33
        }
34
35 3
        return $argumentValue instanceof DateTimeInterface
36 3
            && $itemValue->getTimestamp() === $argumentValue->getTimestamp();
37
    }
38
}
39