Completed
Push — master ( 89011d...ce8f51 )
by Klochok
03:44
created

IndexPageUiOptions::rules()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 0
cts 17
cp 0
rs 9.4285
cc 3
eloc 12
nc 1
nop 0
crap 12
1
<?php
2
3
namespace hipanel\models;
4
5
use Yii;
6
use yii\base\Model;
7
8
class IndexPageUiOptions extends Model
9
{
10
    const ORIENTATION_VERTICAL = 'vertical';
11
12
    const ORIENTATION_HORIZONTAL = 'horizontal';
13
14
    public $sort;
15
16
    public $per_page;
17
18
    public $orientation;
19
20
    public $representation;
21
22
    public $availableRepresentations = [];
23
24
    public function fields()
25
    {
26
        return ['sort', 'per_page', 'orientation', 'representation'];
27
    }
28
29
    public function rules()
30
    {
31
        return [
32
            ['per_page', 'default', 'value' => 25],
33
            ['per_page', 'number', 'skipOnEmpty' => true],
34
35
            ['per_page', 'default', 'value' => null],
36
            ['sort', 'string', 'skipOnEmpty' => true],
37
38
            ['orientation', 'default', 'value' => $this->getDefaultOrientation()],
39
            ['orientation', 'in', 'range' => array_keys($this->getOrientationOptions())],
40
41
            ['representation', 'default', 'value' => null],
42
            ['representation', function ($attribute, $params, $validator) {
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $validator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
                if (!empty($this->availableRepresentations) && !in_array($this->{$attribute}, $this->availableRepresentations)) {
44
                    $this->addError($attribute, 'The token must contain letters or digits.');
45
                }
46
            }],
47
        ];
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    public function getOrientationOptions()
54
    {
55
        return [
56
            self::ORIENTATION_HORIZONTAL => Yii::t('hipanel', 'Horizontal'),
57
            self::ORIENTATION_VERTICAL => Yii::t('hipanel', 'Vertical'),
58
        ];
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    public function getDefaultOrientation()
65
    {
66
        $settings = Yii::$app->themeManager->getSettings();
67
        $orientationOptions = array_keys($this->getOrientationOptions());
68
69
        if (property_exists($settings, 'filterOrientation') && in_array($settings->filterOrientation, $orientationOptions, true)) {
70
            return $settings->filterOrientation;
71
        } else {
72
            return reset($orientationOptions);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression reset($orientationOptions); of type string|false adds false to the return on line 72 which is incompatible with the return type documented by hipanel\models\IndexPage...::getDefaultOrientation of type string. It seems like you forgot to handle an error condition.
Loading history...
73
        }
74
    }
75
76
    public function getSortDirection()
77
    {
78
        return (strncmp($this->sort, '-', 1) === 0) ? SORT_DESC : SORT_ASC;
79
    }
80
81
    public function getSortAttribute()
82
    {
83
        $attribute = $this->sort;
84
        if (strncmp($attribute, '-', 1) === 0) {
85
            $attribute = substr($attribute, 1);
86
        }
87
88
        return $attribute;
89
    }
90
}
91