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