1 | <?php |
||
7 | trait QueryStringParserTrait |
||
8 | { |
||
9 | /** |
||
10 | * @var integer |
||
11 | */ |
||
12 | protected $param = 0; |
||
13 | |||
14 | /** |
||
15 | * Parse HTTP query string and return array representation |
||
16 | * to be attached to a database query. |
||
17 | * |
||
18 | * @param string $query |
||
19 | * |
||
20 | * @return array |
||
21 | */ |
||
22 | 21 | public function parseQueryString($query) |
|
41 | |||
42 | /** |
||
43 | * Map the parsed query string in to correct array structure. |
||
44 | * |
||
45 | * @param string $key |
||
46 | * @param mixed $value |
||
47 | * |
||
48 | * @return array|boolean |
||
49 | */ |
||
50 | 19 | protected function filterQueryParams($key, $value) |
|
51 | { |
||
52 | switch ($key) { |
||
53 | 19 | case 'limit': |
|
54 | 19 | case 'offset': |
|
55 | 1 | return (int) $value; |
|
56 | 19 | case 'sort': |
|
57 | 7 | return $this->parseSort($value); |
|
58 | 17 | case 'filter': |
|
59 | 10 | return $this->parseFilters((array) $value); |
|
60 | 9 | case 'has': |
|
61 | 2 | return explode(',', $value); |
|
62 | 8 | case 'include': |
|
63 | 3 | return $this->parseInclude(explode(',', $value)); |
|
64 | 5 | case 'search': |
|
65 | 4 | return $this->parseSearch($value); |
|
66 | 4 | case 'minscore': |
|
67 | 3 | return (float) $value; |
|
68 | 1 | default: |
|
69 | 1 | return false; |
|
70 | 1 | } |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * Map sorts to a usable format. |
||
75 | * |
||
76 | * @param string $value |
||
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | 7 | protected function parseSort($value) |
|
101 | |||
102 | /** |
||
103 | * Map search to a usable format. |
||
104 | * |
||
105 | * @param string $value |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | 4 | protected function parseSearch($value) |
|
124 | |||
125 | /** |
||
126 | * Map filters in to useable array. |
||
127 | * |
||
128 | * @param array $filters |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | 10 | protected function parseFilters(array $filters) |
|
144 | |||
145 | /** |
||
146 | * Parse an individual filter. |
||
147 | * |
||
148 | * @param string $filter |
||
149 | * |
||
150 | * @return array |
||
151 | */ |
||
152 | 12 | protected function parseFilter($filter) |
|
176 | |||
177 | /** |
||
178 | * Map includes in to useable array. |
||
179 | * |
||
180 | * @param array $includes |
||
181 | * |
||
182 | * @return array |
||
183 | */ |
||
184 | 3 | protected function parseInclude(array $includes) |
|
218 | } |
||
219 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.