SolrSearchQueryWriterRange::generateQueryString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 11
rs 9.9666
1
<?php
2
3
namespace SilverStripe\FullTextSearch\Solr\Writers;
4
5
use InvalidArgumentException;
6
use SilverStripe\FullTextSearch\Search\Criteria\SearchCriterion;
7
use SilverStripe\FullTextSearch\Search\Queries\AbstractSearchQueryWriter;
8
9
/**
10
 * Class SolrSearchQueryWriter_Range
11
 * @package SilverStripe\FullTextSearch\Solr\Writers
12
 */
13
class SolrSearchQueryWriterRange extends AbstractSearchQueryWriter
14
{
15
    /**
16
     * @param SearchCriterion $searchCriterion
17
     * @return string
18
     */
19
    public function generateQueryString(SearchCriterion $searchCriterion)
20
    {
21
        return sprintf(
22
            '%s(%s:%s%s%s%s%s)',
23
            $this->getComparisonPolarity($searchCriterion->getComparison()),
24
            addslashes($searchCriterion->getTarget()),
25
            $this->getOpenComparisonContainer($searchCriterion->getComparison()),
26
            $this->getLeftComparison($searchCriterion),
0 ignored issues
show
Bug introduced by
It seems like $this->getLeftComparison($searchCriterion) can also be of type array; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

26
            /** @scrutinizer ignore-type */ $this->getLeftComparison($searchCriterion),
Loading history...
27
            $this->getComparisonConjunction(),
28
            $this->getRightComparison($searchCriterion),
29
            $this->getCloseComparisonContainer($searchCriterion->getComparison())
30
        );
31
    }
32
33
    /**
34
     * Is this a positive (+) or negative (-) Solr comparison.
35
     *
36
     * @param string $comparison
37
     * @return string
38
     */
39
    protected function getComparisonPolarity($comparison)
40
    {
41
        switch ($comparison) {
42
            case SearchCriterion::ISNULL:
43
                return '-';
44
            default:
45
                return '+';
46
        }
47
    }
48
49
    /**
50
     * Select the value that we want as our left comparison value.
51
     *
52
     * @param SearchCriterion $searchCriterion
53
     * @return mixed|string
54
     * @throws InvalidArgumentException
55
     */
56
    protected function getLeftComparison(SearchCriterion $searchCriterion)
57
    {
58
        switch ($searchCriterion->getComparison()) {
59
            case SearchCriterion::GREATER_EQUAL:
60
            case SearchCriterion::GREATER_THAN:
61
                return $searchCriterion->getValue();
62
            case SearchCriterion::ISNULL:
63
            case SearchCriterion::ISNOTNULL:
64
            case SearchCriterion::LESS_EQUAL:
65
            case SearchCriterion::LESS_THAN:
66
                return '*';
67
            default:
68
                throw new InvalidArgumentException('Invalid comparison for RangeCriterion');
69
        }
70
    }
71
72
    /**
73
     * Select the value that we want as our right comparison value.
74
     *
75
     * @param SearchCriterion $searchCriterion
76
     * @return mixed|string
77
     * @throws InvalidArgumentException
78
     */
79
    protected function getRightComparison(SearchCriterion $searchCriterion)
80
    {
81
        switch ($searchCriterion->getComparison()) {
82
            case SearchCriterion::GREATER_EQUAL:
83
            case SearchCriterion::GREATER_THAN:
84
            case SearchCriterion::ISNULL:
85
            case SearchCriterion::ISNOTNULL:
86
                return '*';
87
            case SearchCriterion::LESS_EQUAL:
88
            case SearchCriterion::LESS_THAN:
89
                return $searchCriterion->getValue();
90
            default:
91
                throw new InvalidArgumentException('Invalid comparison for RangeCriterion');
92
        }
93
    }
94
95
    /**
96
     * Decide how we are comparing our left and right values.
97
     *
98
     * @return string
99
     */
100
    protected function getComparisonConjunction()
101
    {
102
        return ' TO ';
103
    }
104
105
    /**
106
     * Does our comparison need a container? EG: "[* TO *]"? If so, return the opening container brace.
107
     *
108
     * @param string $comparison
109
     * @return string
110
     * @throws InvalidArgumentException
111
     */
112
    protected function getOpenComparisonContainer($comparison)
113
    {
114
        switch ($comparison) {
115
            case SearchCriterion::GREATER_EQUAL:
116
            case SearchCriterion::LESS_EQUAL:
117
            case SearchCriterion::ISNULL:
118
            case SearchCriterion::ISNOTNULL:
119
                return '[';
120
            case SearchCriterion::GREATER_THAN:
121
            case SearchCriterion::LESS_THAN:
122
                return '{';
123
            default:
124
                throw new InvalidArgumentException('Invalid comparison for RangeCriterion');
125
        }
126
    }
127
128
    /**
129
     * Does our comparison need a container? EG: "[* TO *]"? If so, return the closing container brace.
130
     *
131
     * @param string $comparison
132
     * @return string
133
     * @throws InvalidArgumentException
134
     */
135
    protected function getCloseComparisonContainer($comparison)
136
    {
137
        switch ($comparison) {
138
            case SearchCriterion::GREATER_EQUAL:
139
            case SearchCriterion::LESS_EQUAL:
140
            case SearchCriterion::ISNULL:
141
            case SearchCriterion::ISNOTNULL:
142
                return ']';
143
            case SearchCriterion::GREATER_THAN:
144
            case SearchCriterion::LESS_THAN:
145
                return '}';
146
            default:
147
                throw new InvalidArgumentException('Invalid comparison for RangeCriterion');
148
        }
149
    }
150
}
151