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

AnyHandler::getFilterClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Data\Reader\Iterable\FilterHandler;
6
7
use InvalidArgumentException;
8
use LogicException;
9
use Yiisoft\Data\Reader\Filter\Any;
10
use Yiisoft\Data\Reader\FilterInterface;
11
use Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface;
12
13
/**
14
 * `Any` iterable filter handler allows combining multiple sub-filters.
15
 * The filter matches if any of the sub-filters match.
16
 */
17
final class AnyHandler implements IterableFilterHandlerInterface
18
{
19 143
    public function getFilterClass(): string
20
    {
21 143
        return Any::class;
22
    }
23
24 9
    public function match(object|array $item, FilterInterface $filter, array $iterableFilterHandlers): bool
25
    {
26 9
        if (!$filter instanceof Any) {
27 1
            throw new InvalidArgumentException('Incorrect filter.');
28
        }
29
30 8
        foreach ($filter->getFilters() as $subFilter) {
31 8
            $filterHandler = $iterableFilterHandlers[$subFilter::class] ?? null;
32 8
            if ($filterHandler === null) {
33 1
                throw new LogicException(sprintf('Filter "%s" is not supported.', $subFilter::class));
34
            }
35 7
            if ($filterHandler->match($item, $subFilter, $iterableFilterHandlers)) {
36 6
                return true;
37
            }
38
        }
39
40 2
        return false;
41
    }
42
}
43