1
|
|
|
<?php |
2
|
|
|
namespace Flipside; |
3
|
|
|
/** |
4
|
|
|
* ODataParams class |
5
|
|
|
* |
6
|
|
|
* This file describes the ODataParams class |
7
|
|
|
* |
8
|
|
|
* PHP version 5 and 7 |
9
|
|
|
* |
10
|
|
|
* @author Patrick Boyd / [email protected] |
11
|
|
|
* @copyright Copyright (c) 2017, Austin Artistic Reconstruction |
12
|
|
|
* @license http://www.apache.org/licenses/ Apache 2.0 License |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* A class representing OData style URI query string parameters. |
17
|
|
|
* |
18
|
|
|
* The REST APIs use OData style URI query string parameters. |
19
|
|
|
* This class abstracts parsing of those into a more PHP friendly format. |
20
|
|
|
*/ |
21
|
|
|
class ODataParams |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The ODataFilter or false if not set |
25
|
|
|
* @var false|Data\Filter |
26
|
|
|
*/ |
27
|
|
|
public $filter = false; |
28
|
|
|
/** |
29
|
|
|
* An array of properties to expand or false if not set |
30
|
|
|
* @var false|array |
31
|
|
|
*/ |
32
|
|
|
public $expand = false; |
33
|
|
|
/** |
34
|
|
|
* An array of properties to display or false if not set |
35
|
|
|
* @var false|array |
36
|
|
|
*/ |
37
|
|
|
public $select = false; |
38
|
|
|
/** |
39
|
|
|
* An array of properties to sort by or false if not set |
40
|
|
|
* @var false|array |
41
|
|
|
*/ |
42
|
|
|
public $orderby = false; |
43
|
|
|
/** |
44
|
|
|
* The number of results to display or false if not set |
45
|
|
|
* @var false|integer |
46
|
|
|
*/ |
47
|
|
|
public $top = false; |
48
|
|
|
/** |
49
|
|
|
* The number of results to skip or false if not set |
50
|
|
|
* @var false|integer |
51
|
|
|
*/ |
52
|
|
|
public $skip = false; |
53
|
|
|
/** |
54
|
|
|
* Display the count of results |
55
|
|
|
* @var boolean |
56
|
|
|
*/ |
57
|
|
|
public $count = false; |
58
|
|
|
/** |
59
|
|
|
* Not yet implemented |
60
|
|
|
*/ |
61
|
|
|
public $search = false; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Parse the parameter array into an ODataParams instance |
65
|
|
|
* |
66
|
|
|
* @param string[] $params An key=>value array of strings representing the query string. |
67
|
|
|
*/ |
68
|
|
|
public function __construct($params) |
69
|
|
|
{ |
70
|
|
|
$this->processFilter($params); |
71
|
|
|
$this->processExpand($params); |
72
|
|
|
$this->processSelect($params); |
73
|
|
|
$this->processOrderBy($params); |
74
|
|
|
$this->processTop($params); |
75
|
|
|
$this->processSkip($params); |
76
|
|
|
$this->processCount($params); |
77
|
|
|
$this->processSearch($params); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Take the parameter array and find the Filter parameter and convert that to a \Data\Filter if present |
82
|
|
|
* |
83
|
|
|
* @param string[] $params An key=>value array of strings representing the query string. |
84
|
|
|
*/ |
85
|
|
|
protected function processFilter($params) |
86
|
|
|
{ |
87
|
|
View Code Duplication |
if(isset($params['filter'])) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$this->filter = new \Flipside\Data\Filter($params['filter']); |
90
|
|
|
} |
91
|
|
|
else if(isset($params['$filter'])) |
92
|
|
|
{ |
93
|
|
|
$this->filter = new \Flipside\Data\Filter($params['$filter']); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Take the parameter array and find the Expand parameter and convert it to a PHP array |
99
|
|
|
* |
100
|
|
|
* @param string[] $params An key=>value array of strings representing the query string. |
101
|
|
|
*/ |
102
|
|
|
protected function processExpand($params) |
103
|
|
|
{ |
104
|
|
|
if(isset($params['$expand'])) |
105
|
|
|
{ |
106
|
|
|
$this->expand = explode(',', $params['$expand']); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Take the parameter array and find the Select parameter and convert it to a PHP array |
112
|
|
|
* |
113
|
|
|
* @param string[] $params An key=>value array of strings representing the query string. |
114
|
|
|
*/ |
115
|
|
|
protected function processSelect($params) |
116
|
|
|
{ |
117
|
|
|
if(isset($params['select'])) |
118
|
|
|
{ |
119
|
|
|
$this->select = explode(',', $params['select']); |
120
|
|
|
} |
121
|
|
|
else if(isset($params['$select'])) |
122
|
|
|
{ |
123
|
|
|
$this->select = explode(',', $params['$select']); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Take the parameter array and find the OrderBy parameter and convert it to a PHP array |
129
|
|
|
* |
130
|
|
|
* @param string[] $params An key=>value array of strings representing the query string. |
131
|
|
|
*/ |
132
|
|
|
protected function processOrderBy($params) |
133
|
|
|
{ |
134
|
|
|
if(isset($params['$orderby'])) |
135
|
|
|
{ |
136
|
|
|
$this->orderby = array(); |
137
|
|
|
$orderby = explode(',', $params['$orderby']); |
138
|
|
|
$count = count($orderby); |
139
|
|
|
for($i = 0; $i < $count; $i++) |
140
|
|
|
{ |
141
|
|
|
$exp = explode(' ', $orderby[$i]); |
142
|
|
|
if(count($exp) === 1) |
143
|
|
|
{ |
144
|
|
|
//Default to assending |
145
|
|
|
$this->orderby[$exp[0]] = 1; |
146
|
|
|
} |
147
|
|
|
else |
148
|
|
|
{ |
149
|
|
|
switch($exp[1]) |
150
|
|
|
{ |
151
|
|
|
case 'asc': |
152
|
|
|
$this->orderby[$exp[0]] = 1; |
153
|
|
|
break; |
154
|
|
|
case 'desc': |
155
|
|
|
$this->orderby[$exp[0]] = -1; |
156
|
|
|
break; |
157
|
|
|
default: |
158
|
|
|
throw new Exception('Unknown orderby operation'); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Take the parameter array and find the Top parameter and convert it to an int |
167
|
|
|
* |
168
|
|
|
* @param string[] $params An key=>value array of strings representing the query string. |
169
|
|
|
*/ |
170
|
|
|
protected function processTop($params) |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
if(isset($params['$top']) && is_numeric($params['$top'])) |
173
|
|
|
{ |
174
|
|
|
$this->top = intval($params['$top']); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Take the parameter array and find the Skip parameter and convert it to an int |
180
|
|
|
* |
181
|
|
|
* @param string[] $params An key=>value array of strings representing the query string. |
182
|
|
|
*/ |
183
|
|
|
protected function processSkip($params) |
|
|
|
|
184
|
|
|
{ |
185
|
|
|
if(isset($params['$skip']) && is_numeric($params['$skip'])) |
186
|
|
|
{ |
187
|
|
|
$this->skip = intval($params['$skip']); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Take the parameter array and find the Count parameter and convert it to a boolean |
193
|
|
|
* |
194
|
|
|
* @param string[] $params An key=>value array of strings representing the query string. |
195
|
|
|
*/ |
196
|
|
|
protected function processCount($params) |
197
|
|
|
{ |
198
|
|
|
if(isset($params['$count']) && strcasecmp($params['$count'], 'true') === 0) |
199
|
|
|
{ |
200
|
|
|
$this->count = true; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Take the parameter array and find the Search parameter and process it |
206
|
|
|
* |
207
|
|
|
* @param string[] $params An key=>value array of strings representing the query string. |
208
|
|
|
*/ |
209
|
|
|
protected function processSearch($params) |
210
|
|
|
{ |
211
|
|
|
if(isset($params['$search'])) |
212
|
|
|
{ |
213
|
|
|
//throw new Exception('Search not yet implemented'); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Take an input array and filter the array based on the select parameter |
219
|
|
|
* |
220
|
|
|
* @param array $array The array to be filtered |
221
|
|
|
* |
222
|
|
|
* @return array The filtered array |
223
|
|
|
*/ |
224
|
|
|
public function filterArrayPerSelect($array) |
225
|
|
|
{ |
226
|
|
|
if($this->select === false) |
227
|
|
|
{ |
228
|
|
|
return $array; |
229
|
|
|
} |
230
|
|
|
$flip = array_flip($this->select); |
231
|
|
|
$count = count($array); |
232
|
|
|
for($i = 0; $i < $count; $i++) |
233
|
|
|
{ |
234
|
|
|
if(is_a($array[$i], '\Flipside\SerializableObject')) |
235
|
|
|
{ |
236
|
|
|
$array[$i] = array_intersect_key($array[$i]->jsonSerialize(), $flip); |
237
|
|
|
continue; |
238
|
|
|
} |
239
|
|
|
$array[$i] = array_intersect_key($array[$i], $flip); |
240
|
|
|
} |
241
|
|
|
return $array; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: