Completed
Push — develop ( 707223...1b2298 )
by Ben
01:50
created

Query::addIncludedNavigations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace GroupByInc\API;
4
5
use GroupByInc\API\Model\Biasing as MBiasing;
6
use GroupByInc\API\Model\CustomUrlParam;
7
use GroupByInc\API\Model\MatchStrategy as MMatchStrategy;
8
use GroupByInc\API\Model\Navigation;
9
use GroupByInc\API\Model\PartialMatchRule as MPartialMatchRule;
10
use GroupByInc\API\Model\Refinement;
11
use GroupByInc\API\Model\Refinement\Type;
12
use GroupByInc\API\Model\RefinementRange;
13
use GroupByInc\API\Model\RefinementValue;
14
use GroupByInc\API\Model\Sort as MSort;
15
use GroupByInc\API\Request\Biasing;
16
use GroupByInc\API\Request\MatchStrategy as RMatchStrategy;
17
use GroupByInc\API\Request\PartialMatchRule as RPartialMatchRule;
18
use GroupByInc\API\Request\RefinementsRequest;
19
use GroupByInc\API\Request\Request;
20
use GroupByInc\API\Request\RestrictNavigation;
21
use GroupByInc\API\Request\SelectedRefinement;
22
use GroupByInc\API\Request\SelectedRefinementRange;
23
use GroupByInc\API\Request\SelectedRefinementValue;
24
use GroupByInc\API\Request\Sort as RSort;
25
use GroupByInc\API\Util\SerializerFactory;
26
use GroupByInc\API\Util\StringBuilder;
27
use GroupByInc\API\Util\StringUtils;
28
use JMS\Serializer\Serializer;
29
use RuntimeException;
30
31
class Symbol
32
{
33
    const TILDE = "~";
34
    const DOT = ".";
35
    const DOUBLE_DOT = "..";
36
    const EQUAL = "=";
37
    const COLON = ":";
38
    const AMPERSAND = "&";
39
    const SLASH = "/";
40
}
41
42
class Query
43
{
44
    /** @var string */
45
    private $query;
46
    /** @var int */
47
    private $skip = 0;
48
    /** @var int */
49
    private $pageSize = 10;
50
    /** @var string */
51
    private $collection;
52
    /** @var string */
53
    private $area;
54
    /** @var string */
55
    private $biasingProfile;
56
    /** @var string */
57
    private $language;
58
    /** @var MSort[] */
59
    private $sort;
60
    /** @var CustomUrlParam[] */
61
    private $customUrlParams = array();
62
    /** @var Navigation[] */
63
    private $navigations = array();
64
    /** @var string[] */
65
    private $includedNavigations = array();
66
    /** @var string[] */
67
    private $excludedNavigations = array();
68
    /** @var string[] */
69
    private $fields = array();
70
    /** @var string[] */
71
    private $orFields = array();
72
    /** @var bool */
73
    private $pruneRefinements = true;
74
    /** @var bool */
75
    private $disableAutocorrection = false;
76
    /** @var bool */
77
    private $wildcardSearchEnabled = false;
78
    // Removed until CBOR support for serialization / de-serialization improves
79
//    /** @var bool */
80
//    private $returnBinary = false;
81
    /** @var RestrictNavigation */
82
    private $restrictNavigation;
83
    /** @var MBiasing */
84
    private $biasing;
85
86
    /** @var Serializer */
87
    private $serializer;
88
89
    const TILDE_REGEX = "/~((?=[\\w]*[=:]))/";
90
91
    /**
92
     * @param mixed $request
93
     *
94
     * @return string
95
     */
96
    private function requestToJson($request)
97
    {
98
        $jsonRequest = null;
99
        try {
100
            $jsonRequest = $this->serializer->serialize($request, 'json');
101
        } catch (RuntimeException $e) {
102
            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...
103
        }
104
105
        return $jsonRequest;
106
    }
107
108
    /**
109
     * @param string $clientKey Your client key.
110
     *
111
     * @return string JSON representation of request to Bridge.
112
     */
113
    public function getBridgeJson($clientKey)
114
    {
115
        $data = $this->populateRequest($clientKey);
116
        return $this->requestToJson($data);
117
    }
118
119
    /**
120
     * @param string $clientKey      Your client key.
121
     * @param string $navigationName Name of the navigation to get refinements for
122
     *
123
     * @return string JSON representation of request to Bridge.
124
     */
125
    public function getBridgeRefinementsJson($clientKey, $navigationName)
126
    {
127
        $data = new RefinementsRequest();
128
        $data->originalQuery = $this->populateRequest($clientKey);
129
        $data->navigationName = $navigationName;
130
        return $this->requestToJson($data);
131
    }
132
133
    /**
134
     * @param string $clientKey
135
     *
136
     * @return Request
137
     */
138
    private function populateRequest($clientKey)
139
    {
140
        $request = new Request();
141
        $request->clientKey = $clientKey;
142
        $request->area = $this->area;
143
        $request->collection = $this->collection;
144
        $request->query = $this->query;
145
        $request->fields = $this->fields;
146
        $request->orFields = $this->orFields;
147
        $request->language = $this->language;
148
        $request->biasingProfile = $this->biasingProfile;
149
        $request->pageSize = $this->pageSize;
150
        $request->skip = $this->skip;
151
        $request->customUrlParams = $this->customUrlParams;
152
        $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...
153
        $request->restrictNavigation = $this->restrictNavigation;
154
155
        if (!empty($this->biasing)) {
156
            $request->biasing = self::convertBiasing($this->biasing);
157
        }
158
159
        if (!empty($this->includedNavigations)) {
160
            $request->includedNavigations = $this->includedNavigations;
161
        }
162
163
        if (!empty($this->excludedNavigations)) {
164
            $request->excludedNavigations = $this->excludedNavigations;
165
        }
166
167
        $pruneRefinements = $this->pruneRefinements;
168
        if (isset($pruneRefinements) && $pruneRefinements === false) {
169
            $request->pruneRefinements = false;
170
        }
171
172
        $disableAutocorrection = $this->disableAutocorrection;
173
        if (isset($disableAutocorrection) && $disableAutocorrection === true) {
174
            $request->disableAutocorrection = true;
175
        }
176
177
        $wildcardSearchEnabled = $this->wildcardSearchEnabled;
178
        if (isset($wildcardSearchEnabled) && $wildcardSearchEnabled === true) {
179
            $request->wildcardSearchEnabled = true;
180
        }
181
182
        if (!empty($this->sort)) {
183
            foreach ($this->sort as $s) {
184
                array_push($request->sort, $this->convertSort($s));
185
            }
186
        }
187
188
//        $returnBinary = $this->returnBinary;
189
//        if (isset($returnBinary) && $returnBinary === true) {
190
//            $request->returnBinary = true;
191
//        }
192
193
        return $request;
194
    }
195
196
    /**
197
     * @param Navigation[] $navigations
198
     *
199
     * @return Refinement[]
200
     */
201
    private function generateSelectedRefinements($navigations)
202
    {
203
        $refinements = [];
204
        foreach ($navigations as $key => $navigation) {
205
            foreach ($navigation->getRefinements() as $refinement) {
206
                switch ($refinement->getType()) {
207
                    case Type::Range: {
208
                        /** @var RefinementRange $rr */
209
                        $rr = $refinement;
210
                        $selectedRefinementRange = new SelectedRefinementRange();
211
                        $selectedRefinementRange
212
                            ->setNavigationName($navigation->getName())
213
                            ->setLow($rr->getLow())
214
                            ->setHigh($rr->getHigh())
215
                            ->setExclude($rr->isExclude());
216
217
                        array_push($refinements, $selectedRefinementRange);
218
                        break;
219
                    }
220
                    case Type::Value: {
221
                        /** @var RefinementValue $rv */
222
                        $rv = $refinement;
223
                        $selectedRefinementValue = new SelectedRefinementValue();
224
                        $selectedRefinementValue
225
                            ->setNavigationName($navigation->getName())
226
                            ->setValue($rv->getValue())
227
                            ->setExclude($rv->isExclude());
228
229
                        array_push($refinements, $selectedRefinementValue);
230
                        break;
231
                    }
232
                }
233
            }
234
        }
235
        return $refinements;
236
    }
237
238
    /**
239
     * @param string $clientKey Your client key.
240
     *
241
     * @return string JSON representation of request to Bridge.
242
     */
243
    public function getBridgeJsonRefinementSearch($clientKey)
244
    {
245
        $data = new Request();
246
        $data->clientKey = $clientKey;
247
        $data->collection = $this->collection;
248
        $data->area = $this->area;
249
        $data->refinementQuery = $this->query;
250
251
        $wildcardSearchEnabled = $this->wildcardSearchEnabled;
252
        if (isset($wildcardSearchEnabled) && $wildcardSearchEnabled === true) {
253
            $data->wildcardSearchEnabled = true;
254
        }
255
256
        return $this->requestToJson($data);
257
    }
258
259
    public function __construct()
260
    {
261
        $this->serializer = SerializerFactory::build();
262
    }
263
264
    /**
265
     * @return string The current search string.
266
     */
267
    public function getQuery()
268
    {
269
        return $this->query;
270
    }
271
272
    /**
273
     * @param string $query The search string.
274
     */
275
    public function setQuery($query)
276
    {
277
        $this->query = $query;
278
    }
279
280
    /**
281
     * @return string The data sub-collection.
282
     */
283
    public function getCollection()
284
    {
285
        return $this->collection;
286
    }
287
288
    /**
289
     * @param string $collection The string representation of a collection query.
290
     */
291
    public function setCollection($collection)
292
    {
293
        $this->collection = $collection;
294
    }
295
296
    /**
297
     * @return string The area name.
298
     */
299
    public function getArea()
300
    {
301
        return $this->area;
302
    }
303
304
    /**
305
     * @param string $area The area name.
306
     */
307
    public function setArea($area)
308
    {
309
        $this->area = $area;
310
    }
311
312
    /**
313
     * @return string[] A list of metadata fields that will be returned by the search engine.
314
     */
315
    public function getFields()
316
    {
317
        return $this->fields;
318
    }
319
320
    /**
321
     * @return string[] A list of the fields that the search service will treat as OR'able.
322
     */
323
    public function getOrFields()
324
    {
325
        return $this->orFields;
326
    }
327
328
    /**
329
     * @param string[] $fields A list of case-sensitive names of the attributes to return.
330
     */
331
    public function addFields($fields)
332
    {
333
        $this->fields = array_merge($this->fields, $fields);
334
    }
335
336
    /**
337
     * @return string[] A list of which navigations to return from the bridge.
338
     */
339
    public function getIncludedNavigations()
340
    {
341
        return $this->includedNavigations;
342
    }
343
344
    /**
345
     * @param string[] $navigations A list of which navigations to return from the bridge.
346
     */
347
    public function addIncludedNavigations($navigations)
348
    {
349
        $this->includedNavigations = array_merge($this->includedNavigations, $navigations);
350
    }
351
352
    /**
353
     * @return string[] A list of which navigations to not return from the bridge.
354
     */
355
    public function getExcludedNavigations()
356
    {
357
        return $this->excludedNavigations;
358
    }
359
360
    /**
361
     * @param string[] $navigations A list of which navigations to not return from the bridge.
362
     */
363
    public function addExcludedNavigations($navigations)
364
    {
365
        $this->excludedNavigations = array_merge($this->excludedNavigations, $navigations);
366
    }
367
368
    /**
369
     * @return Navigation[]
370
     */
371
    public function &getNavigations()
372
    {
373
        return $this->navigations;
374
    }
375
376
    /**
377
     * @param Navigation[] $navigations
378
     */
379
    public function setNavigations($navigations)
380
    {
381
        $this->navigations = $navigations;
382
    }
383
384
    /**
385
     * @param string $name The case-sensitive name of the attribute to return.
386
     */
387
    public function addField($name)
388
    {
389
        array_push($this->fields, $name);
390
    }
391
392
    /**
393
     * @param string $name Field that should be treated as OR.
394
     */
395
    public function addOrField($name)
396
    {
397
        array_push($this->orFields, $name);
398
    }
399
400
    /**
401
     * @param string[] $fields A list of fields that should be treated as OR.
402
     */
403
    public function addOrFields($fields)
404
    {
405
        $this->orFields = array_merge($this->orFields, $fields);
406
    }
407
408
    /**
409
     * @param string $name  The parameter name.
410
     * @param string $value The parameter value.
411
     */
412
    public function addCustomUrlParamByName($name, $value)
413
    {
414
        $param = new CustomUrlParam();
415
        $this->addCustomUrlParam($param->setKey($name)->setValue($value));
416
    }
417
418
    /**
419
     * @param CustomUrlParam $param Set an additional parameter that can be used to trigger rules.
420
     */
421
    public function addCustomUrlParam($param)
422
    {
423
        array_push($this->customUrlParams, $param);
424
    }
425
426
    public function splitRefinements($refinementString)
427
    {
428
        if (StringUtils::isNotBlank($refinementString)) {
429
            return preg_split(self::TILDE_REGEX, $refinementString, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
430
        }
431
        return [];
432
    }
433
434
    /**
435
     * @param string $refinementString A tilde separated list of refinements.
436
     */
437
    public function addRefinementsByString($refinementString)
438
    {
439
        if ($refinementString == null) {
440
            return;
441
        }
442
443
        $refinementStrings = self::splitRefinements($refinementString);
444
        foreach ($refinementStrings as $refinementString) {
445
            if (empty($refinementString) || "=" == $refinementString) {
446
                continue;
447
            }
448
            $colon = strpos($refinementString, Symbol::COLON);
449
            $equals = strpos($refinementString, Symbol::EQUAL);
450
            //when === false, it means it did not find the substring in the string
451
            $isRange = !($colon === false) && ($equals === false);
452
453
            $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...
454
            if ($isRange) {
455
                $nameValue = explode(Symbol::COLON, $refinementString, 2);
456
                $refinement = new RefinementRange();
457
                if (StringUtils::endsWith($nameValue[1], Symbol::DOUBLE_DOT)) {
458
                    $value = explode(Symbol::DOUBLE_DOT, $nameValue[1]);
459
                    $refinement->setLow($value[0]);
460
                    $refinement->setHigh("");
461
                } else if (StringUtils::startsWith($nameValue[1], Symbol::DOUBLE_DOT)) {
462
                    $refinement->setLow("");
463
                    $value = explode(Symbol::DOUBLE_DOT, $nameValue[1]);
464
                    $refinement->setHigh($value[1]);
465
                } else {
466
                    $lowHigh = explode(Symbol::DOUBLE_DOT, $nameValue[1]);
467
                    $refinement->setLow($lowHigh[0]);
468
                    $refinement->setHigh($lowHigh[1]);
469
                }
470
            } else {
471
                $nameValue = explode(Symbol::EQUAL, $refinementString, 2);
472
                $refinement = new RefinementValue();
473
                $refinement->setValue($nameValue[1]);
474
            }
475
            if (!empty($nameValue[0])) {
476
                $this->addRefinement($nameValue[0], $refinement);
477
            }
478
        }
479
    }
480
481
    /**
482
     * @param string     $navigationName The name of the Navigation.
483
     * @param Refinement $refinement     A RefinementRange or RefinementValue object.
484
     */
485
    public function addRefinement($navigationName, $refinement)
486
    {
487
        $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...
488
        if (array_key_exists($navigationName, $this->navigations)) {
489
            $navigation = $this->navigations[$navigationName];
490
        } else {
491
            $navigation = new Navigation();
492
            $navigation->setName($navigationName)->setRange($refinement instanceof SelectedRefinementRange);
493
            $this->navigations[$navigationName] = $navigation;
494
        }
495
        $refinements = $navigation->getRefinements();
496
        array_push($refinements, $refinement);
497
        $navigation->setRefinements($refinements);
498
    }
499
500
    /**
501
     * @param string $navigationName The name of the refinement.
502
     * @param mixed  $low            The low value.
503
     * @param mixed  $high           The high value.
504
     * @param bool   $exclude        True if the results should exclude this range refinement, false otherwise.
505
     */
506
    public function addRangeRefinement($navigationName, $low, $high, $exclude = false)
507
    {
508
        $refinement = new RefinementRange();
509
        $this->addRefinement($navigationName, $refinement->setLow($low)->setHigh($high)->setExclude($exclude));
510
    }
511
512
    /**
513
     * @param string $navigationName The name of the refinement.
514
     * @param mixed  $value          The refinement value.
515
     * @param bool   $exclude        True if the results should exclude this value refinement, false otherwise.
516
     */
517
    public function addValueRefinement($navigationName, $value, $exclude = false)
518
    {
519
        $refinement = new RefinementValue();;
520
        $this->addRefinement($navigationName, $refinement->setValue($value)->setExclude($exclude));
521
    }
522
523
    /**
524
     * @return bool Are refinements with zero counts being removed.
525
     */
526
    public function isPruneRefinements()
527
    {
528
        return $this->pruneRefinements;
529
    }
530
531
    /**
532
     * @param bool $pruneRefinements Specifies whether refinements should be pruned.
533
     */
534
    public function setPruneRefinements($pruneRefinements)
535
    {
536
        $this->pruneRefinements = $pruneRefinements;
537
    }
538
539
    /**
540
     * @return MSort[] The current list of sort parameters.
541
     */
542
    public function &getSort()
543
    {
544
        return $this->sort;
545
    }
546
547
    /**
548
     * @param MSort[] $sort Any number of sort criteria.
549
     */
550
    public function setSort($sort)
551
    {
552
        $this->sort = $sort;
553
    }
554
555
    /**
556
     * @return int The number of documents to skip.
557
     */
558
    public function getSkip()
559
    {
560
        return $this->skip;
561
    }
562
563
    /**
564
     * @param int $skip The number of documents to skip.
565
     */
566
    public function setSkip($skip)
567
    {
568
        $this->skip = $skip;
569
    }
570
571
    /**
572
     * @return CustomUrlParam[] A list of custom url params.
573
     */
574
    public function getCustomUrlParams()
575
    {
576
        return $this->customUrlParams;
577
    }
578
579
    /**
580
     * @param CustomUrlParam[] $customUrlParams Set the custom url params.
581
     */
582
    public function setCustomUrlParams($customUrlParams)
583
    {
584
        $this->customUrlParams = $customUrlParams;
585
    }
586
587
//    /**
588
//     * @return bool Is return JSON set to true.
589
//     */
590
//    public function isReturnBinary()
591
//    {
592
//        return $this->returnBinary;
593
//    }
594
//
595
//    /**
596
//     * @param bool $returnBinary Whether to tell the bridge to return binary data rather than JSON.
597
//     */
598
//    public function setReturnBinary($returnBinary)
599
//    {
600
//        $this->returnBinary = $returnBinary;
601
//    }
602
603
    /**
604
     * @return string The current language restrict value.
605
     */
606
    public function getLanguage()
607
    {
608
        return $this->language;
609
    }
610
611
    /**
612
     * @param string $language The value for language restrict.
613
     */
614
    public function setLanguage($language)
615
    {
616
        $this->language = $language;
617
    }
618
619
    /**
620
     * @return string The current biasing profile name.
621
     */
622
    public function getBiasingProfile()
623
    {
624
        return $this->biasingProfile;
625
    }
626
627
    /**
628
     * @param string $biasingProfile Override the biasing profile used for this query.
629
     */
630
    public function setBiasingProfile($biasingProfile)
631
    {
632
        $this->biasingProfile = $biasingProfile;
633
    }
634
635
    /**
636
     * @return int The current page size.
637
     */
638
    public function getPageSize()
639
    {
640
        return $this->pageSize;
641
    }
642
643
    /**
644
     * @param int $pageSize The number of records to return with the query.
645
     */
646
    public function setPageSize($pageSize)
647
    {
648
        $this->pageSize = $pageSize;
649
    }
650
651
    /**
652
     * @return boolean
653
     */
654
    public function isDisableAutocorrection()
655
    {
656
        return $this->disableAutocorrection;
657
    }
658
659
    /**
660
     * @param boolean $disableAutocorrection Specifies whether the auto-correction behavior should be disabled.
661
     *                                       By default, when no results are returned for the given query (and there is
662
     *                                       a did-you-mean available), the first did-you-mean is automatically queried
663
     *                                       instead.
664
     */
665
    public function setDisableAutocorrection($disableAutocorrection)
666
    {
667
        $this->disableAutocorrection = $disableAutocorrection;
668
    }
669
670
    /**
671
     * @return boolean
672
     */
673
    public function isWildcardSearchEnabled()
674
    {
675
        return $this->wildcardSearchEnabled;
676
    }
677
678
    /**
679
     * @param boolean $wildcardSearchEnabled Indicate if the *(star) character in the search string should be treated
680
     *                                       as a wildcard prefix search. For example, `sta*` will match `star` and
681
     *                                       `start`.
682
     */
683
    public function setWildcardSearchEnabled($wildcardSearchEnabled)
684
    {
685
        $this->wildcardSearchEnabled = $wildcardSearchEnabled;
686
    }
687
688
    /**
689
     * <b>Warning</b>  This will count as two queries against your search index.
690
     *
691
     * Typically, this feature is used when you have a large number of navigation items that will overwhelm the end
692
     * user. It works by using one of the existing navigation items to decide what the query is about and fires a second
693
     * query to restrict the navigation to the most relevant set of navigation items for this search term.
694
     *
695
     * For example, if you pass in a search of `paper` and a restrict navigation of `category:2`
696
     *
697
     * The bridge will find the category navigation refinements in the first query and fire a second query for the top 2
698
     * most populous categories.  Therefore, a search for something generic like "paper" will bring back top category
699
     * matches like copy paper (1,030), paper pads (567).  The bridge will fire off the second query with the search
700
     * term, plus an OR refinement with the most likely categories.  The navigation items in the first query are
701
     * entirely replaced with the navigation items in the second query, except for the navigation that was used for the
702
     * restriction so that users still have the ability to navigate by all category types.
703
     *
704
     * @param RestrictNavigation $restrictNavigation Restriction criteria
705
     */
706
    public function setRestrictNavigation($restrictNavigation)
707
    {
708
        $this->restrictNavigation = $restrictNavigation;
709
    }
710
711
    /** @return RestrictNavigation */
712
    public function getRestrictNavigation()
713
    {
714
        return $this->restrictNavigation;
715
    }
716
717
    /**
718
     * @return MBiasing
719
     */
720
    public function getBiasing()
721
    {
722
        return $this->biasing;
723
    }
724
725
    /**
726
     * Add a biasing profile, which is defined at query time.
727
     *
728
     * @param MBiasing $biasing
729
     */
730
    public function setBiasing($biasing)
731
    {
732
        $this->biasing = $biasing;
733
    }
734
735
    /**
736
     * @param string[] $bringToTop
737
     */
738
    public function setBringToTop($bringToTop) {
739
        if (empty($this->biasing)) {
740
            $this->biasing = new MBiasing();
741
        }
742
        $this->biasing->bringToTop = $bringToTop;
743
    }
744
745
    /**
746
     * @return string A string representation of all of the currently set refinements.
747
     */
748 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...
749
    {
750
        if (!empty($this->navigations)) {
751
            $builder = new StringBuilder();
752
            foreach ($this->navigations as $n) {
753
                foreach ($n->getRefinements() as $r) {
754
                    $builder->append(Symbol::TILDE)->append($n->getName())->append($r->toTildeString());
755
                }
756
            }
757
            if ($builder->length() > 0) {
758
                return $builder->__toString();
759
            }
760
        }
761
        return null;
762
    }
763
764
    /**
765
     * @return string A string representation of all of the currently set custom url parameters.
766
     */
767 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...
768
    {
769
        if (!empty($this->customUrlParams)) {
770
            $builder = new StringBuilder();
771
            foreach ($this->customUrlParams as $c) {
772
                $builder->append(Symbol::TILDE)->append($c->getKey())->append(Symbol::EQUAL)->append($c->getValue());
773
            }
774
            if ($builder->length() > 0) {
775
                return $builder->__toString();
776
            }
777
        }
778
        return null;
779
    }
780
781
    /**
782
     * @param MSort $sort
783
     *
784
     * @return RSort
785
     */
786
    protected static function convertSort($sort)
787
    {
788
        /** @var RSort $convertedSort */
789
        $convertedSort = null;
790
        if (!empty($sort)) {
791
            $convertedSort = new RSort();
792
            $convertedSort->setField($sort->getField());
793
            switch ($sort->getOrder()) {
794
                case MSort\Order::Ascending:
795
                    $convertedSort->setOrder(RSort\Order::Ascending);
796
                    break;
797
                case MSort\Order::Descending:
798
                    $convertedSort->setOrder(RSort\Order::Descending);
799
                    break;
800
            }
801
        }
802
        return $convertedSort;
803
    }
804
805
    /**
806
     * @param MMatchStrategy $strategy
807
     *
808
     * @return RMatchStrategy
809
     */
810
    protected static function convertPartialMatchStrategy($strategy)
811
    {
812
        /** @var RMatchStrategy $convertedStrategy */
813
        $convertedStrategy = null;
814
        if (!empty($strategy)) {
815
            $rules = $strategy->getRules();
816
            if (!empty($rules)) {
817
                $convertedStrategy = new RMatchStrategy();
818
                /** @var MPartialMatchRule $r */
819
                foreach ($rules as $r) {
820
                    array_push($rules, Query::convertPartialMatchRule($r));
821
                }
822
                $strategy->setRules($rules);
823
            }
824
        }
825
        return $convertedStrategy;
826
    }
827
828
    /**
829
     * @param MPartialMatchRule $rule
830
     *
831
     * @return RPartialMatchRule
832
     */
833
    protected static function convertPartialMatchRule($rule)
834
    {
835
        /** @var RPartialMatchRule $convertedRule */
836
        $convertedRule = null;
837
        if (!empty($rule)) {
838
            $convertedRule = new RPartialMatchRule();
839
            $convertedRule->setTerms($rule->getTerms())
840
                ->setTermsGreaterThan($rule->getTermsGreaterThan())
841
                ->setMustMatch($rule->getMustMatch())
842
                ->setPercentage($rule->isPercentage());
843
        }
844
        return $convertedRule;
845
    }
846
847
    /**
848
     * @param MBiasing $biasing
849
     *
850
     * @return Biasing
851
     */
852
    protected static function convertBiasing($biasing)
853
    {
854
        /** @var Biasing $convertedBiasing */
855
        $convertedBiasing = null;
856
        if (!empty($biasing) && !empty($biasing->bringToTop)) {
857
            $convertedBiasing = new Biasing();
858
            $convertedBiasing->setBringToTop($biasing->bringToTop);
859
        }
860
        return $convertedBiasing;
861
    }
862
863
}
864