-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQueryConditionGenerator.php
745 lines (614 loc) · 28.5 KB
/
QueryConditionGenerator.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
<?php
declare(strict_types=1);
/*
* This file is part of the RollerworksSearch package.
*
* (c) Sebastiaan Stok <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Rollerworks\Component\Search\Elasticsearch;
use Elastica\Query;
use Rollerworks\Component\Search\Exception\BadMethodCallException;
use Rollerworks\Component\Search\Exception\UnknownFieldException;
use Rollerworks\Component\Search\ParameterBag;
use Rollerworks\Component\Search\SearchCondition;
use Rollerworks\Component\Search\SearchOrder;
use Rollerworks\Component\Search\Value\Compare;
use Rollerworks\Component\Search\Value\ExcludedRange;
use Rollerworks\Component\Search\Value\PatternMatch;
use Rollerworks\Component\Search\Value\Range;
use Rollerworks\Component\Search\Value\ValuesGroup;
/* final */ class QueryConditionGenerator implements ConditionGenerator
{
private const PROPERTY_ID = '_id';
// Elasticsearch general query elements
public const SORT = 'sort';
public const SORT_ORDER = 'order';
public const SORT_SCORE = '_score';
public const QUERY = 'query';
public const QUERY_BOOL = 'bool';
public const QUERY_IDS = 'ids';
public const QUERY_NESTED = 'nested';
public const QUERY_TYPE = 'type';
public const QUERY_PATH = 'path';
public const QUERY_HAS_CHILD = 'has_child';
public const QUERY_MATCH = 'match';
public const QUERY_PREFIX = 'prefix';
public const QUERY_RANGE = 'range';
public const QUERY_WILDCARD = 'wildcard';
public const QUERY_TERM = 'term';
public const QUERY_TERMS = 'terms';
public const QUERY_VALUE = 'value';
public const QUERY_VALUES = 'values';
public const QUERY_FUNCTION_SCORE = 'function_score';
public const QUERY_SCRIPT_SCORE = 'script_score';
public const QUERY_SCRIPT = 'script';
// Elasticsearch boolean operators
public const CONDITION_NOT = 'must_not';
public const CONDITION_AND = 'must';
public const CONDITION_OR = 'should';
// Elasticsearch comparison operators
public const COMPARISON_LESS = 'lt';
public const COMPARISON_LESS_OR_EQUAL = 'lte';
public const COMPARISON_GREATER = 'gt';
public const COMPARISON_GREATER_OR_EQUAL = 'gte';
// note: this one is NOT available for Elasticsearch, we use it as a named constant only
private const COMPARISON_UNEQUAL = '<>';
private const COMPARISON_OPERATOR_MAP = [
'<>' => self::COMPARISON_UNEQUAL,
'<' => self::COMPARISON_LESS,
'<=' => self::COMPARISON_LESS_OR_EQUAL,
'>' => self::COMPARISON_GREATER,
'>=' => self::COMPARISON_GREATER_OR_EQUAL,
];
private $searchCondition;
private $fieldSet;
/** @var FieldMapping[] $mapping */
private $mappings;
/** @var ParameterBag|null */
private $parameterBag;
public function __construct(SearchCondition $searchCondition, ?ParameterBag $parameterBag = null)
{
$this->searchCondition = $searchCondition;
$this->parameterBag = $parameterBag;
$this->fieldSet = $searchCondition->getFieldSet();
}
public function registerField(string $fieldName, string $property, array $conditions = [], array $options = [])
{
$conditionMappings = [];
foreach ($conditions as $condition => $value) {
$conditionMapping = new FieldMapping($fieldName, $this->injectParameters($condition), $this->fieldSet->get($fieldName), [], $options);
$conditionMapping->propertyValue = $this->injectParameters($value);
$conditionMappings[] = $conditionMapping;
}
$this->mappings[$fieldName] = new FieldMapping($fieldName, $this->injectParameters($property), $this->fieldSet->get($fieldName), $conditionMappings, $options);
return $this;
}
public function getQuery(): Query
{
$primaryConditionGroupCondition = [];
$orderCondition = [];
$orderClause = [];
$rootGroupCondition = $this->processGroup($this->searchCondition->getValuesGroup());
$this->processOrder($this->searchCondition->getOrder(), $orderClause, $orderCondition);
if (($primaryCondition = $this->searchCondition->getPrimaryCondition()) !== null) {
$primaryConditionGroupCondition = $this->processGroup($primaryCondition->getValuesGroup(), true);
$this->processOrder($primaryCondition->getOrder(), $orderClause, $orderCondition, true);
}
$rootGroupCondition = array_values(array_filter([
$primaryConditionGroupCondition,
$rootGroupCondition,
$orderCondition,
]));
if (\count($rootGroupCondition) > 1) {
$rootGroupCondition = [self::QUERY_BOOL => [self::CONDITION_AND => $rootGroupCondition]];
} else {
$rootGroupCondition = current($rootGroupCondition);
}
return new Query(array_filter([self::QUERY => $rootGroupCondition, self::SORT => $orderClause]));
}
public function getMappings(): array
{
$mappings = [];
$group = $this->searchCondition->getValuesGroup();
$this->getGroupMappings($group, $mappings);
if (null !== $primaryCondition = $this->searchCondition->getPrimaryCondition()) {
$this->getGroupMappings($primaryCondition->getValuesGroup(), $mappings);
}
if ($mappings === []) {
if (null !== $searchOrder = $this->searchCondition->getOrder()) {
$this->getGroupMappings($searchOrder->getValuesGroup(), $mappings);
}
if ($primaryCondition !== null && null !== $primarySearchOrder = $primaryCondition->getOrder()) {
$this->getGroupMappings($primarySearchOrder->getValuesGroup(), $mappings);
}
}
return array_values($mappings);
}
public function getSearchCondition(): SearchCondition
{
return $this->searchCondition;
}
public static function generateRangeParams(Range $range): array
{
$lowerCondition = $range->isLowerInclusive() ? self::COMPARISON_GREATER_OR_EQUAL : self::COMPARISON_GREATER;
$upperCondition = $range->isUpperInclusive() ? self::COMPARISON_LESS_OR_EQUAL : self::COMPARISON_LESS;
return [
$lowerCondition => $range->getLower(),
$upperCondition => $range->getUpper(),
];
}
/**
* @param string $operator SearchCondition / Compare operator
*
* @return string Equivalent Elasticsearch operator
*/
public static function translateComparison(string $operator): string
{
return self::COMPARISON_OPERATOR_MAP[$operator];
}
private function getGroupMappings(ValuesGroup $group, array &$mappings): void
{
foreach ($group->getFields() as $fieldName => $valuesBag) {
if ($valuesBag->hasSimpleValues()) {
$mappings[$fieldName] = $this->mappings[$fieldName];
}
if ($valuesBag->has(Range::class)) {
$mappings[$fieldName] = $this->mappings[$fieldName];
}
if ($valuesBag->has(Compare::class)) {
$mappings[$fieldName] = $this->mappings[$fieldName];
}
if ($valuesBag->has(PatternMatch::class)) {
$mappings[$fieldName] = $this->mappings[$fieldName];
}
}
foreach ($group->getGroups() as $subGroup) {
$this->getGroupMappings($subGroup, $mappings);
}
}
private function processGroup(ValuesGroup $group, bool $injectParams = false): array
{
// Note: Excludes are `must_not`, for includes `must` (AND) or `should` (OR) is used. Subgroups use `must`.
$includingType = $group->getGroupLogical() === ValuesGroup::GROUP_LOGICAL_AND
? self::CONDITION_AND
: self::CONDITION_OR;
$bool = [];
$hints = new QueryPreparationHints();
foreach ($group->getFields() as $fieldName => $valuesBag) {
if (! isset($this->mappings[$fieldName])) {
throw new UnknownFieldException($fieldName);
}
$mapping = $this->mappings[$fieldName];
$propertyName = $mapping->propertyName;
$valueConverter = $mapping->valueConversion;
$queryConverter = $mapping->queryConversion;
$nested = $mapping->nested;
$join = $mapping->join;
$options = $mapping->options;
$hints->identifier = ($propertyName === self::PROPERTY_ID);
$conditions = $this->processMappingConditions($mapping, $hints, $injectParams);
// simple values
if ($valuesBag->hasSimpleValues()) {
$hints->context = QueryPreparationHints::CONTEXT_SIMPLE_VALUES;
$this->mergeQuery($bool, $includingType, $this->prepareProcessedValuesQuery(
$propertyName,
$valuesBag->getSimpleValues(),
$hints,
$queryConverter,
$valueConverter,
$nested,
$join,
$injectParams,
$conditions,
$options
));
}
if ($valuesBag->hasExcludedSimpleValues()) {
$hints->context = QueryPreparationHints::CONTEXT_EXCLUDED_SIMPLE_VALUES;
$this->mergeQuery($bool, self::CONDITION_NOT, $this->prepareProcessedValuesQuery(
$propertyName,
$valuesBag->getExcludedSimpleValues(),
$hints,
$queryConverter,
$valueConverter,
$nested,
$join,
$injectParams,
$conditions,
$options
));
}
// ranges
if ($valuesBag->has(Range::class)) {
/** @var Range $range */
foreach ($valuesBag->get(Range::class) as $range) {
$hints->context = QueryPreparationHints::CONTEXT_RANGE_VALUES;
$range = $this->convertRangeValues($range, $valueConverter, $injectParams);
$this->mergeQuery($bool, $includingType, $this->prepareQuery($propertyName, $range, $hints, $queryConverter, $nested, $join, $conditions, $options));
}
}
if ($valuesBag->has(ExcludedRange::class)) {
/** @var Range $range */
foreach ($valuesBag->get(ExcludedRange::class) as $range) {
$hints->context = QueryPreparationHints::CONTEXT_EXCLUDED_RANGE_VALUES;
$range = $this->convertRangeValues($range, $valueConverter, $injectParams);
$this->mergeQuery($bool, self::CONDITION_NOT, $this->prepareQuery($propertyName, $range, $hints, $queryConverter, $nested, $join, $conditions, $options));
}
}
// comparison
if ($valuesBag->has(Compare::class)) {
/** @var Compare $compare */
foreach ($valuesBag->get(Compare::class) as $compare) {
$hints->context = QueryPreparationHints::CONTEXT_COMPARISON;
$compare = $this->convertCompareValue($compare, $valueConverter, $injectParams);
$localIncludingType = $compare->getOperator() === self::COMPARISON_UNEQUAL ? self::CONDITION_NOT : $includingType;
$this->mergeQuery($bool, $localIncludingType, $this->prepareQuery($propertyName, $compare, $hints, $queryConverter, $nested, $join, $conditions, $options));
}
}
// matchers
if ($valuesBag->has(PatternMatch::class)) {
/** @var PatternMatch $patternMatch */
foreach ($valuesBag->get(PatternMatch::class) as $patternMatch) {
$patternMatch = $this->convertMatcherValue($patternMatch, $valueConverter, $injectParams);
$hints->context = QueryPreparationHints::CONTEXT_PATTERN_MATCH;
$localIncludingType = $patternMatch->isExclusive() ? self::CONDITION_NOT : $includingType;
$this->mergeQuery($bool, $localIncludingType, $this->prepareQuery($propertyName, $patternMatch, $hints, $queryConverter, $nested, $join, $conditions, $options));
}
}
}
foreach ($group->getGroups() as $subGroup) {
$subGroupCondition = $this->processGroup($subGroup, $injectParams);
if ($subGroupCondition !== []) {
$bool[$includingType][] = $subGroupCondition;
}
}
if ($bool === []) {
return [];
}
return [self::QUERY_BOOL => $bool];
}
private function processOrder(?SearchOrder $order, array &$clause, array &$conditions, bool $injectParams = false): void
{
if ($order === null) {
return;
}
$hints = new QueryPreparationHints();
$hints->context = QueryPreparationHints::CONTEXT_ORDER;
foreach ($order->getFields() as $fieldName => $direction) {
$mapping = $this->mappings[$fieldName];
// apply conditions from order fields
$mappingConditions = $this->processMappingConditions($mapping, $hints, $injectParams);
if ($mappingConditions) {
if (! isset($conditions[self::QUERY_BOOL])) {
$conditions[self::QUERY_BOOL] = [];
}
foreach ($mappingConditions as $mappingCondition) {
$this->mergeQuery($conditions[self::QUERY_BOOL], self::CONDITION_AND, $mappingCondition);
}
}
// sorting by has_child query is special
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-query.html#_sorting
$propertyName = $mapping->join ? self::SORT_SCORE : $mapping->propertyName;
$clause = array_merge_recursive(
$clause,
$mapping->options,
[
$propertyName => [
self::SORT_ORDER => $direction,
],
]
);
}
}
private function convertValue($value, ?ValueConversion $converter, bool $injectParams)
{
$value = $injectParams ? $this->injectParameters($value) : $value;
if ($converter === null) {
return $value;
}
return $converter->convertValue($value);
}
private function convertRangeValues(Range $range, ?ValueConversion $converter, bool $injectParams): Range
{
return new Range(
$this->convertValue($range->getLower(), $converter, $injectParams),
$this->convertValue($range->getUpper(), $converter, $injectParams),
$range->isLowerInclusive(),
$range->isUpperInclusive()
);
}
private function convertCompareValue(Compare $compare, ?ValueConversion $converter, bool $injectParams): Compare
{
return new Compare(
$this->convertValue($compare->getValue(), $converter, $injectParams),
$compare->getOperator()
);
}
private function convertMatcherValue(PatternMatch $patternMatch, ?ValueConversion $converter, bool $injectParams): PatternMatch
{
return new PatternMatch(
$this->convertValue($patternMatch->getValue(), $converter, $injectParams),
$patternMatch->getType(),
$patternMatch->isCaseInsensitive()
);
}
/**
* @param QueryConversion|null $queryConverter
* @param ValueConversion|null $valueConverter
* @param array|bool $nested
* @param array|bool $join
*/
private function prepareProcessedValuesQuery(string $propertyName, array $values, QueryPreparationHints $hints, $queryConverter, $valueConverter, $nested, $join, bool $injectParams, array $conditions = [], array $options = []): array
{
$convertedValues = $values;
if ($hints->context !== QueryPreparationHints::CONTEXT_PRECONDITION_QUERY) {
$convertedValues = [];
foreach ($values as $value) {
$convertedValues[] = $this->convertValue($value, $valueConverter, $injectParams);
}
}
return $this->prepareQuery($propertyName, $convertedValues, $hints, $queryConverter, $nested, $join, $conditions, $options);
}
/**
* @param array|bool $nested
* @param array|bool $join
*/
private function prepareQuery(string $propertyName, $value, QueryPreparationHints $hints, ?QueryConversion $converter, $nested, $join, array $conditions = [], array $options = []): array
{
if ($converter === null || ($query = $converter->convertQuery($propertyName, $value, $hints)) === null) {
switch ($hints->context) {
case QueryPreparationHints::CONTEXT_RANGE_VALUES:
case QueryPreparationHints::CONTEXT_EXCLUDED_RANGE_VALUES:
$query = [self::QUERY_RANGE => [$propertyName => static::generateRangeParams($value)]];
if ($hints->identifier) {
// IDs cannot be queries by range in Elasticsearch, use ids query
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html
/** @var Range $value */
$query = [
self::QUERY_IDS => [
self::QUERY_VALUES => range($value->getLower(), $value->getUpper()),
],
];
}
break;
case QueryPreparationHints::CONTEXT_COMPARISON:
/** @var Compare $value */
$operator = self::translateComparison($value->getOperator());
$query = [
$propertyName => [$operator => $value->getValue()],
];
if ($value->getOperator() === self::COMPARISON_UNEQUAL) {
$query = [
self::QUERY_TERM => [
$propertyName => [self::QUERY_VALUE => $value->getValue()],
],
];
}
break;
case QueryPreparationHints::CONTEXT_PATTERN_MATCH:
/** @var PatternMatch $value */
$query = $this->preparePatternMatch($propertyName, $value);
break;
case QueryPreparationHints::CONTEXT_ORDER:
$query = [
self::QUERY_TERMS => [$propertyName => $value],
];
break;
case QueryPreparationHints::CONTEXT_PRECONDITION_QUERY:
$query = $value;
break;
default:
case QueryPreparationHints::CONTEXT_SIMPLE_VALUES:
case QueryPreparationHints::CONTEXT_EXCLUDED_SIMPLE_VALUES:
// simple values
$query = [self::QUERY_TERMS => [$propertyName => $value]];
if ($hints->identifier) {
$query = [self::QUERY_IDS => [self::QUERY_VALUES => $value]];
}
break;
}
}
if ($nested) {
while ($nested !== false) {
$path = $nested[self::QUERY_PATH];
$query = [
self::QUERY_NESTED => array_replace_recursive(compact('path', 'query'), $options),
];
$nested = $nested['nested'];
}
}
if ($join) {
while ($join !== false) {
$type = $join[self::QUERY_TYPE];
$query = [
self::QUERY_HAS_CHILD => array_replace_recursive(compact('type', 'query'), $options),
];
$join = $join['join'];
}
}
return $this->injectConditions($query, $conditions);
}
private function preparePatternMatch(string $propertyName, PatternMatch $patternMatch): array
{
$query = [];
switch ($patternMatch->getType()) {
// Faster then Wildcard but less accurate.
// XXX Allow to configure `fuzzy`, `operator`, `zero_terms_query` and `cutoff_frequency` (TextType).
case PatternMatch::PATTERN_CONTAINS:
case PatternMatch::PATTERN_NOT_CONTAINS:
$query[self::QUERY_MATCH] = [$propertyName => [self::QUERY => $patternMatch->getValue()]];
break;
case PatternMatch::PATTERN_STARTS_WITH:
case PatternMatch::PATTERN_NOT_STARTS_WITH:
$query[self::QUERY_PREFIX] = [$propertyName => [self::QUERY_VALUE => $patternMatch->getValue()]];
break;
case PatternMatch::PATTERN_ENDS_WITH:
case PatternMatch::PATTERN_NOT_ENDS_WITH:
$query[self::QUERY_WILDCARD] = [
$propertyName => [self::QUERY_VALUE => '?' . addcslashes($patternMatch->getValue(), '?*')],
];
break;
case PatternMatch::PATTERN_EQUALS:
case PatternMatch::PATTERN_NOT_EQUALS:
$query[self::QUERY_TERM] = [$propertyName => [self::QUERY_VALUE => $patternMatch->getValue()]];
break;
default:
$message = \sprintf('Not supported PatternMatch type "%s"', $patternMatch->getType());
throw new BadMethodCallException($message);
}
return $query;
}
private function injectParameters($template)
{
if ($this->parameterBag === null) {
return $template;
}
if (\is_array($template)) {
return array_map([$this->parameterBag, 'injectParameters'], $template);
}
return $this->parameterBag->injectParameters($template);
}
private function injectConditions(array $query, array $conditions): array
{
if ($conditions === []) {
return $query ?? [];
}
$childType = null;
$nestedPath = null;
if (isset($query[self::QUERY_HAS_CHILD])) {
// wrap has_child.query into a bool.must to prepare for accepting conditions
$query[self::QUERY_HAS_CHILD][self::QUERY] = [
self::QUERY_BOOL => [
self::CONDITION_AND => [
$query[self::QUERY_HAS_CHILD][self::QUERY],
],
],
];
$childType = $query[self::QUERY_HAS_CHILD][self::QUERY_TYPE];
$nestedBool = &$query[self::QUERY_HAS_CHILD][self::QUERY][self::QUERY_BOOL];
} elseif (isset($query[self::QUERY_NESTED])) {
// wrap nested.query into a bool.must to prepare for accepting conditions
$query[self::QUERY_NESTED][self::QUERY] = [
self::QUERY_BOOL => [
self::CONDITION_AND => [
$query[self::QUERY_NESTED][self::QUERY],
],
],
];
$nestedPath = $query[self::QUERY_NESTED][self::QUERY_PATH];
$nestedBool = &$query[self::QUERY_NESTED][self::QUERY][self::QUERY_BOOL];
}
$query = [
self::QUERY_BOOL => [
self::CONDITION_AND => [
$query,
],
],
];
$rootBool = &$query[self::QUERY_BOOL];
foreach ($conditions as $condition) {
if (isset($condition[self::QUERY_HAS_CHILD])) {
if ($childType === $condition[self::QUERY_HAS_CHILD][self::QUERY_TYPE]) {
$this->mergeQuery($nestedBool, self::CONDITION_AND, $condition[self::QUERY_HAS_CHILD][self::QUERY]);
} else {
$this->mergeQuery($rootBool, self::CONDITION_AND, $condition);
}
} elseif (isset($condition[self::QUERY_NESTED])) {
if ($nestedPath === $condition[self::QUERY_NESTED][self::QUERY_PATH]) {
$this->mergeQuery($nestedBool, self::CONDITION_AND, $condition[self::QUERY_NESTED][self::QUERY]);
} else {
$this->mergeQuery($rootBool, self::CONDITION_AND, $condition);
}
} else {
$this->mergeQuery($rootBool, self::CONDITION_AND, $condition);
}
}
return $query;
}
private function processMappingConditions(FieldMapping $mapping, QueryPreparationHints $hints, bool $injectParams): array
{
$conditions = [];
if ($mapping->conditions !== []) {
foreach ($mapping->conditions as $mappingCondition) {
if ($mappingCondition->propertyQuery) {
$hints->context = QueryPreparationHints::CONTEXT_PRECONDITION_QUERY;
$values = $mappingCondition->propertyQuery;
} else {
$hints->context = QueryPreparationHints::CONTEXT_PRECONDITION_VALUE;
$values = (array) $mappingCondition->propertyValue;
}
$conditions[] = $this->prepareProcessedValuesQuery(
$mappingCondition->propertyName,
$values,
$hints,
$mappingCondition->queryConversion,
$mappingCondition->valueConversion,
$mappingCondition->nested,
$mappingCondition->join,
$injectParams,
[],
$mappingCondition->options
);
}
}
return $conditions;
}
private function mergeQuery(array &$bool, string $condition, array $query): void
{
if (isset($query[self::QUERY_HAS_CHILD], $bool[$condition])) {
foreach ($bool[$condition] as &$previousQuery) {
if (isset($previousQuery[self::QUERY_HAS_CHILD])
&& $previousQuery[self::QUERY_HAS_CHILD][self::QUERY_TYPE] === $query[self::QUERY_HAS_CHILD][self::QUERY_TYPE]) {
$previousQuery[self::QUERY_HAS_CHILD] = array_replace(
$previousQuery[self::QUERY_HAS_CHILD],
$query[self::QUERY_HAS_CHILD],
[
self::QUERY => $previousQuery[self::QUERY_HAS_CHILD][self::QUERY],
]
);
if (! isset($previousQuery[self::QUERY_HAS_CHILD][self::QUERY][self::QUERY_BOOL])) {
$previousQuery[self::QUERY_HAS_CHILD][self::QUERY] = [
self::QUERY_BOOL => [
$condition => [
$previousQuery[self::QUERY_HAS_CHILD][self::QUERY],
],
],
];
}
$previousQuery[self::QUERY_HAS_CHILD][self::QUERY][self::QUERY_BOOL][$condition][] = $query[self::QUERY_HAS_CHILD][self::QUERY];
return;
}
}
unset($previousQuery);
} elseif (isset($query[self::QUERY_NESTED], $bool[$condition])) {
foreach ($bool[$condition] as &$previousQuery) {
if (isset($previousQuery[self::QUERY_NESTED])
&& $previousQuery[self::QUERY_NESTED][self::QUERY_PATH] === $query[self::QUERY_NESTED][self::QUERY_PATH]) {
$previousQuery[self::QUERY_NESTED] = array_replace(
$previousQuery[self::QUERY_NESTED],
$query[self::QUERY_NESTED],
[
self::QUERY => $previousQuery[self::QUERY_NESTED][self::QUERY],
]
);
if (! isset($previousQuery[self::QUERY_NESTED][self::QUERY][self::QUERY_BOOL])) {
$previousQuery[self::QUERY_NESTED][self::QUERY] = [
self::QUERY_BOOL => [
$condition => [
$previousQuery[self::QUERY_NESTED][self::QUERY],
],
],
];
}
$previousQuery[self::QUERY_NESTED][self::QUERY][self::QUERY_BOOL][$condition][] = $query[self::QUERY_NESTED][self::QUERY];
return;
}
}
unset($previousQuery);
}
$bool[$condition][] = $query;
}
}