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

AllHandler::match()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

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