|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Yiisoft\Data\Reader\Iterable\Processor; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Data\Reader\Filter\FilterProcessorInterface; |
|
8
|
|
|
|
|
9
|
|
|
abstract class GroupProcessor implements IterableProcessorInterface, FilterProcessorInterface |
|
10
|
|
|
{ |
|
11
|
|
|
abstract protected function checkResults(array $result): bool; |
|
12
|
|
|
|
|
13
|
|
|
abstract protected function checkResult($result): ?bool; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* PHP variable specific execute |
|
17
|
|
|
*/ |
|
18
|
7 |
|
public function match(array $item, array $arguments, array $filterProcessors): bool |
|
19
|
|
|
{ |
|
20
|
7 |
|
if (count($arguments) < 1) { |
|
21
|
1 |
|
throw new \RuntimeException('At least one argument should be provided!'); |
|
22
|
6 |
|
} elseif (!is_array($arguments[0])) { |
|
23
|
1 |
|
throw new \RuntimeException('Sub filters is not an array!'); |
|
24
|
|
|
} |
|
25
|
5 |
|
$results = []; |
|
26
|
5 |
|
foreach ($arguments[0] as $subFilter) { |
|
27
|
5 |
|
if (!is_array($subFilter)) { |
|
28
|
1 |
|
throw new \RuntimeException('Sub filter is not an array!'); |
|
29
|
4 |
|
} elseif (count($subFilter) < 1) { |
|
30
|
1 |
|
throw new \RuntimeException('At least operator should be provided!'); |
|
31
|
|
|
} |
|
32
|
3 |
|
$operator = array_shift($subFilter); |
|
33
|
3 |
|
if (!is_string($operator)) { |
|
34
|
|
|
throw new \RuntimeException('Operator is not a string!'); |
|
35
|
3 |
|
} elseif (strlen($operator) === 0) { |
|
36
|
1 |
|
throw new \RuntimeException('The operator string cannot be empty!'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
2 |
|
$processor = $filterProcessors[$operator] ?? null; |
|
40
|
2 |
|
if ($processor === null) { |
|
41
|
|
|
throw new \RuntimeException(sprintf('"%s" operator is not supported!', $operator)); |
|
42
|
|
|
} |
|
43
|
|
|
/* @var $processor IterableProcessorInterface */ |
|
44
|
2 |
|
$result = $processor->match($item, $subFilter, $filterProcessors); |
|
45
|
2 |
|
if (is_bool($this->checkResult($result))) { |
|
46
|
2 |
|
return $result; |
|
47
|
|
|
} |
|
48
|
2 |
|
$results[] = $result; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
return $this->checkResults($results); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|