Passed
Pull Request — master (#165)
by Sergei
03:21 queued 42s
created

InHandler::match()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 3
crap 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\Filter\In;
10
use Yiisoft\Data\Reader\FilterInterface;
11
use Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface;
12
13
use function in_array;
14
15
/**
16
 * `In` iterable filter handler ensures that the field value matches one of the value provided.
17
 */
18
final class InHandler implements IterableFilterHandlerInterface
19
{
20 143
    public function getFilterClass(): string
21
    {
22 143
        return In::class;
23
    }
24
25 6
    public function match(object|array $item, FilterInterface $filter, array $iterableFilterHandlers): bool
26
    {
27 6
        if (!$filter instanceof In) {
28 1
            throw new InvalidArgumentException('Incorrect filter.');
29
        }
30
31 5
        $itemValue = ArrayHelper::getValue($item, $filter->field);
32 5
        $argumentValue = $filter->getValues();
33
34 5
        return in_array($itemValue, $argumentValue);
35
    }
36
}
37