ReadUsersBuilder::mainFilterItem()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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