1 | <?php |
||
7 | trait QueryStringParserTrait |
||
8 | { |
||
9 | /** |
||
10 | * Parse HTTP query string and return array representation |
||
11 | * to be attached to a database query. |
||
12 | * |
||
13 | * @param string $query |
||
14 | * |
||
15 | * @return array |
||
16 | */ |
||
17 | 16 | public function parseQueryString($query) |
|
36 | |||
37 | /** |
||
38 | * Map the parsed query string in to correct array structure. |
||
39 | * |
||
40 | * @param string $key |
||
41 | * @param mixed $value |
||
42 | * |
||
43 | * @return array|boolean |
||
44 | */ |
||
45 | 15 | protected function filterQueryParams($key, $value) |
|
46 | { |
||
47 | switch ($key) { |
||
48 | 15 | case 'limit': |
|
49 | 15 | case 'offset': |
|
50 | 1 | return (int) $value; |
|
51 | 15 | case 'sort': |
|
52 | 5 | return $this->parseSort($value); |
|
53 | 14 | case 'filter': |
|
54 | 10 | return $this->parseFilters((array) $value); |
|
55 | 6 | case 'has': |
|
56 | 1 | return explode(',', $value); |
|
57 | 5 | case 'search': |
|
58 | 4 | return $this->parseSearch($value); |
|
59 | 4 | case 'minscore': |
|
60 | 3 | return (float) $value; |
|
61 | 1 | default: |
|
62 | 1 | return false; |
|
63 | 1 | } |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * Map sorts to a usable format. |
||
68 | * |
||
69 | * @param string $value |
||
70 | * |
||
71 | * @return array |
||
72 | */ |
||
73 | 5 | protected function parseSort($value) |
|
94 | |||
95 | /** |
||
96 | * Map search to a usable format. |
||
97 | * |
||
98 | * @param string $value |
||
99 | * |
||
100 | * @return array |
||
101 | */ |
||
102 | 4 | protected function parseSearch($value) |
|
117 | |||
118 | /** |
||
119 | * Map filters in to useable array. |
||
120 | * |
||
121 | * @param array $filters |
||
122 | * |
||
123 | * @return array |
||
124 | */ |
||
125 | 10 | protected function parseFilters(array $filters) |
|
156 | } |
||
157 |