forked from mallgroup/mpapi-client-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilter.php
163 lines (133 loc) · 4.06 KB
/
Filter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php declare(strict_types=1);
namespace MpApiClient\Filter;
use InvalidArgumentException;
final class Filter
{
private const FILTER_QUERY_PATTERN = '%s:%s';
private const SORT_QUERY_PATTERN = '%s:%s';
public const DIRECTION_ASC = 'asc';
public const DIRECTION_DESC = 'desc';
/**
* @var FilterItem[]
*/
private array $filterItems = [];
/**
* @var array<string, string>
*/
private array $sortColumns = [];
private int $offset = 0;
private int $limit = 100;
/**
* @return FilterItem[]
*/
public function getFilterItems(): array
{
return $this->filterItems;
}
public function resetFilter(): void
{
$this->filterItems = [];
}
/**
* Adds new FilterItem to filter, overwriting previous column filter if set
*/
public function addFilterItem(FilterItem $filterItem): void
{
$this->filterItems[$filterItem->getColumn()] = $filterItem;
}
/**
* Removes FilterItem for column from filter if set
*/
public function removeFilterItem(string $column): void
{
if (isset($this->filterItems[$column])) {
unset($this->filterItems[$column]);
}
}
/**
* @return array<string, string>
*/
public function getSortColumns(): array
{
return $this->sortColumns;
}
public function resetSort(): void
{
$this->sortColumns = [];
}
public function prependSortColumn(string $column, string $direction = self::DIRECTION_ASC): void
{
if (isset($this->sortColumns[$column])) {
unset($this->sortColumns[$column]);
}
$this->sortColumns = [$column => strtolower($direction)] + $this->sortColumns;
}
public function addSortColumn(string $column, string $direction = self::DIRECTION_ASC): void
{
if (isset($this->sortColumns[$column])) {
unset($this->sortColumns[$column]);
}
$this->sortColumns[$column] = strtolower($direction);
}
/**
* Removes column from sort if set
*/
public function removeSortColumn(string $column): void
{
if (isset($this->sortColumns[$column])) {
unset($this->sortColumns[$column]);
}
}
public function getOffset(): int
{
return $this->offset;
}
public function setOffset(int $offset): void
{
$this->offset = $offset;
}
public function getLimit(): int
{
return $this->limit;
}
/**
* @throws InvalidArgumentException
*/
public function setLimit(int $limit): void
{
if ($limit <= 0) {
throw new InvalidArgumentException(sprintf('Filter limit must be a positive number above zero, [%d] provided', $limit));
}
$this->limit = $limit;
}
/**
* @return array<string, string|int>
*/
public function buildFilterQuery(): array
{
$query = [];
foreach ($this->filterItems as $item) {
if ($item->getOperator()->equals(FilterOperatorEnum::EMPTY())) {
// column_name => value
$query[$item->getColumn()] = implode(',', $item->getValues());
} else {
// column_name => nin:value1,value2,value3
$query[$item->getColumn()] = sprintf(self::FILTER_QUERY_PATTERN, $item->getOperator()->getValue(), implode(',', $item->getValues()));
}
}
$sort = [];
foreach ($this->sortColumns as $column => $direction) {
$sort[] = sprintf(self::SORT_QUERY_PATTERN, $column, $direction);
}
if ($sort !== []) {
// _sort => column1:asc,column2:desc
$query['_sort'] = implode(',', $sort);
}
$query['page'] = (int) (floor($this->getOffset() / $this->getLimit()) + 1);
$query['page_size'] = $this->getLimit();
// MPAPI for now uses paging instead of offset and limit
// $query['_offset'] = $this->getOffset();
// $query['_limit'] = $this->getLimit();
return $query;
}
}