1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Data\Reader\Iterable\Handler; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Yiisoft\Data\Reader\FilterDataValidationHelper; |
9
|
|
|
|
10
|
|
|
use function array_shift; |
11
|
|
|
use function count; |
12
|
|
|
use function is_array; |
13
|
|
|
use function is_string; |
14
|
|
|
use function sprintf; |
15
|
|
|
|
16
|
|
|
final class Not implements IterableHandlerInterface |
17
|
|
|
{ |
18
|
104 |
|
public function getOperator(): string |
19
|
|
|
{ |
20
|
104 |
|
return \Yiisoft\Data\Reader\Filter\Not::getOperator(); |
21
|
|
|
} |
22
|
|
|
|
23
|
26 |
|
public function match(array|object $item, array $arguments, array $filterHandlers): bool |
24
|
|
|
{ |
25
|
26 |
|
if (count($arguments) !== 1) { |
26
|
4 |
|
throw new InvalidArgumentException('$arguments should contain exactly one element.'); |
27
|
|
|
} |
28
|
|
|
|
29
|
22 |
|
[$values] = $arguments; |
30
|
|
|
|
31
|
22 |
|
if (!is_array($values)) { |
32
|
8 |
|
throw new InvalidArgumentException(sprintf( |
33
|
8 |
|
'The values should be array. The %s is received.', |
34
|
8 |
|
FilterDataValidationHelper::getValueType($values), |
35
|
8 |
|
)); |
36
|
|
|
} |
37
|
|
|
|
38
|
14 |
|
if (empty($values)) { |
39
|
1 |
|
throw new InvalidArgumentException('At least operator should be provided.'); |
40
|
|
|
} |
41
|
|
|
|
42
|
13 |
|
$operator = array_shift($values); |
43
|
|
|
|
44
|
13 |
|
if (!is_string($operator)) { |
45
|
5 |
|
throw new InvalidArgumentException(sprintf( |
46
|
5 |
|
'The operator should be string. The %s is received.', |
47
|
5 |
|
FilterDataValidationHelper::getValueType($operator), |
48
|
5 |
|
)); |
49
|
|
|
} |
50
|
|
|
|
51
|
8 |
|
if ($operator === '') { |
52
|
1 |
|
throw new InvalidArgumentException('The operator string cannot be empty.'); |
53
|
|
|
} |
54
|
|
|
|
55
|
7 |
|
$filterHandler = $filterHandlers[$operator] ?? null; |
56
|
|
|
|
57
|
7 |
|
if ($filterHandler === null) { |
58
|
1 |
|
throw new InvalidArgumentException(sprintf('"%s" operator is not supported.', $operator)); |
59
|
|
|
} |
60
|
|
|
|
61
|
6 |
|
FilterDataValidationHelper::assertFilterHandlerIsIterable($filterHandler); |
62
|
5 |
|
return !$filterHandler->match($item, $values, $filterHandlers); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|