Completed
Push — master ( 370b73...bafc2b )
by Oscar
03:27
created

SearchQuery   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 257
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 35
c 3
b 0
f 1
lcom 2
cbo 0
dl 0
loc 257
rs 9

16 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 14 5
D setQuery() 0 31 9
B getQuery() 0 20 5
A getPage() 0 4 1
A setPage() 0 6 1
A getLimit() 0 4 1
A setLimit() 0 6 1
A getIds() 0 4 1
A setIds() 0 6 1
A getWords() 0 4 1
A setWords() 0 6 1
A setConditions() 0 6 1
A getConditions() 0 4 1
A setSortAndDirection() 0 15 3
A getSort() 0 4 1
A getDirection() 0 4 2
1
<?php
2
3
namespace Folk;
4
5
/**
6
 * Class to manage a query to search rows.
7
 */
8
class SearchQuery
9
{
10
    protected $limit = 50;
11
    protected $page;
12
    protected $ids = [];
13
    protected $conditions = [];
14
    protected $words = [];
15
    protected $sort;
16
    protected $direction;
17
18
    /**
19
     * @param array $query
20
     */
21
    public function __construct(array $query = [])
22
    {
23
        if (!empty($query['query'])) {
24
            $this->setQuery($query['query']);
25
        }
26
27
        if (!empty($query['page'])) {
28
            $this->setPage($query['page']);
29
        }
30
31
        if (!empty($query['sort'])) {
32
            $this->setSortAndDirection($query['sort'], isset($query['direction']) ? $query['direction'] : null);
33
        }
34
    }
35
36
    /**
37
     * Set the a query.
38
     *
39
     * @param string $query
40
     *
41
     * @return self
42
     */
43
    public function setQuery($query)
44
    {
45
        $this->conditions = $this->ids = $this->words = [];
46
47
        preg_match_all('/([\w]+:)?("([^"]*)"|([^ ]*))/', trim($query), $pieces, PREG_SET_ORDER);
48
49
        if (is_array($pieces)) {
50
            foreach ($pieces as $piece) {
51
                if (empty($piece[0])) {
52
                    continue;
53
                }
54
55
                $name = $piece[1] ? substr($piece[1], 0, -1) : null;
56
                $value = isset($piece[4]) ? $piece[4] : $piece[3];
57
58
                if ($name !== null) {
59
                    if (!isset($this->conditions[$name])) {
60
                        $this->conditions[$name] = [$value];
61
                    } else {
62
                        $this->conditions[$name][] = $value;
63
                    }
64
                } elseif (preg_match('/^#[\w-]+$/', $value)) {
65
                    $this->ids[] = substr($value, 1);
66
                } else {
67
                    $this->words[] = $value;
68
                }
69
            }
70
        }
71
72
        return $this;
73
    }
74
75
    /**
76
     * Returns the query as string.
77
     *
78
     * @return string
79
     */
80
    public function getQuery()
81
    {
82
        $query = implode(' ', $this->words);
83
84
        foreach ($this->ids as $id) {
85
            $query .= " #{$id}";
86
        }
87
88
        foreach ($this->conditions as $name => $values) {
89
            foreach ($values as $value) {
90
                if (strpos($value, ' ') === false) {
91
                    $query .= " {$name}:{$value}";
92
                } else {
93
                    $query .= " {$name}:\"{$value}\"";
94
                }
95
            }
96
        }
97
98
        return trim($query);
99
    }
100
101
    /**
102
     * Returns the page number.
103
     *
104
     * @return null|int
105
     */
106
    public function getPage()
107
    {
108
        return $this->page;
109
    }
110
111
    /**
112
     * Set the page.
113
     *
114
     * @param null|int $page
115
     * 
116
     * @return self
117
     */
118
    public function setPage($page)
119
    {
120
        $this->page = (int) $page;
121
122
        return $this;
123
    }
124
125
    /**
126
     * Returns the limit of results per page.
127
     *
128
     * @return int
129
     */
130
    public function getLimit()
131
    {
132
        return $this->limit;
133
    }
134
135
    /**
136
     * Set the limit of results per page.
137
     *
138
     * @param int $limit
139
     * 
140
     * @return self
141
     */
142
    public function setLimit($limit)
143
    {
144
        $this->limit = (int) $limit;
145
146
        return $this;
147
    }
148
149
    /**
150
     * Returns all ids found.
151
     *
152
     * @return array
153
     */
154
    public function getIds()
155
    {
156
        return $this->ids;
157
    }
158
159
    /**
160
     * Set new ids.
161
     *
162
     * @param array $ids
163
     * 
164
     * @return self
165
     */
166
    public function setIds(array $ids)
167
    {
168
        $this->ids = $ids;
169
170
        return $this;
171
    }
172
173
    /**
174
     * Returns all words in the query.
175
     *
176
     * @return array
177
     */
178
    public function getWords()
179
    {
180
        return $this->words;
181
    }
182
183
    /**
184
     * Set new words.
185
     *
186
     * @param array $words
187
     * 
188
     * @return self
189
     */
190
    public function setWords(array $words)
191
    {
192
        $this->words = $words;
193
194
        return $this;
195
    }
196
197
    /**
198
     * Set new conditions.
199
     *
200
     * @param array $conditions
201
     * 
202
     * @return self
203
     */
204
    public function setConditions(array $conditions)
0 ignored issues
show
Unused Code introduced by
The parameter $conditions is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
205
    {
206
        $this->conditions[$name] = $value;
0 ignored issues
show
Bug introduced by
The variable $name does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $value does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
207
208
        return $this;
209
    }
210
211
    /**
212
     * Return all conditions.
213
     *
214
     * @return array
215
     */
216
    public function getConditions()
217
    {
218
        return $this->conditions;
219
    }
220
221
    /**
222
     * Set the sort and direction fields.
223
     *
224
     * @param string $sort
225
     * @param string $direction
226
     * 
227
     * @return self
228
     */
229
    public function setSortAndDirection($sort, $direction)
230
    {
231
        $this->sort = $this->direction = null;
232
233
        if (!empty($sort)) {
234
            $this->sort = $sort;
235
            $this->direction = strtoupper($direction);
236
237
            if ($this->direction !== 'DESC') {
238
                $this->direction = 'ASC';
239
            }
240
        }
241
242
        return $this;
243
    }
244
245
    /**
246
     * Return the sort field.
247
     *
248
     * @return string|null
249
     */
250
    public function getSort()
251
    {
252
        return $this->sort;
253
    }
254
255
    /**
256
     * Return the sort direction in UPPERCASE.
257
     *
258
     * @return string|null
259
     */
260
    public function getDirection()
261
    {
262
        return isset($this->direction) ? strtoupper($this->direction) : null;
263
    }
264
}
265