Passed
Pull Request — master (#35)
by Anton
07:27
created

JsonApiDataProvider::getFilter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
/**
3
 * @author Anton Tuyakhov <[email protected]>
4
 */
5
6
namespace tuyakhov\jsonapi\data;
7
8
9
use Yii;
10
use yii\base\InvalidParamException;
11
use yii\data\ActiveDataProvider;
12
13
class JsonApiDataProvider extends ActiveDataProvider
14
{
15
    private $_filter;
16
17
    protected function prepareModels()
18
    {
19
        $this->query->andFilterWhere([]);
20
        return parent::prepareModels();
21
    }
22
23
    /**
24
     * Returns the filtering object used by this data provider.
25
     * @return Filter|boolean the filtering object. If this is false, it means the filtering is disabled.
26
     */
27
    public function getFilter()
28
    {
29
        if ($this->_filter === null) {
30
            $this->setFilter([]);
31
        }
32
33
        return $this->_filter;
34
    }
35
36
    /**
37
     * Sets the filter definition for this data provider.
38
     * @param array|Filter|boolean $value the filter definition to be used by this data provider.
39
     * This can be one of the following:
40
     *
41
     * - a configuration array for creating the filter definition object. The "class" element defaults
42
     *   to 'tuyakhov\jsonapi\data\Filter'
43
     * - an instance of [[Filter]] or its subclass
44
     * - false, if sorting needs to be disabled.
45
     *
46
     * @throws InvalidParamException
47
     */
48
    public function setFilter($value)
49
    {
50
        if (is_array($value)) {
51
            $config = ['class' => Filter::className()];
52
            if ($this->id !== null) {
53
                $config['filterParam'] = $this->id . '-filter';
54
            }
55
            $this->_filter = Yii::createObject(array_merge($config, $value));
56
        } elseif ($value instanceof Filter || $value === false) {
57
            $this->_filter = $value;
58
        } else {
59
            throw new InvalidParamException('Only Filter instance, configuration array or false is allowed.');
60
        }
61
    }
62
63
}