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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\CatalogRule\Model\Rule\Condition;
use Magento\Framework\Exception\InputException;
use Magento\Rule\Model\Condition\ConditionInterface;
use Magento\CatalogRule\Model\Rule\Condition\Combine as CombinedCondition;
use Magento\CatalogRule\Model\Rule\Condition\Product as SimpleCondition;
use Magento\Framework\Api\CombinedFilterGroup as FilterGroup;
use Magento\Framework\Api\Filter;
use Magento\Framework\Api\SearchCriteria;
/**
* Maps catalog price rule conditions to search criteria
*/
class ConditionsToSearchCriteriaMapper
{
/**
* @var \Magento\Framework\Api\SearchCriteriaBuilderFactory
*/
private $searchCriteriaBuilderFactory;
/**
* @var \Magento\Framework\Api\CombinedFilterGroupFactory
*/
private $combinedFilterGroupFactory;
/**
* @var \Magento\Framework\Api\FilterFactory
*/
private $filterFactory;
/**
* @param \Magento\Framework\Api\SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory
* @param \Magento\Framework\Api\CombinedFilterGroupFactory $combinedFilterGroupFactory
* @param \Magento\Framework\Api\FilterFactory $filterFactory
*/
public function __construct(
\Magento\Framework\Api\SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory,
\Magento\Framework\Api\CombinedFilterGroupFactory $combinedFilterGroupFactory,
\Magento\Framework\Api\FilterFactory $filterFactory
) {
$this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory;
$this->combinedFilterGroupFactory = $combinedFilterGroupFactory;
$this->filterFactory = $filterFactory;
}
/**
* Maps catalog price rule conditions to search criteria
*
* @param CombinedCondition $conditions
* @return SearchCriteria
* @throws InputException
*/
public function mapConditionsToSearchCriteria(CombinedCondition $conditions): SearchCriteria
{
$filterGroup = $this->mapCombinedConditionToFilterGroup($conditions);
$searchCriteriaBuilder = $this->searchCriteriaBuilderFactory->create();
if ($filterGroup !== null) {
$searchCriteriaBuilder->setFilterGroups([$filterGroup]);
}
return $searchCriteriaBuilder->create();
}
/**
* Convert condition to filter group
*
* @param ConditionInterface $condition
* @return null|\Magento\Framework\Api\CombinedFilterGroup|\Magento\Framework\Api\Filter
* @throws InputException
*/
private function mapConditionToFilterGroup(ConditionInterface $condition)
{
if ($condition->getType() === CombinedCondition::class) {
return $this->mapCombinedConditionToFilterGroup($condition);
} elseif ($condition->getType() === SimpleCondition::class) {
return $this->mapSimpleConditionToFilterGroup($condition);
}
throw new InputException(
__('Undefined condition type "%1" passed in.', $condition->getType())
);
}
/**
* Convert combined condition to filter group
*
* @param Combine $combinedCondition
* @return null|\Magento\Framework\Api\CombinedFilterGroup
* @throws InputException
*/
private function mapCombinedConditionToFilterGroup(CombinedCondition $combinedCondition)
{
$filters = [];
foreach ($combinedCondition->getConditions() as $condition) {
$filter = $this->mapConditionToFilterGroup($condition);
if ($filter === null) {
continue;
}
// This required to solve cases when condition is configured like:
// "If ALL/ANY of these conditions are FALSE" - we need to reverse SQL operator for this "FALSE"
if ((bool)$combinedCondition->getValue() === false) {
$this->reverseSqlOperatorInFilter($filter);
}
$filters[] = $filter;
}
if (count($filters) === 0) {
return null;
}
return $this->createCombinedFilterGroup($filters, $combinedCondition->getAggregator());
}
/**
* Convert simple condition to filter group
*
* @param ConditionInterface $productCondition
* @return FilterGroup|Filter
* @throws InputException
*/
private function mapSimpleConditionToFilterGroup(ConditionInterface $productCondition)
{
if (is_array($productCondition->getValue())) {
return $this->processSimpleConditionWithArrayValue($productCondition);
}
return $this->createFilter(
$productCondition->getAttribute(),
(string) $productCondition->getValue(),
$productCondition->getOperator()
);
}
/**
* Convert simple condition with array value to filter group
*
* @param ConditionInterface $productCondition
* @return FilterGroup
* @throws InputException
*/
private function processSimpleConditionWithArrayValue(ConditionInterface $productCondition): FilterGroup
{
$filters = [];
foreach ($productCondition->getValue() as $subValue) {
$filters[] = $this->createFilter(
$productCondition->getAttribute(),
(string) $subValue,
$productCondition->getOperator()
);
}
$combinationMode = $this->getGlueForArrayValues($productCondition->getOperator());
return $this->createCombinedFilterGroup($filters, $combinationMode);
}
/**
* Get glue for multiple values by operator
*
* @param string $operator
* @return string
*/
private function getGlueForArrayValues(string $operator): string
{
if (in_array($operator, ['!=', '!{}', '!()'], true)) {
return 'all';
}
return 'any';
}
/**
* Reverse sql conditions to their corresponding negative analog
*
* @param Filter $filter
* @return void
* @throws InputException
*/
private function reverseSqlOperatorInFilter(Filter $filter)
{
$operatorsMap = [
'eq' => 'neq',
'neq' => 'eq',
'gteq' => 'lt',
'lteq' => 'gt',
'gt' => 'lteq',
'lt' => 'gteq',
'like' => 'nlike',
'nlike' => 'like',
'in' => 'nin',
'nin' => 'in',
];
if (!array_key_exists($filter->getConditionType(), $operatorsMap)) {
throw new InputException(
__(
'Undefined SQL operator "%1" passed in. Valid operators are: %2',
$filter->getConditionType(),
implode(',', array_keys($operatorsMap))
)
);
}
$filter->setConditionType(
$operatorsMap[$filter->getConditionType()]
);
}
/**
* Convert filters array into combined filter group
*
* @param array $filters
* @param string $combinationMode
* @return FilterGroup
* @throws InputException
*/
private function createCombinedFilterGroup(array $filters, string $combinationMode): FilterGroup
{
return $this->combinedFilterGroupFactory->create([
'data' => [
FilterGroup::FILTERS => $filters,
FilterGroup::COMBINATION_MODE => $this->mapRuleAggregatorToSQLAggregator($combinationMode)
]
]);
}
/**
* Creating of filter object by filtering params
*
* @param string $field
* @param string $value
* @param string $conditionType
* @return Filter
* @throws InputException
*/
private function createFilter(string $field, string $value, string $conditionType): Filter
{
return $this->filterFactory->create([
'data' => [
Filter::KEY_FIELD => $field,
Filter::KEY_VALUE => $value,
Filter::KEY_CONDITION_TYPE => $this->mapRuleOperatorToSQLCondition($conditionType)
]
]);
}
/**
* Maps catalog price rule operators to their corresponding operators in SQL
*
* @param string $ruleOperator
* @return string
* @throws InputException
*/
private function mapRuleOperatorToSQLCondition(string $ruleOperator): string
{
$operatorsMap = [
'==' => 'eq', // is
'!=' => 'neq', // is not
'>=' => 'gteq', // equals or greater than
'<=' => 'lteq', // equals or less than
'>' => 'gt', // greater than
'<' => 'lt', // less than
'{}' => 'like', // contains
'!{}' => 'nlike', // does not contains
'()' => 'in', // is one of
'!()' => 'nin', // is not one of
'<=>' => 'is_null'
];
if (!array_key_exists($ruleOperator, $operatorsMap)) {
throw new InputException(
__(
'Undefined rule operator "%1" passed in. Valid operators are: %2',
$ruleOperator,
implode(',', array_keys($operatorsMap))
)
);
}
return $operatorsMap[$ruleOperator];
}
/**
* Map rule combine aggregations to corresponding SQL operator
*
* @param string $ruleAggregator
* @return string
* @throws InputException
*/
private function mapRuleAggregatorToSQLAggregator(string $ruleAggregator): string
{
$operatorsMap = [
'all' => 'AND',
'any' => 'OR',
];
if (!array_key_exists(strtolower($ruleAggregator), $operatorsMap)) {
throw new InputException(
__(
'Undefined rule aggregator "%1" passed in. Valid operators are: %2',
$ruleAggregator,
implode(',', array_keys($operatorsMap))
)
);
}
return $operatorsMap[$ruleAggregator];
}
}