|
4 | 4 |
|
5 | 5 | namespace Kiboko\Component\Flow\Magento2; |
6 | 6 |
|
| 7 | +use Kiboko\Component\Flow\Magento2\Filter\FilterInterface; |
| 8 | +use Kiboko\Component\Flow\Magento2\Filter\ScalarFilter; |
| 9 | + |
7 | 10 | class FilterGroup |
8 | 11 | { |
9 | 12 | private array $filters = []; |
10 | | - private array $longFilters = []; |
11 | | - private int $offset = 0; |
12 | | - private int $lenght = 200; |
13 | 13 |
|
14 | | - public function withFilter(Filter $filter): self |
| 14 | + public function withFilter(FilterInterface $filter): self |
15 | 15 | { |
16 | | - $this->filters[] = [ |
17 | | - 'field' => $filter->field, |
18 | | - 'value' => $filter->value, |
19 | | - 'condition_type' => $filter->conditionType, |
20 | | - ]; |
| 16 | + $this->filters[] = $filter; |
21 | 17 |
|
22 | 18 | return $this; |
23 | 19 | } |
24 | 20 |
|
25 | | - public function withLongFilter(Filter $filter, int $offset = 0, int $lenght = 200): self |
| 21 | + /** |
| 22 | + * @param array $parameters |
| 23 | + * @param int $groupIndex |
| 24 | + * @return \Traversable<int, array> |
| 25 | + */ |
| 26 | + public function walkFilters(array $parameters, int $groupIndex = 0): \Traversable |
26 | 27 | { |
27 | | - $this->longFilters[] = [ |
28 | | - 'field' => $filter->field, |
29 | | - 'value' => $filter->value, |
30 | | - 'condition_type' => $filter->conditionType, |
31 | | - ]; |
32 | | - |
33 | | - $this->offset = $offset; |
34 | | - $this->lenght = $lenght; |
| 28 | + if (count($this->filters) < 1) { |
| 29 | + return; |
| 30 | + } |
35 | 31 |
|
36 | | - return $this; |
| 32 | + yield from $this->buildFilters($parameters, $groupIndex, 1, ...$this->filters); |
37 | 33 | } |
38 | 34 |
|
39 | | - public function withFilters(Filter ...$filters): self |
| 35 | + private function buildFilters(array $parameters, int $groupIndex, int $filterIndex, FilterInterface $first, FilterInterface ...$next): \Traversable |
40 | 36 | { |
41 | | - array_walk($filters, fn (Filter $filter) => $this->filters[] = [ |
42 | | - 'field' => $filter->field, |
43 | | - 'value' => $filter->value, |
44 | | - 'condition_type' => $filter->conditionType, |
45 | | - ]); |
| 37 | + foreach ($first as $current) { |
| 38 | + $childParameters = [ |
| 39 | + ...$parameters, |
| 40 | + ...[ |
| 41 | + sprintf('searchCriteria[filterGroups][%s][filters][%s][field]', $groupIndex, $filterIndex) => $current['field'], |
| 42 | + sprintf('searchCriteria[filterGroups][%s][filters][%s][value]', $groupIndex, $filterIndex) => $current['value'], |
| 43 | + sprintf('searchCriteria[filterGroups][%s][filters][%s][conditionType]', $groupIndex, $filterIndex) => $current['conditionType'], |
| 44 | + ] |
| 45 | + ]; |
46 | 46 |
|
47 | | - return $this; |
48 | | - } |
49 | | - |
50 | | - private function sliceFilter(string $value): iterable |
51 | | - { |
52 | | - $iterator = new \ArrayIterator(explode(',', $value)); |
53 | | - while ($this->offset < iterator_count($iterator)) { |
54 | | - $filteredValue = \array_slice(iterator_to_array($iterator), $this->offset, $this->lenght); |
55 | | - $this->offset += $this->lenght; |
56 | | - yield $filteredValue; |
| 47 | + if (count($next) >= 1) { |
| 48 | + yield from $this->buildFilters($childParameters, $groupIndex, $filterIndex + 1, ...$next); |
| 49 | + } else { |
| 50 | + yield $childParameters; |
| 51 | + } |
57 | 52 | } |
58 | 53 | } |
59 | 54 |
|
60 | | - public function compileLongFilters(int $groupIndex = 0) |
| 55 | + public function greaterThan(string $field, int|float|string|\DateTimeInterface $value): self |
61 | 56 | { |
62 | | - return array_merge(...array_map(fn (array $item, int $key) => [ |
63 | | - sprintf('searchCriteria[filterGroups][%s][filters][%s][field]', $groupIndex, $key) => $item['field'], |
64 | | - sprintf('searchCriteria[filterGroups][%s][filters][%s][value]', $groupIndex, $key) => iterator_to_array($this->sliceFilter($item['value'])), |
65 | | - sprintf('searchCriteria[filterGroups][%s][filters][%s][conditionType]', $groupIndex, $key) => $item['condition_type'], |
66 | | - ], $this->longFilters, array_keys($this->longFilters))); |
| 57 | + return $this->withFilter(new ScalarFilter($field, 'gt', $value)); |
67 | 58 | } |
68 | 59 |
|
69 | | - public function compileFilters(int $groupIndex = 0): array |
| 60 | + public function lowerThan(string $field, int|float|string|\DateTimeInterface $value): self |
70 | 61 | { |
71 | | - return array_merge(...array_map(fn (array $item, int $key) => [ |
72 | | - sprintf('searchCriteria[filterGroups][%s][filters][%s][field]', $groupIndex, $key) => $item['field'], |
73 | | - sprintf('searchCriteria[filterGroups][%s][filters][%s][value]', $groupIndex, $key) => $item['value'], |
74 | | - sprintf('searchCriteria[filterGroups][%s][filters][%s][conditionType]', $groupIndex, $key) => $item['condition_type'], |
75 | | - ], $this->filters, array_keys($this->filters))); |
| 62 | + return $this->withFilter(new ScalarFilter($field, 'lt', $value)); |
76 | 63 | } |
77 | 64 |
|
78 | | - public function greaterThan(string $field, mixed $value): self |
| 65 | + public function greaterThanOrEqual(string $field, int|float|string|\DateTimeInterface $value): self |
79 | 66 | { |
80 | | - return $this->withFilter(new Filter($field, 'gt', $value)); |
| 67 | + return $this->withFilter(new ScalarFilter($field, 'gteq', $value)); |
81 | 68 | } |
82 | 69 |
|
83 | | - public function greaterThanEqual(string $field, mixed $value): self |
| 70 | + public function lowerThanOrEqual(string $field, int|float|string|\DateTimeInterface $value): self |
84 | 71 | { |
85 | | - return $this->withFilter(new Filter($field, 'gteq', $value)); |
| 72 | + return $this->withFilter(new ScalarFilter($field, 'lteq', $value)); |
86 | 73 | } |
87 | 74 | } |
0 commit comments