FacetLinkHelper::getDrillDownFacetLink()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 14
rs 9.9666
cc 3
nc 3
nop 2
1
<?php declare(strict_types = 1);
2
3
/**
4
 * Created by PhpStorm.
5
 * User: gordon
6
 * Date: 25/3/2561
7
 * Time: 17:01 น.
8
 */
9
10
namespace Suilven\FreeTextSearch\Helper;
11
12
use SilverStripe\Control\Controller;
13
14
// @todo Fix the root cause of this
15
// @phpcs:disable SlevomatCodingStandard.Operators.DisallowEqualOperators.DisallowedEqualOperator
16
class FacetLinkHelper
17
{
18
    /** @var string */
19
    private $query;
20
21
    /** @var array<string,string|int|float|bool> */
22
    private $params = [];
23
24
    /** @var string */
25
    private $facetInContext = '';
26
27
    public function __construct()
28
    {
29
        $controller = Controller::curr();
30
        $params = $controller->getRequest()->getVars();
31
        $this->query = isset($params['q'])
32
            ? $params['q']
33
            : '';
34
        $this->params = $params;
35
    }
36
37
38
    public function setFacetInContext(string $facetInContext): void
39
    {
40
        $this->facetInContext = $facetInContext;
41
    }
42
43
44
    /** @param bool|float|int|string $key */
45
    public function isSelectedFacet($key): bool
46
    {
47
        // @TODO === $key on the RHS
48
        return isset($this->params[$this->facetInContext]) && $this->params[$this->facetInContext] == $key;
49
    }
50
51
52
    /** @param bool|float|int|string $facetKey */
53
    public function getDrillDownFacetLink(string $searchPageLink, $facetKey): string
54
    {
55
        $result = $searchPageLink . '?';
56
        $facetParams = \array_merge($this->params, [$this->facetInContext => $facetKey]);
57
        foreach ($facetParams as $key => $value) {
58
            $encodedValue = \is_string($value)
59
                ? \urlencode($value)
60
                : $value;
61
            $result .= $key .'=' . ($encodedValue) .'&';
62
        }
63
64
        $result = \rtrim($result, '&');
65
66
        return $result;
67
    }
68
69
70
    /** @param bool|float|int|string $facetKey */
71
    public function getClearFacetLink(string $searchPageLink, $facetKey): string
72
    {
73
        $result = $searchPageLink . '?';
74
        foreach ($this->params as $key => $value) {
75
            if ($key === $facetKey) {
76
                continue;
77
            }
78
            $encodedValue = \is_string($value)
79
                ? \urlencode($value)
80
                : $value;
81
            $result .= $key .'=' . ($encodedValue) .'&';
82
        }
83
84
        $result = \rtrim($result, '&');
85
86
        return $result;
87
    }
88
}
89