Completed
Branch master (9efafe)
by Patrick
03:38
created

class.ODataParams.php (2 issues)

Labels
Severity

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
class ODataParams
3
{
4
    /**
5
     * The ODataFilter or false if not set
6
     * @var false|Data\Filter
7
     */
8
    public $filter = false;
9
    /**
10
     * An array of properties to expand or false if not set
11
     * @var false|array
12
     */
13
    public $expand = false;
14
    /**
15
     * An array of properties to display or false if not set
16
     * @var false|array
17
     */
18
    public $select = false;
19
    /**
20
     * An array of properties to sort by or false if not set
21
     * @var false|array
22
     */
23
    public $orderby = false;
24
    /**
25
     * The number of results to display or false if not set
26
     * @var false|integer
27
     */
28
    public $top = false;
29
    /**
30
     * The number of results to skip or false if not set
31
     * @var false|integer
32
     */
33
    public $skip = false;
34
    /**
35
     * Display the count of results
36
     * @var boolean
37
     */
38
    public $count = false;
39
    public $search = false;
40
41
    public function __construct($params)
42
    {
43
        $this->processFilter($params);
44
        $this->processExpand($params);
45
        $this->processSelect($params);
46
        $this->processOrderBy($params);
47
        $this->processTop($params);
48
        $this->processSkip($params);
49
        $this->processCount($params);
50
        $this->processSearch($params);
51
    }
52
53
    protected function processFilter($params)
54
    {
55
        if(isset($params['filter']))
56
        {
57
            $this->filter = new \Data\Filter($params['filter']);
58
        }
59
        else if(isset($params['$filter']))
60
        {
61
            $this->filter = new \Data\Filter($params['$filter']);
62
        }
63
    }
64
65
    protected function processExpand($params)
66
    {
67
        if(isset($params['$expand']))
68
        {
69
            $this->expand = explode(',', $params['$expand']);
70
        }
71
    }
72
73
    protected function processSelect($params)
74
    {
75
        if(isset($params['select']))
76
        {
77
            $this->select = explode(',', $params['select']);
78
        }
79
        else if(isset($params['$select']))
80
        {
81
            $this->select = explode(',', $params['$select']);
82
        }
83
    }
84
85
    protected function processOrderBy($params)
86
    {
87
        if(isset($params['$orderby']))
88
        {
89
            $this->orderby = array();
90
            $orderby = explode(',', $params['$orderby']);
91
            $count = count($orderby);
92
            for($i = 0; $i < $count; $i++)
93
            {
94
                $exp = explode(' ', $orderby[$i]);
95
                if(count($exp) === 1)
96
                {
97
                    //Default to assending
98
                    $this->orderby[$exp[0]] = 1;
99
                }
100
                else
101
                {
102
                    switch($exp[1])
103
                    {
104
                        case 'asc':
105
                            $this->orderby[$exp[0]] = 1;
106
                            break;
107
                        case 'desc':
108
                            $this->orderby[$exp[0]] = -1;
109
                            break;
110
                        default:
111
                            throw new Exception('Unknown orderby operation');
112
                    }
113
                }
114
            }
115
        }
116
    }
117
118
    protected function processTop($params)
119
    {
120
        if(isset($params['$top']) && is_numeric($params['$top']))
121
        {
122
            $this->top = intval($params['$top']);
123
        }
124
    }
125
126
    protected function processSkip($params)
127
    {
128
        if(isset($params['$skip']) && is_numeric($params['$skip']))
129
        {
130
            $this->skip = intval($params['$skip']);
131
        }
132
    }
133
134
    protected function processCount($params)
135
    {
136
        if(isset($params['$count']) && strcasecmp($params['$count'], 'true') === 0)
137
        {
138
            $this->count = true;
139
        }
140
    }
141
142
    protected function processSearch($params)
143
    {
144
        if(isset($params['$search']))
145
        {
146
            throw new Exception('Search not yet implemented');
147
        }
148
    }
149
150
    public function filterArrayPerSelect($array)
151
    {
152
        $flip = array_flip($this->select);
153
        $count = count($leads);
0 ignored issues
show
The variable $leads does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
154
        for($i = 0; $i < $count; $i++)
155
        {
156
            if(is_a($array[$i], 'SerializableObject'))
157
            {
158
                $array[$i] = array_intersect_key($array[$i]->jsonSerialize(), $flip);
159
                continue;
160
            }
161
            $array[$i] = array_intersect_key($array[$i], $select);
0 ignored issues
show
The variable $select does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
162
        }
163
        return $array;
164
    }
165
}
166