|
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
|
|
|
abstract class Group implements IterableHandlerInterface |
|
17
|
|
|
{ |
|
18
|
|
|
abstract protected function checkResults(array $results): bool; |
|
19
|
|
|
|
|
20
|
73 |
|
public function match(array|object $item, array $arguments, array $filterHandlers): bool |
|
21
|
|
|
{ |
|
22
|
73 |
|
if (count($arguments) !== 1) { |
|
23
|
8 |
|
throw new InvalidArgumentException('$arguments should contain exactly one element.'); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
65 |
|
[$subFilters] = $arguments; |
|
27
|
|
|
|
|
28
|
65 |
|
if (!is_array($subFilters)) { |
|
29
|
16 |
|
throw new InvalidArgumentException(sprintf( |
|
30
|
16 |
|
'The sub filters should be array. The %s is received.', |
|
31
|
16 |
|
FilterDataValidationHelper::getValueType($subFilters), |
|
32
|
16 |
|
)); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
49 |
|
$results = []; |
|
36
|
|
|
|
|
37
|
49 |
|
foreach ($subFilters as $subFilter) { |
|
38
|
49 |
|
if (!is_array($subFilter)) { |
|
39
|
16 |
|
throw new InvalidArgumentException(sprintf( |
|
40
|
16 |
|
'The sub filter should be array. The %s is received.', |
|
41
|
16 |
|
FilterDataValidationHelper::getValueType($subFilter), |
|
42
|
16 |
|
)); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
33 |
|
if (empty($subFilter)) { |
|
46
|
2 |
|
throw new InvalidArgumentException('At least operator should be provided.'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
31 |
|
$operator = array_shift($subFilter); |
|
50
|
|
|
|
|
51
|
31 |
|
if (!is_string($operator)) { |
|
52
|
10 |
|
throw new InvalidArgumentException(sprintf( |
|
53
|
10 |
|
'The operator should be string. The %s is received.', |
|
54
|
10 |
|
FilterDataValidationHelper::getValueType($operator), |
|
55
|
10 |
|
)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
21 |
|
if ($operator === '') { |
|
59
|
2 |
|
throw new InvalidArgumentException('The operator string cannot be empty.'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
19 |
|
$filterHandler = $filterHandlers[$operator] ?? null; |
|
63
|
|
|
|
|
64
|
19 |
|
if ($filterHandler === null) { |
|
65
|
2 |
|
throw new InvalidArgumentException(sprintf('"%s" operator is not supported.', $operator)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
17 |
|
FilterDataValidationHelper::assertFilterHandlerIsIterable($filterHandler); |
|
69
|
15 |
|
$results[] = $filterHandler->match($item, $subFilter, $filterHandlers); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
15 |
|
return $this->checkResults($results); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|