Completed
Pull Request — develop (#18)
by Ben
06:42 queued 03:17
created

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