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

NotHandler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 50
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getOperator() 0 3 1
B match() 0 43 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Data\Reader\Iterable\FilterHandler;
6
7
use InvalidArgumentException;
8
use Yiisoft\Data\Reader\FilterAssert;
9
use Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface;
10
11
use function array_shift;
12
use function count;
13
use function is_array;
14
use function is_string;
15
use function sprintf;
16
17
/**
18
 * `Not` iterable filter handler negates another filter.
19
 */
20
final class NotHandler implements IterableFilterHandlerInterface
21
{
22 105
    public function getOperator(): string
23
    {
24 105
        return \Yiisoft\Data\Reader\Filter\Not::getOperator();
25
    }
26
27 26
    public function match(array|object $item, array $arguments, array $iterableFilterHandlers): bool
28
    {
29 26
        if (count($arguments) !== 1) {
30 4
            throw new InvalidArgumentException('$arguments should contain exactly one element.');
31
        }
32
33 22
        [$values] = $arguments;
34
35 22
        if (!is_array($values)) {
36 8
            throw new InvalidArgumentException(sprintf(
37 8
                'The values should be array. The %s is received.',
38 8
                get_debug_type($values),
39 8
            ));
40
        }
41
42 14
        if (empty($values)) {
43 1
            throw new InvalidArgumentException('At least operator should be provided.');
44
        }
45
46 13
        $operator = array_shift($values);
47
48 13
        if (!is_string($operator)) {
49 5
            throw new InvalidArgumentException(sprintf(
50 5
                'The operator should be string. The %s is received.',
51 5
                get_debug_type($operator),
52 5
            ));
53
        }
54
55 8
        if ($operator === '') {
56 1
            throw new InvalidArgumentException('The operator string cannot be empty.');
57
        }
58
59
        /** @var mixed $filterHandler */
60 7
        $filterHandler = $iterableFilterHandlers[$operator] ?? null;
61
62 7
        if ($filterHandler === null) {
63 1
            throw new InvalidArgumentException(sprintf('"%s" operator is not supported.', $operator));
64
        }
65
66 6
        FilterAssert::isIterableFilterHandlerInterface($filterHandler);
67
        /** @var IterableFilterHandlerInterface $filterHandler */
68
69 5
        return !$filterHandler->match($item, $values, $iterableFilterHandlers);
70
    }
71
}
72