Passed
Push — master ( b4e3f0...d6a119 )
by Robbie
04:16 queued 02:31
created

src/Search/Criteria/SearchCriterion.php (2 issues)

Severity
1
<?php
2
3
namespace SilverStripe\FullTextSearch\Search\Criteria;
4
5
use SilverStripe\Core\Injector\Injectable;
6
use SilverStripe\FullTextSearch\Search\Adapters\SearchAdapterInterface;
7
use SilverStripe\FullTextSearch\Search\Queries\AbstractSearchQueryWriter;
8
9
/**
10
 * Class SearchCriterion
11
 * @package SilverStripe\FullTextSearch\Search\Criteria
12
 */
13
class SearchCriterion implements SearchCriteriaInterface
14
{
15
    use Injectable;
16
17
    /**
18
     * field:value
19
     *
20
     * @var string
21
     */
22
    const EQUAL = 'EQUAL';
23
24
    /**
25
     * -field:value
26
     *
27
     * @var string
28
     */
29
    const NOT_EQUAL = 'NOT_EQUAL';
30
31
    /**
32
     * field:[value TO *]
33
     *
34
     * @var string
35
     */
36
    const GREATER_EQUAL = 'GREATER_EQUAL';
37
38
    /**
39
     * field:{value TO *}
40
     *
41
     * @var string
42
     */
43
    const GREATER_THAN = 'GREATER_THAN';
44
45
    /**
46
     * field:[* TO value]
47
     *
48
     * @var string
49
     */
50
    const LESS_EQUAL = 'LESS_EQUAL';
51
52
    /**
53
     * field:{* TO value}
54
     *
55
     * @var string
56
     */
57
    const LESS_THAN = 'LESS_THAN';
58
59
    /**
60
     * (field:value1 field:value2 field:value3)
61
     *
62
     * @var string
63
     */
64
    const IN = 'IN';
65
66
    /**
67
     * -(field:value1 field:value2 field:value3)
68
     *
69
     * @var string
70
     */
71
    const NOT_IN = 'NOT_IN';
72
73
    /**
74
     * field:[* TO *]
75
     *
76
     * @var string
77
     */
78
    const ISNULL = 'ISNULL';
79
80
    /**
81
     * -field:[* TO *]
82
     *
83
     * @var string
84
     */
85
    const ISNOTNULL = 'ISNOTNULL';
86
87
    /**
88
     * A custom Criterion with it's own SearchQueryWriter
89
     *
90
     * @var string
91
     */
92
    const CUSTOM = 'CUSTOM';
93
94
    /**
95
     * @var string
96
     */
97
    protected $comparison;
98
99
    /**
100
     * The table and field that this Criterion is applied to.
101
     *
102
     * @var string
103
     */
104
    protected $target;
105
106
    /**
107
     * @var mixed
108
     */
109
    protected $value;
110
111
    /**
112
     * @var SearchAdapterInterface
113
     */
114
    protected $adapter;
115
116
    /**
117
     * @var AbstractSearchQueryWriter
118
     */
119
    protected $searchQueryWriter;
120
121
    /**
122
     * @param string $target
123
     * @param string|array $value
124
     * @param string|null $comparison
125
     * @param AbstractSearchQueryWriter $searchQueryWriter
126
     */
127
    public function __construct(
128
        $target,
129
        $value,
130
        $comparison = null,
131
        AbstractSearchQueryWriter $searchQueryWriter = null
132
    ) {
133
        // EQUAL is our default comparison.
134
        if ($comparison === null) {
135
            $comparison = SearchCriterion::EQUAL;
136
        }
137
138
        $this->setTarget($target);
139
        $this->setValue($value);
140
        $this->setComparison($comparison);
141
        $this->setSearchQueryWriter($searchQueryWriter);
142
    }
143
144
    /**
145
     * @return SearchAdapterInterface
146
     */
147
    public function getAdapter()
148
    {
149
        return $this->adapter;
150
    }
151
152
    /**
153
     * @param SearchAdapterInterface $adapter
154
     * @return $this
155
     */
156
    public function setAdapter(SearchAdapterInterface $adapter)
157
    {
158
        $this->adapter = $adapter;
159
160
        return $this;
161
    }
162
163
    /**
164
     * @param string $ps
165
     * @return void
166
     * @throws \Exception
167
     */
168
    public function appendPreparedStatementTo(&$ps)
169
    {
170
        $adapter = $this->getAdapter();
171
172
        if (!$adapter instanceof SearchAdapterInterface) {
0 ignored issues
show
$adapter is always a sub-type of SilverStripe\FullTextSea...\SearchAdapterInterface.
Loading history...
173
            throw new \Exception('No adapter has been applied to SearchCriteria');
174
        }
175
176
        $ps .= $adapter->generateQueryString($this);
177
    }
178
179
    /**
180
     * String values should be passed into our filter string with quotation marks and escaping.
181
     *
182
     * @param string $value
183
     * @return string
184
     */
185
    public function getQuoteValue($value)
186
    {
187
        if (is_string($value)) {
0 ignored issues
show
The condition is_string($value) is always true.
Loading history...
188
            return sprintf('"%s"', $value);
189
        }
190
191
        return $value;
192
    }
193
194
    /**
195
     * @return AbstractSearchQueryWriter
196
     */
197
    public function getSearchQueryWriter()
198
    {
199
        return $this->searchQueryWriter;
200
    }
201
202
    /**
203
     * @param AbstractSearchQueryWriter $searchQueryWriter
204
     * @return $this
205
     */
206
    public function setSearchQueryWriter($searchQueryWriter)
207
    {
208
        $this->searchQueryWriter = $searchQueryWriter;
209
210
        return $this;
211
    }
212
213
    /**
214
     * @return string
215
     */
216
    public function getComparison()
217
    {
218
        return $this->comparison;
219
    }
220
221
    /**
222
     * @param string|null $comparison
223
     * @return $this
224
     */
225
    protected function setComparison($comparison)
226
    {
227
        $this->comparison = $comparison;
228
229
        return $this;
230
    }
231
232
    /**
233
     * @return string
234
     */
235
    public function getTarget()
236
    {
237
        return $this->target;
238
    }
239
240
    /**
241
     * @param string $target
242
     * @return $this
243
     */
244
    protected function setTarget($target)
245
    {
246
        $this->target = $target;
247
248
        return $this;
249
    }
250
251
    /**
252
     * @return string|array
253
     */
254
    public function getValue()
255
    {
256
        return $this->value;
257
    }
258
259
    /**
260
     * @param string|array $value
261
     * @return $this
262
     */
263
    protected function setValue($value)
264
    {
265
        $this->value = $value;
266
267
        return $this;
268
    }
269
}
270