Completed
Pull Request — master (#109)
by Patrick
10:35
created

class.ODataParams.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * ODataParams class
4
 *
5
 * This file describes the ODataParams class
6
 *
7
 * PHP version 5 and 7
8
 *
9
 * @author Patrick Boyd / [email protected]
10
 * @copyright Copyright (c) 2017, Austin Artistic Reconstruction
11
 * @license http://www.apache.org/licenses/ Apache 2.0 License
12
 */
13
14
/**
15
 * A class representing OData style URI query string parameters.
16
 *
17
 * The REST APIs use OData style URI query string parameters. 
18
 * This class abstracts parsing of those into a more PHP friendly format.
19
 */
20
class ODataParams
21
{
22
    /**
23
     * The ODataFilter or false if not set
24
     * @var false|Data\Filter
25
     */
26
    public $filter = false;
27
    /**
28
     * An array of properties to expand or false if not set
29
     * @var false|array
30
     */
31
    public $expand = false;
32
    /**
33
     * An array of properties to display or false if not set
34
     * @var false|array
35
     */
36
    public $select = false;
37
    /**
38
     * An array of properties to sort by or false if not set
39
     * @var false|array
40
     */
41
    public $orderby = false;
42
    /**
43
     * The number of results to display or false if not set
44
     * @var false|integer
45
     */
46
    public $top = false;
47
    /**
48
     * The number of results to skip or false if not set
49
     * @var false|integer
50
     */
51
    public $skip = false;
52
    /**
53
     * Display the count of results
54
     * @var boolean
55
     */
56
    public $count = false;
57
    /**
58
     * Not yet implemented
59
     */
60
    public $search = false;
61
62
    /**
63
     * Parse the parameter array into an ODataParams instance
64
     *
65
     * @param string[] $params An key=>value array of strings representing the query string.
66
     */
67
    public function __construct($params)
68
    {
69
        $this->processFilter($params);
70
        $this->processExpand($params);
71
        $this->processSelect($params);
72
        $this->processOrderBy($params);
73
        $this->processTop($params);
74
        $this->processSkip($params);
75
        $this->processCount($params);
76
        $this->processSearch($params);
77
    }
78
79
    /**
80
     * Take the parameter array and find the Filter parameter and convert that to a \Data\Filter if present
81
     *
82
     * @param string[] $params An key=>value array of strings representing the query string.
83
     */
84
    protected function processFilter($params)
85
    {
86 View Code Duplication
        if(isset($params['filter']))
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
        {
88
            $this->filter = new \Data\Filter($params['filter']);
89
        }
90
        else if(isset($params['$filter']))
91
        {
92
            $this->filter = new \Data\Filter($params['$filter']);
93
        }
94
    }
95
96
    /**
97
     * Take the parameter array and find the Expand parameter and convert it to a PHP array
98
     *
99
     * @param string[] $params An key=>value array of strings representing the query string.
100
     */
101
    protected function processExpand($params)
102
    {
103
        if(isset($params['$expand']))
104
        {
105
            $this->expand = explode(',', $params['$expand']);
106
        }
107
    }
108
109
    /**
110
     * Take the parameter array and find the Select parameter and convert it to a PHP array
111
     *
112
     * @param string[] $params An key=>value array of strings representing the query string.
113
     */
114
    protected function processSelect($params)
115
    {
116
        if(isset($params['select']))
117
        {
118
            $this->select = explode(',', $params['select']);
119
        }
120
        else if(isset($params['$select']))
121
        {
122
            $this->select = explode(',', $params['$select']);
123
        }
124
    }
125
126
    /**
127
     * Take the parameter array and find the OrderBy parameter and convert it to a PHP array
128
     *
129
     * @param string[] $params An key=>value array of strings representing the query string.
130
     */
131
    protected function processOrderBy($params)
132
    {
133
        if(isset($params['$orderby']))
134
        {
135
            $this->orderby = array();
136
            $orderby = explode(',', $params['$orderby']);
137
            $count = count($orderby);
138
            for($i = 0; $i < $count; $i++)
139
            {
140
                $exp = explode(' ', $orderby[$i]);
141
                if(count($exp) === 1)
142
                {
143
                    //Default to assending
144
                    $this->orderby[$exp[0]] = 1;
145
                }
146
                else
147
                {
148
                    switch($exp[1])
149
                    {
150
                        case 'asc':
151
                            $this->orderby[$exp[0]] = 1;
152
                            break;
153
                        case 'desc':
154
                            $this->orderby[$exp[0]] = -1;
155
                            break;
156
                        default:
157
                            throw new Exception('Unknown orderby operation');
158
                    }
159
                }
160
            }
161
        }
162
    }
163
164
    /**
165
     * Take the parameter array and find the Top parameter and convert it to an int
166
     *
167
     * @param string[] $params An key=>value array of strings representing the query string.
168
     */
169
    protected function processTop($params)
170
    {
171
        if(isset($params['$top']) && is_numeric($params['$top']))
172
        {
173
            $this->top = intval($params['$top']);
174
        }
175
    }
176
177
    /**
178
     * Take the parameter array and find the Skip parameter and convert it to an int
179
     *
180
     * @param string[] $params An key=>value array of strings representing the query string.
181
     */
182
    protected function processSkip($params)
183
    {
184
        if(isset($params['$skip']) && is_numeric($params['$skip']))
185
        {
186
            $this->skip = intval($params['$skip']);
187
        }
188
    }
189
190
    /**
191
     * Take the parameter array and find the Count parameter and convert it to a boolean
192
     *
193
     * @param string[] $params An key=>value array of strings representing the query string.
194
     */
195
    protected function processCount($params)
196
    {
197
        if(isset($params['$count']) && strcasecmp($params['$count'], 'true') === 0)
198
        {
199
            $this->count = true;
200
        }
201
    }
202
203
    /**
204
     * Take the parameter array and find the Search parameter and process it
205
     *
206
     * @param string[] $params An key=>value array of strings representing the query string.
207
     */
208
    protected function processSearch($params)
209
    {
210
        if(isset($params['$search']))
211
        {
212
            //throw new Exception('Search not yet implemented');
213
        }
214
    }
215
216
    /**
217
     * Take an input array and filter the array based on the select parameter
218
     *
219
     * @param array $array The array to be filtered
220
     *
221
     * @return array The filtered array
222
     */
223
    public function filterArrayPerSelect($array)
224
    {
225
        if($this->select === false)
226
        {
227
            return $array;
228
        }
229
        $flip = array_flip($this->select);
230
        $count = count($array);
231
        for($i = 0; $i < $count; $i++)
232
        {
233
            if(is_a($array[$i], 'SerializableObject'))
234
            {
235
                $array[$i] = array_intersect_key($array[$i]->jsonSerialize(), $flip);
236
                continue;
237
            }
238
            $array[$i] = array_intersect_key($array[$i], $flip);
239
        }
240
        return $array;
241
    }
242
}
243