Completed
Push — develop ( 43f15c...707223 )
by Ben
04:36 queued 02:20
created

Query   F

Complexity

Total Complexity 99

Size/Duplication

Total Lines 772
Duplicated Lines 3.63 %

Coupling/Cohesion

Components 1
Dependencies 19

Importance

Changes 10
Bugs 2 Features 2
Metric Value
wmc 99
c 10
b 2
f 2
lcom 1
cbo 19
dl 28
loc 772
rs 1.2632

57 Methods

Rating   Name   Duplication   Size   Complexity  
A requestToJson() 0 11 2
A getBridgeJson() 0 5 1
A getBridgeRefinementsJson() 0 7 1
C populateRequest() 0 53 11
B generateSelectedRefinements() 0 36 5
A getBridgeJsonRefinementSearch() 0 15 3
A __construct() 0 4 1
A getQuery() 0 4 1
A setQuery() 0 4 1
A getCollection() 0 4 1
A setCollection() 0 4 1
A getArea() 0 4 1
A setArea() 0 4 1
A getFields() 0 4 1
A getOrFields() 0 4 1
A addFields() 0 4 1
A getIncludedNavigations() 0 4 1
A addIncludedNavigations() 0 4 1
A getExcludedNavigations() 0 4 1
A addExcludedNavigations() 0 4 1
A getNavigations() 0 4 1
A setNavigations() 0 4 1
A addField() 0 4 1
A addOrField() 0 4 1
A addOrFields() 0 4 1
A addCustomUrlParamByName() 0 5 1
A addCustomUrlParam() 0 4 1
A splitRefinements() 0 7 2
D addRefinementsByString() 0 43 10
A addRefinement() 0 14 2
A addRangeRefinement() 0 5 1
A addValueRefinement() 0 5 1
A isPruneRefinements() 0 4 1
A setPruneRefinements() 0 4 1
A getSort() 0 4 1
A setSort() 0 4 1
A getSkip() 0 4 1
A setSkip() 0 4 1
A getCustomUrlParams() 0 4 1
A setCustomUrlParams() 0 4 1
A getLanguage() 0 4 1
A setLanguage() 0 4 1
A getBiasingProfile() 0 4 1
A setBiasingProfile() 0 4 1
A getPageSize() 0 4 1
A setPageSize() 0 4 1
A isDisableAutocorrection() 0 4 1
A setDisableAutocorrection() 0 4 1
A isWildcardSearchEnabled() 0 4 1
A setWildcardSearchEnabled() 0 4 1
A setRestrictNavigation() 0 4 1
A getRestrictNavigation() 0 4 1
B getRefinementString() 15 15 5
A getCustomUrlParamsString() 13 13 4
A convertSort() 0 18 4
A convertPartialMatchStrategy() 0 17 4
A convertPartialMatchRule() 0 13 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Query often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Query, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace GroupByInc\API;
4
5
use GroupByInc\API\Model\CustomUrlParam;
6
use GroupByInc\API\Model\MatchStrategy as MMatchStrategy;
7
use GroupByInc\API\Model\Navigation;
8
use GroupByInc\API\Model\PartialMatchRule as MPartialMatchRule;
9
use GroupByInc\API\Model\Refinement;
10
use GroupByInc\API\Model\Refinement\Type;
11
use GroupByInc\API\Model\RefinementRange;
12
use GroupByInc\API\Model\RefinementValue;
13
use GroupByInc\API\Model\Sort as MSort;
14
use GroupByInc\API\Request\MatchStrategy as RMatchStrategy;
15
use GroupByInc\API\Request\PartialMatchRule as RPartialMatchRule;
16
use GroupByInc\API\Request\RefinementsRequest;
17
use GroupByInc\API\Request\Request;
18
use GroupByInc\API\Request\RestrictNavigation;
19
use GroupByInc\API\Request\SelectedRefinement;
20
use GroupByInc\API\Request\SelectedRefinementRange;
21
use GroupByInc\API\Request\SelectedRefinementValue;
22
use GroupByInc\API\Request\Sort as RSort;
23
use GroupByInc\API\Util\SerializerFactory;
24
use GroupByInc\API\Util\StringBuilder;
25
use GroupByInc\API\Util\StringUtils;
26
use JMS\Serializer\Serializer;
27
use RuntimeException;
28
29
class Symbol
30
{
31
    const TILDE = "~";
32
    const DOT = ".";
33
    const DOUBLE_DOT = "..";
34
    const EQUAL = "=";
35
    const COLON = ":";
36
    const AMPERSAND = "&";
37
    const SLASH = "/";
38
}
39
40
class Query
41
{
42
    /** @var string */
43
    private $query;
44
    /** @var int */
45
    private $skip = 0;
46
    /** @var int */
47
    private $pageSize = 10;
48
    /** @var string */
49
    private $collection;
50
    /** @var string */
51
    private $area;
52
    /** @var string */
53
    private $biasingProfile;
54
    /** @var string */
55
    private $language;
56
    /** @var MSort[] */
57
    private $sort;
58
    /** @var CustomUrlParam[] */
59
    private $customUrlParams = array();
60
    /** @var Navigation[] */
61
    private $navigations = array();
62
    /** @var string[] */
63
    private $includedNavigations = array();
64
    /** @var string[] */
65
    private $excludedNavigations = array();
66
    /** @var string[] */
67
    private $fields = array();
68
    /** @var string[] */
69
    private $orFields = array();
70
    /** @var bool */
71
    private $pruneRefinements = true;
72
    /** @var bool */
73
    private $disableAutocorrection = false;
74
    /** @var bool */
75
    private $wildcardSearchEnabled = false;
76
    // Removed until CBOR support for serialization / de-serialization improves
77
//    /** @var bool */
78
//    private $returnBinary = false;
79
    /** @var RestrictNavigation */
80
    private $restrictNavigation;
81
82
    /** @var Serializer */
83
    private $serializer;
84
85
    const TILDE_REGEX = "/~((?=[\\w]*[=:]))/";
86
87
    /**
88
     * @param mixed $request
89
     *
90
     * @return string
91
     */
92
    private function requestToJson($request)
93
    {
94
        $jsonRequest = null;
95
        try {
96
            $jsonRequest = $this->serializer->serialize($request, 'json');
97
        } catch (RuntimeException $e) {
98
            throw new RuntimeException('Unable to serialize request ' . var_dump($request));
0 ignored issues
show
Security Debugging Code introduced by
var_dump($request); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
99
        }
100
101
        return $jsonRequest;
102
    }
103
104
    /**
105
     * @param string $clientKey Your client key.
106
     *
107
     * @return string JSON representation of request to Bridge.
108
     */
109
    public function getBridgeJson($clientKey)
110
    {
111
        $data = $this->populateRequest($clientKey);
112
        return $this->requestToJson($data);
113
    }
114
115
    /**
116
     * @param string $clientKey      Your client key.
117
     * @param string $navigationName Name of the navigation to get refinements for
118
     *
119
     * @return string JSON representation of request to Bridge.
120
     */
121
    public function getBridgeRefinementsJson($clientKey, $navigationName)
122
    {
123
        $data = new RefinementsRequest();
124
        $data->originalQuery = $this->populateRequest($clientKey);
125
        $data->navigationName = $navigationName;
126
        return $this->requestToJson($data);
127
    }
128
129
    /**
130
     * @param string $clientKey
131
     *
132
     * @return Request
133
     */
134
    private function populateRequest($clientKey)
135
    {
136
        $request = new Request();
137
        $request->clientKey = $clientKey;
138
        $request->area = $this->area;
139
        $request->collection = $this->collection;
140
        $request->query = $this->query;
141
        $request->fields = $this->fields;
142
        $request->orFields = $this->orFields;
143
        $request->language = $this->language;
144
        $request->biasingProfile = $this->biasingProfile;
145
        $request->pageSize = $this->pageSize;
146
        $request->skip = $this->skip;
147
        $request->customUrlParams = $this->customUrlParams;
148
        $request->refinements = $this->generateSelectedRefinements($this->navigations);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->generateSelectedR...nts($this->navigations) of type array<integer,object<Gro...\API\Model\Refinement>> is incompatible with the declared type array<integer,object<Gro...st\SelectedRefinement>> of property $refinements.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
149
        $request->restrictNavigation = $this->restrictNavigation;
150
151
        if (!empty($this->includedNavigations)) {
152
            $request->includedNavigations = $this->includedNavigations;
153
        }
154
155
        if (!empty($this->excludedNavigations)) {
156
            $request->excludedNavigations = $this->excludedNavigations;
157
        }
158
159
        $pruneRefinements = $this->pruneRefinements;
160
        if (isset($pruneRefinements) && $pruneRefinements === false) {
161
            $request->pruneRefinements = false;
162
        }
163
164
        $disableAutocorrection = $this->disableAutocorrection;
165
        if (isset($disableAutocorrection) && $disableAutocorrection === true) {
166
            $request->disableAutocorrection = true;
167
        }
168
169
        $wildcardSearchEnabled = $this->wildcardSearchEnabled;
170
        if (isset($wildcardSearchEnabled) && $wildcardSearchEnabled === true) {
171
            $request->wildcardSearchEnabled = true;
172
        }
173
174
        if (!empty($this->sort)) {
175
            foreach ($this->sort as $s) {
176
                array_push($request->sort, $this->convertSort($s));
177
            }
178
        }
179
180
//        $returnBinary = $this->returnBinary;
181
//        if (isset($returnBinary) && $returnBinary === true) {
182
//            $request->returnBinary = true;
183
//        }
184
185
        return $request;
186
    }
187
188
    /**
189
     * @param Navigation[] $navigations
190
     *
191
     * @return Refinement[]
192
     */
193
    private function generateSelectedRefinements($navigations)
194
    {
195
        $refinements = [];
196
        foreach ($navigations as $key => $navigation) {
197
            foreach ($navigation->getRefinements() as $refinement) {
198
                switch ($refinement->getType()) {
199
                    case Type::Range: {
200
                        /** @var RefinementRange $rr */
201
                        $rr = $refinement;
202
                        $selectedRefinementRange = new SelectedRefinementRange();
203
                        $selectedRefinementRange
204
                            ->setNavigationName($navigation->getName())
205
                            ->setLow($rr->getLow())
206
                            ->setHigh($rr->getHigh())
207
                            ->setExclude($rr->isExclude());
208
209
                        array_push($refinements, $selectedRefinementRange);
210
                        break;
211
                    }
212
                    case Type::Value: {
213
                        /** @var RefinementValue $rv */
214
                        $rv = $refinement;
215
                        $selectedRefinementValue = new SelectedRefinementValue();
216
                        $selectedRefinementValue
217
                            ->setNavigationName($navigation->getName())
218
                            ->setValue($rv->getValue())
219
                            ->setExclude($rv->isExclude());
220
221
                        array_push($refinements, $selectedRefinementValue);
222
                        break;
223
                    }
224
                }
225
            }
226
        }
227
        return $refinements;
228
    }
229
230
    /**
231
     * @param string $clientKey Your client key.
232
     *
233
     * @return string JSON representation of request to Bridge.
234
     */
235
    public function getBridgeJsonRefinementSearch($clientKey)
236
    {
237
        $data = new Request();
238
        $data->clientKey = $clientKey;
239
        $data->collection = $this->collection;
240
        $data->area = $this->area;
241
        $data->refinementQuery = $this->query;
242
243
        $wildcardSearchEnabled = $this->wildcardSearchEnabled;
244
        if (isset($wildcardSearchEnabled) && $wildcardSearchEnabled === true) {
245
            $data->wildcardSearchEnabled = true;
246
        }
247
248
        return $this->requestToJson($data);
249
    }
250
251
    public function __construct()
252
    {
253
        $this->serializer = SerializerFactory::build();
254
    }
255
256
    /**
257
     * @return string The current search string.
258
     */
259
    public function getQuery()
260
    {
261
        return $this->query;
262
    }
263
264
    /**
265
     * @param string $query The search string.
266
     */
267
    public function setQuery($query)
268
    {
269
        $this->query = $query;
270
    }
271
272
    /**
273
     * @return string The data sub-collection.
274
     */
275
    public function getCollection()
276
    {
277
        return $this->collection;
278
    }
279
280
    /**
281
     * @param string $collection The string representation of a collection query.
282
     */
283
    public function setCollection($collection)
284
    {
285
        $this->collection = $collection;
286
    }
287
288
    /**
289
     * @return string The area name.
290
     */
291
    public function getArea()
292
    {
293
        return $this->area;
294
    }
295
296
    /**
297
     * @param string $area The area name.
298
     */
299
    public function setArea($area)
300
    {
301
        $this->area = $area;
302
    }
303
304
    /**
305
     * @return string[] A list of metadata fields that will be returned by the search engine.
306
     */
307
    public function getFields()
308
    {
309
        return $this->fields;
310
    }
311
312
    /**
313
     * @return string[] A list of the fields that the search service will treat as OR'able.
314
     */
315
    public function getOrFields()
316
    {
317
        return $this->orFields;
318
    }
319
320
    /**
321
     * @param string[] $fields A list of case-sensitive names of the attributes to return.
322
     */
323
    public function addFields($fields)
324
    {
325
        $this->fields = array_merge($this->fields, $fields);
326
    }
327
328
    /**
329
     * @return string[] A list of which navigations to return from the bridge.
330
     */
331
    public function getIncludedNavigations()
332
    {
333
        return $this->includedNavigations;
334
    }
335
336
    /**
337
     * @param string[] $navigations A list of which navigations to return from the bridge.
338
     */
339
    public function addIncludedNavigations($navigations)
340
    {
341
        $this->includedNavigations = array_merge($this->includedNavigations, $navigations);
342
    }
343
344
    /**
345
     * @return string[] A list of which navigations to not return from the bridge.
346
     */
347
    public function getExcludedNavigations()
348
    {
349
        return $this->excludedNavigations;
350
    }
351
352
    /**
353
     * @param string[] $navigations A list of which navigations to not return from the bridge.
354
     */
355
    public function addExcludedNavigations($navigations)
356
    {
357
        $this->excludedNavigations = array_merge($this->excludedNavigations, $navigations);
358
    }
359
360
    /**
361
     * @return Navigation[]
362
     */
363
    public function &getNavigations()
364
    {
365
        return $this->navigations;
366
    }
367
368
    /**
369
     * @param Navigation[] $navigations
370
     */
371
    public function setNavigations($navigations)
372
    {
373
        $this->navigations = $navigations;
374
    }
375
376
    /**
377
     * @param string $name The case-sensitive name of the attribute to return.
378
     */
379
    public function addField($name)
380
    {
381
        array_push($this->fields, $name);
382
    }
383
384
    /**
385
     * @param string $name Field that should be treated as OR.
386
     */
387
    public function addOrField($name)
388
    {
389
        array_push($this->orFields, $name);
390
    }
391
392
    /**
393
     * @param string[] $fields A list of fields that should be treated as OR.
394
     */
395
    public function addOrFields($fields)
396
    {
397
        $this->orFields = array_merge($this->orFields, $fields);
398
    }
399
400
    /**
401
     * @param string $name  The parameter name.
402
     * @param string $value The parameter value.
403
     */
404
    public function addCustomUrlParamByName($name, $value)
405
    {
406
        $param = new CustomUrlParam();
407
        $this->addCustomUrlParam($param->setKey($name)->setValue($value));
408
    }
409
410
    /**
411
     * @param CustomUrlParam $param Set an additional parameter that can be used to trigger rules.
412
     */
413
    public function addCustomUrlParam($param)
414
    {
415
        array_push($this->customUrlParams, $param);
416
    }
417
418
    public function splitRefinements($refinementString)
419
    {
420
        if (StringUtils::isNotBlank($refinementString)) {
421
            return preg_split(self::TILDE_REGEX, $refinementString, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
422
        }
423
        return [];
424
    }
425
426
    /**
427
     * @param string $refinementString A tilde separated list of refinements.
428
     */
429
    public function addRefinementsByString($refinementString)
430
    {
431
        if ($refinementString == null) {
432
            return;
433
        }
434
435
        $refinementStrings = self::splitRefinements($refinementString);
436
        foreach ($refinementStrings as $refinementString) {
437
            if (empty($refinementString) || "=" == $refinementString) {
438
                continue;
439
            }
440
            $colon = strpos($refinementString, Symbol::COLON);
441
            $equals = strpos($refinementString, Symbol::EQUAL);
442
            //when === false, it means it did not find the substring in the string
443
            $isRange = !($colon === false) && ($equals === false);
444
445
            $refinement = null;
0 ignored issues
show
Unused Code introduced by
$refinement is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
446
            if ($isRange) {
447
                $nameValue = explode(Symbol::COLON, $refinementString, 2);
448
                $refinement = new RefinementRange();
449
                if (StringUtils::endsWith($nameValue[1], Symbol::DOUBLE_DOT)) {
450
                    $value = explode(Symbol::DOUBLE_DOT, $nameValue[1]);
451
                    $refinement->setLow($value[0]);
452
                    $refinement->setHigh("");
453
                } else if (StringUtils::startsWith($nameValue[1], Symbol::DOUBLE_DOT)) {
454
                    $refinement->setLow("");
455
                    $value = explode(Symbol::DOUBLE_DOT, $nameValue[1]);
456
                    $refinement->setHigh($value[1]);
457
                } else {
458
                    $lowHigh = explode(Symbol::DOUBLE_DOT, $nameValue[1]);
459
                    $refinement->setLow($lowHigh[0]);
460
                    $refinement->setHigh($lowHigh[1]);
461
                }
462
            } else {
463
                $nameValue = explode(Symbol::EQUAL, $refinementString, 2);
464
                $refinement = new RefinementValue();
465
                $refinement->setValue($nameValue[1]);
466
            }
467
            if (!empty($nameValue[0])) {
468
                $this->addRefinement($nameValue[0], $refinement);
469
            }
470
        }
471
    }
472
473
    /**
474
     * @param string     $navigationName The name of the Navigation.
475
     * @param Refinement $refinement     A RefinementRange or RefinementValue object.
476
     */
477
    public function addRefinement($navigationName, $refinement)
478
    {
479
        $navigation = null;
0 ignored issues
show
Unused Code introduced by
$navigation is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
480
        if (array_key_exists($navigationName, $this->navigations)) {
481
            $navigation = $this->navigations[$navigationName];
482
        } else {
483
            $navigation = new Navigation();
484
            $navigation->setName($navigationName)->setRange($refinement instanceof SelectedRefinementRange);
485
            $this->navigations[$navigationName] = $navigation;
486
        }
487
        $refinements = $navigation->getRefinements();
488
        array_push($refinements, $refinement);
489
        $navigation->setRefinements($refinements);
490
    }
491
492
    /**
493
     * @param string $navigationName The name of the refinement.
494
     * @param mixed  $low            The low value.
495
     * @param mixed  $high           The high value.
496
     * @param bool   $exclude        True if the results should exclude this range refinement, false otherwise.
497
     */
498
    public function addRangeRefinement($navigationName, $low, $high, $exclude = false)
499
    {
500
        $refinement = new RefinementRange();
501
        $this->addRefinement($navigationName, $refinement->setLow($low)->setHigh($high)->setExclude($exclude));
502
    }
503
504
    /**
505
     * @param string $navigationName The name of the refinement.
506
     * @param mixed  $value          The refinement value.
507
     * @param bool   $exclude        True if the results should exclude this value refinement, false otherwise.
508
     */
509
    public function addValueRefinement($navigationName, $value, $exclude = false)
510
    {
511
        $refinement = new RefinementValue();;
512
        $this->addRefinement($navigationName, $refinement->setValue($value)->setExclude($exclude));
513
    }
514
515
    /**
516
     * @return bool Are refinements with zero counts being removed.
517
     */
518
    public function isPruneRefinements()
519
    {
520
        return $this->pruneRefinements;
521
    }
522
523
    /**
524
     * @param bool $pruneRefinements Specifies whether refinements should be pruned.
525
     */
526
    public function setPruneRefinements($pruneRefinements)
527
    {
528
        $this->pruneRefinements = $pruneRefinements;
529
    }
530
531
    /**
532
     * @return MSort[] The current list of sort parameters.
533
     */
534
    public function &getSort()
535
    {
536
        return $this->sort;
537
    }
538
539
    /**
540
     * @param MSort[] $sort Any number of sort criteria.
541
     */
542
    public function setSort($sort)
543
    {
544
        $this->sort = $sort;
545
    }
546
547
    /**
548
     * @return int The number of documents to skip.
549
     */
550
    public function getSkip()
551
    {
552
        return $this->skip;
553
    }
554
555
    /**
556
     * @param int $skip The number of documents to skip.
557
     */
558
    public function setSkip($skip)
559
    {
560
        $this->skip = $skip;
561
    }
562
563
    /**
564
     * @return CustomUrlParam[] A list of custom url params.
565
     */
566
    public function getCustomUrlParams()
567
    {
568
        return $this->customUrlParams;
569
    }
570
571
    /**
572
     * @param CustomUrlParam[] $customUrlParams Set the custom url params.
573
     */
574
    public function setCustomUrlParams($customUrlParams)
575
    {
576
        $this->customUrlParams = $customUrlParams;
577
    }
578
579
//    /**
580
//     * @return bool Is return JSON set to true.
581
//     */
582
//    public function isReturnBinary()
583
//    {
584
//        return $this->returnBinary;
585
//    }
586
//
587
//    /**
588
//     * @param bool $returnBinary Whether to tell the bridge to return binary data rather than JSON.
589
//     */
590
//    public function setReturnBinary($returnBinary)
591
//    {
592
//        $this->returnBinary = $returnBinary;
593
//    }
594
595
    /**
596
     * @return string The current language restrict value.
597
     */
598
    public function getLanguage()
599
    {
600
        return $this->language;
601
    }
602
603
    /**
604
     * @param string $language The value for language restrict.
605
     */
606
    public function setLanguage($language)
607
    {
608
        $this->language = $language;
609
    }
610
611
    /**
612
     * @return string The current biasing profile name.
613
     */
614
    public function getBiasingProfile()
615
    {
616
        return $this->biasingProfile;
617
    }
618
619
    /**
620
     * @param string $biasingProfile Override the biasing profile used for this query.
621
     */
622
    public function setBiasingProfile($biasingProfile)
623
    {
624
        $this->biasingProfile = $biasingProfile;
625
    }
626
627
    /**
628
     * @return int The current page size.
629
     */
630
    public function getPageSize()
631
    {
632
        return $this->pageSize;
633
    }
634
635
    /**
636
     * @param int $pageSize The number of records to return with the query.
637
     */
638
    public function setPageSize($pageSize)
639
    {
640
        $this->pageSize = $pageSize;
641
    }
642
643
    /**
644
     * @return boolean
645
     */
646
    public function isDisableAutocorrection()
647
    {
648
        return $this->disableAutocorrection;
649
    }
650
651
    /**
652
     * @param boolean $disableAutocorrection Specifies whether the auto-correction behavior should be disabled.
653
     *                                       By default, when no results are returned for the given query (and there is
654
     *                                       a did-you-mean available), the first did-you-mean is automatically queried
655
     *                                       instead.
656
     */
657
    public function setDisableAutocorrection($disableAutocorrection)
658
    {
659
        $this->disableAutocorrection = $disableAutocorrection;
660
    }
661
662
    /**
663
     * @return boolean
664
     */
665
    public function isWildcardSearchEnabled()
666
    {
667
        return $this->wildcardSearchEnabled;
668
    }
669
670
    /**
671
     * @param boolean $wildcardSearchEnabled Indicate if the *(star) character in the search string should be treated
672
     *                                       as a wildcard prefix search. For example, `sta*` will match `star` and
673
     *                                       `start`.
674
     */
675
    public function setWildcardSearchEnabled($wildcardSearchEnabled)
676
    {
677
        $this->wildcardSearchEnabled = $wildcardSearchEnabled;
678
    }
679
680
    /**
681
     * <b>Warning</b>  This will count as two queries against your search index.
682
     *
683
     * Typically, this feature is used when you have a large number of navigation items that will overwhelm the end
684
     * user. It works by using one of the existing navigation items to decide what the query is about and fires a second
685
     * query to restrict the navigation to the most relevant set of navigation items for this search term.
686
     *
687
     * For example, if you pass in a search of `paper` and a restrict navigation of `category:2`
688
     *
689
     * The bridge will find the category navigation refinements in the first query and fire a second query for the top 2
690
     * most populous categories.  Therefore, a search for something generic like "paper" will bring back top category
691
     * matches like copy paper (1,030), paper pads (567).  The bridge will fire off the second query with the search
692
     * term, plus an OR refinement with the most likely categories.  The navigation items in the first query are
693
     * entirely replaced with the navigation items in the second query, except for the navigation that was used for the
694
     * restriction so that users still have the ability to navigate by all category types.
695
     *
696
     * @param RestrictNavigation $restrictNavigation Restriction criteria
697
     */
698
    public function setRestrictNavigation($restrictNavigation)
699
    {
700
        $this->restrictNavigation = $restrictNavigation;
701
    }
702
703
    /** @return RestrictNavigation */
704
    public function getRestrictNavigation()
705
    {
706
        return $this->restrictNavigation;
707
    }
708
709
    /**
710
     * @return string A string representation of all of the currently set refinements.
711
     */
712 View Code Duplication
    public function getRefinementString()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
713
    {
714
        if (!empty($this->navigations)) {
715
            $builder = new StringBuilder();
716
            foreach ($this->navigations as $n) {
717
                foreach ($n->getRefinements() as $r) {
718
                    $builder->append(Symbol::TILDE)->append($n->getName())->append($r->toTildeString());
719
                }
720
            }
721
            if ($builder->length() > 0) {
722
                return $builder->__toString();
723
            }
724
        }
725
        return null;
726
    }
727
728
    /**
729
     * @return string A string representation of all of the currently set custom url parameters.
730
     */
731 View Code Duplication
    public function getCustomUrlParamsString()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
732
    {
733
        if (!empty($this->customUrlParams)) {
734
            $builder = new StringBuilder();
735
            foreach ($this->customUrlParams as $c) {
736
                $builder->append(Symbol::TILDE)->append($c->getKey())->append(Symbol::EQUAL)->append($c->getValue());
737
            }
738
            if ($builder->length() > 0) {
739
                return $builder->__toString();
740
            }
741
        }
742
        return null;
743
    }
744
745
    /**
746
     * @param MSort $sort
747
     *
748
     * @return RSort
749
     */
750
    protected static function convertSort($sort)
751
    {
752
        /** @var RSort $convertedSort */
753
        $convertedSort = null;
754
        if (!empty($sort)) {
755
            $convertedSort = new RSort();
756
            $convertedSort->setField($sort->getField());
757
            switch ($sort->getOrder()) {
758
                case MSort\Order::Ascending:
759
                    $convertedSort->setOrder(RSort\Order::Ascending);
760
                    break;
761
                case MSort\Order::Descending:
762
                    $convertedSort->setOrder(RSort\Order::Descending);
763
                    break;
764
            }
765
        }
766
        return $convertedSort;
767
    }
768
769
    /**
770
     * @param MMatchStrategy $strategy
771
     *
772
     * @return RMatchStrategy
773
     */
774
    protected static function convertPartialMatchStrategy($strategy)
775
    {
776
        /** @var RMatchStrategy $convertedStrategy */
777
        $convertedStrategy = null;
778
        if (!empty($strategy)) {
779
            $rules = $strategy->getRules();
780
            if (!empty($rules)) {
781
                $convertedStrategy = new RMatchStrategy();
782
                /** @var MPartialMatchRule $r */
783
                foreach ($rules as $r) {
784
                    array_push($rules, Query::convertPartialMatchRule($r));
785
                }
786
                $strategy->setRules($rules);
787
            }
788
        }
789
        return $convertedStrategy;
790
    }
791
792
    /**
793
     * @param MPartialMatchRule $rule
794
     *
795
     * @return RPartialMatchRule
796
     */
797
    protected static function convertPartialMatchRule($rule)
798
    {
799
        /** @var RPartialMatchRule $convertedRule */
800
        $convertedRule = null;
801
        if (!empty($rule)) {
802
            $convertedRule = new RPartialMatchRule();
803
            $convertedRule->setTerms($rule->getTerms())
804
                ->setTermsGreaterThan($rule->getTermsGreaterThan())
805
                ->setMustMatch($rule->getMustMatch())
806
                ->setPercentage($rule->isPercentage());
807
        }
808
        return $convertedRule;
809
    }
810
811
}
812