Passed
Push — master ( cc7bc9...273064 )
by Sergei
18:45 queued 16:00
created

CompareHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 23
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A match() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Data\Reader\Iterable\FilterHandler;
6
7
use InvalidArgumentException;
8
use Yiisoft\Arrays\ArrayHelper;
9
use Yiisoft\Data\Reader\FilterAssert;
10
use Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface;
11
12
use function count;
13
14
/**
15
 * Abstract `Compare` iterable filter handler compares item's field value with a given value.
16
 * The actual comparison is defined in {@see compare()} method implemented in child classes.
17
 */
18
abstract class CompareHandler implements IterableFilterHandlerInterface
19
{
20
    /**
21
     * Compare item's field value with a given value.
22
     *
23
     * @param mixed $itemValue Value of the item to compare.
24
     * @param mixed $argumentValue Value to compare with.
25
     *
26
     * @return bool If the comparison is true.
27
     */
28
    abstract protected function compare(mixed $itemValue, mixed $argumentValue): bool;
29
30 165
    public function match(array|object $item, array $arguments, array $iterableFilterHandlers): bool
31
    {
32 165
        if (count($arguments) !== 2) {
33 28
            throw new InvalidArgumentException('$arguments should contain exactly two elements.');
34
        }
35
36 137
        [$field, $value] = $arguments;
37 137
        FilterAssert::fieldIsString($field);
38
39
        /** @var string $field */
40 81
        return $this->compare(ArrayHelper::getValue($item, $field), $value);
41
    }
42
}
43