UserSearch::rules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace app\modules\admin\models\search;
4
5
use app\models\UserModel;
6
use yii\data\ActiveDataProvider;
7
8
/**
9
 * Class UserSearch
10
 *
11
 * @package app\modules\admin\models\search
12
 */
13
class UserSearch extends UserModel
14
{
15
    /**
16
     * @inheritdoc
17
     */
18
    public function rules(): array
19
    {
20
        return [
21
            [['id', 'status'], 'integer'],
22
            [['username', 'email'], 'safe'],
23
        ];
24
    }
25
26
    /**
27
     * Creates data provider instance with search query applied
28
     *
29
     * @param $params
30
     * @param int $pageSize
31
     *
32
     * @return ActiveDataProvider
33
     */
34
    public function search(array $params, int $pageSize = 10): ActiveDataProvider
35
    {
36
        $query = UserModel::find();
37
38
        $dataProvider = new ActiveDataProvider([
39
            'query' => $query,
40
            'pagination' => [
41
                'pageSize' => $pageSize,
42
            ],
43
            'sort' => [
44
                'defaultOrder' => ['id' => SORT_DESC],
45
            ],
46
        ]);
47
48
        $this->load($params);
49
50
        if (!$this->validate()) {
51
            return $dataProvider;
52
        }
53
54
        $query->andFilterWhere([
55
            'id' => $this->id,
56
            'status' => $this->status,
57
        ]);
58
59
        $query->andFilterWhere(['like', 'username', $this->username])
60
            ->andFilterWhere(['like', 'email', $this->email]);
61
62
        return $dataProvider;
63
    }
64
}
65