Completed
Pull Request — master (#150)
by
unknown
03:45
created

getOpenComparisonContainer()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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

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