Passed
Push — dev ( 7c9a77...068aed )
by Konstantin
03:03
created

ReadUsersBuilder::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace linkprofit\Tracker\builder;
4
5
use linkprofit\Tracker\request\ReadUsersRoute;
6
7
/**
8
 * Class ReadUsersBuilder
9
 *
10
 * @package linkprofit\Tracker\filter
11
 */
12
class ReadUsersBuilder extends BaseBuilder
13
{
14
    /**
15
     * @var array
16
     */
17
    private $allowedFields = [
18
        'userid', 'refid', 'username', 'apikey',
19
        'firstname', 'lastname', 'middlename',
20
        'topname', 'phone', 'city', 'regip',
21
        'dateinserted', 'datelastlogin',
22
        'status', 'commissionrate', 'managerid',
23
    ];
24
25
    /**
26
     * A — одобрен
27
     * P — в ожидании
28
     * D — отклонён
29
     *
30
     * @var array
31
     */
32
    private $allowedStatuses = [
33
        'A', 'P', 'D'
34
    ];
35
36
    /**
37
     * @var array
38
     */
39
    private $allowedSorting = [
40
        SORT_DESC, SORT_ASC
41
    ];
42
43
    /**
44
     * @return ReadUsersRoute
45
     */
46 5
    public function createRoute()
47
    {
48 5
        $route = new ReadUsersRoute();
49 5
        $route->setActiveFilters($this->toArray());
50
51 5
        return $route;
52
    }
53
54
    /**
55
     * @param $limit
56
     *
57
     * @return $this
58
     */
59 6
    public function limit($limit)
60
    {
61 6
        $this->params['limit'] = $limit;
62
63 6
        return $this;
64
    }
65
66
    /**
67
     * @param $offset
68
     *
69
     * @return $this
70
     */
71 1
    public function offset($offset)
72
    {
73 1
        $this->params['offset'] = $offset;
74
75 1
        return $this;
76
    }
77
78
    /**
79
     * @param array $fields
80
     *
81
     * @return $this
82
     */
83 6
    public function fields($fields = [])
84
    {
85 6
        if (!empty($fields)) {
86 6
            $this->params['fields'] = $this->getValidFieldsValues($fields);
87
        }
88
89 6
        return $this;
90
    }
91
92
    /**
93
     * @param array $statuses
94
     *
95
     * @return $this
96
     */
97 6
    public function statuses($statuses = [])
98
    {
99 6
        if (!empty($statuses)) {
100 6
            $this->params['statuses'] = $this->getValidStatuses($statuses);
101
        }
102
103 6
        return $this;
104
    }
105
106
    /**
107
     * @param integer $unixTime
108
     *
109
     * @return $this
110
     */
111 1
    public function dateInsertedFrom($unixTime = null)
112
    {
113 1
        if ($unixTime === null) {
114 1
            $unixTime = time();
115
        }
116
117 1
        $this->params['dateInsertedFrom'] = date('d.m.Y', $unixTime);
118
119 1
        return $this;
120
    }
121
122
    /**
123
     * @param integer $unixTime
124
     *
125
     * @return $this
126
     */
127 1
    public function dateInsertedTo($unixTime = null)
128
    {
129 1
        if ($unixTime === null) {
130 1
            $unixTime = time();
131
        }
132
133 1
        $this->params['dateInsertedTo'] = date('d.m.Y', $unixTime);
134
135 1
        return $this;
136
    }
137
138
    /**
139
     * @param $field
140
     *
141
     * @return $this
142
     */
143 1
    public function orderByField($field)
144
    {
145 1
        $this->params['orderByField'] = $field;
146
147 1
        return $this;
148
    }
149
150
    /**
151
     * @param $method
152
     *
153
     * @return $this
154
     */
155 1
    public function orderByMethod($method)
156
    {
157 1
        if (in_array($method, $this->allowedSorting, true)) {
158 1
            $method = ($method === SORT_ASC) ? 'ASC' : 'DESC';
159
        }
160
161 1
        $this->params['orderByMethod'] = $method;
162
163 1
        return $this;
164
    }
165
166
    /**
167
     * Like поиск по полям, поля не указываются
168
     *
169
     * @param string $term
170
     *
171
     * @return $this
172
     */
173 1
    public function mainFilterItem($term)
174
    {
175 1
        $this->params['mainFilterItem'] = (string) $term;
176
177 1
        return $this;
178
    }
179
180
    /**
181
     * @param integer $id
182
     *
183
     * @return $this
184
     */
185 1
    public function accountManagerId($id)
186
    {
187 1
        $this->params['accountManagerId'] = $id;
188
189 1
        return $this;
190
    }
191
192
    /**
193
     * @param array $fields
194
     *
195
     * @return array
196
     */
197 6
    protected function getValidFieldsValues($fields)
198
    {
199 6
        return array_values(array_intersect(array_map('strtolower', $fields), $this->allowedFields));
200
    }
201
202
    /**
203
     * @param $statuses
204
     *
205
     * @return array
206
     */
207 6
    protected function getValidStatuses($statuses)
208
    {
209 6
        return array_values(array_intersect(array_map('strtoupper', $statuses), $this->allowedStatuses));
210
    }
211
}
212