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