IndexPageUiOptions::fields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * HiPanel core package
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\models;
12
13
use Yii;
14
use yii\base\Model;
15
16
class IndexPageUiOptions extends Model
17
{
18
    const ORIENTATION_VERTICAL = 'vertical';
19
20
    const ORIENTATION_HORIZONTAL = 'horizontal';
21
22
    public $sort;
23
24
    public $per_page;
25
26
    public $orientation;
27
28
    public $representation;
29
30
    public $availableRepresentations = [];
31
32
    public function fields()
33
    {
34
        return ['sort', 'per_page', 'orientation', 'representation'];
35
    }
36
37
    public function rules()
38
    {
39
        return [
40
            ['per_page', 'default', 'value' => 25],
41
            ['per_page', 'number', 'skipOnEmpty' => true],
42
43
            ['sort', 'default', 'value' => null],
44
            ['sort', 'string', 'skipOnEmpty' => true],
45
46
            ['orientation', 'default', 'value' => $this->getDefaultOrientation()],
47
            ['orientation', 'in', 'range' => array_keys($this->getOrientationOptions())],
48
49
            ['representation', 'default', 'value' => null],
50
            ['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...
51
                if (!empty($this->availableRepresentations) && in_array($this->{$attribute}, $this->availableRepresentations, true)) {
52
                    $this->addError($attribute, 'The token must contain letters or digits.');
53
                }
54
            }],
55
        ];
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function getOrientationOptions()
62
    {
63
        return [
64
            self::ORIENTATION_HORIZONTAL => Yii::t('hipanel', 'Horizontal'),
65
            self::ORIENTATION_VERTICAL => Yii::t('hipanel', 'Vertical'),
66
        ];
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    public function getDefaultOrientation()
73
    {
74
        $settings = isset(Yii::$app->themeManager) ? Yii::$app->themeManager->getSettings() : null;
75
        $orientationOptions = array_keys($this->getOrientationOptions());
76
77
        if (isset($settings->filterOrientation) && in_array($settings->filterOrientation, $orientationOptions, true)) {
78
            return $settings->filterOrientation;
79
        } else {
80
            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 80 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...
81
        }
82
    }
83
84
    public function getSortDirection()
85
    {
86
        return (strncmp($this->sort, '-', 1) === 0) ? SORT_DESC : SORT_ASC;
87
    }
88
89
    public function getSortAttribute()
90
    {
91
        $attribute = $this->sort;
92
        if (strncmp($attribute, '-', 1) === 0) {
93
            $attribute = substr($attribute, 1);
94
        }
95
96
        return $attribute;
97
    }
98
}
99