Completed
Pull Request — develop (#20)
by
unknown
04:04 queued 02:05
created

Query::convertBiasing()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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