SortableItem::setKey()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ListSorter;
6
7
class SortableItem
8
{
9
    private $key;
10
    private $tableAlias;
11
    private $column;
12
    private $title;
13
    private $isSelected;
14
    private $sortDir;
15
    private $isHidden;
16
17 24
    public function __construct($key, $column = '', $tableAlias = '')
18
    {
19 24
        $this->setKey($key);
20 23
        $this->setTableAlias($tableAlias);
21 23
        $this->setColumn($column);
22 23
    }
23
24
    /**
25
     * @return string
26
     */
27 15
    public function getKey()
28
    {
29 15
        return $this->key;
30
    }
31
32
    /**
33
     * @param $key
34
     *
35
     * @throws \Exception
36
     */
37 24
    public function setKey($key)
38
    {
39 24
        if (empty($key)) {
40 1
            throw new \Exception('Sortable item key can not be empty');
41
        }
42
43 23
        $this->key = $key;
44 23
    }
45
46
    /**
47
     * @return string
48
     */
49 3
    public function getTableAlias()
50
    {
51 3
        return $this->tableAlias;
52
    }
53
54
    /**
55
     * @param string $tableAlias
56
     */
57 23
    public function setTableAlias($tableAlias)
58
    {
59 23
        $this->tableAlias = $tableAlias;
60 23
    }
61
62
    /**
63
     * @return string
64
     */
65 4
    public function getColumn()
66
    {
67 4
        if (empty($this->column)) {
68
            // fall back option: replace the spaces with under line
69 2
            return strtolower(str_replace(' ', '_', $this->getKey()));
70
        }
71
72 2
        return $this->column;
73
    }
74
75
    /**
76
     * @param string $column
77
     */
78 23
    public function setColumn($column)
79
    {
80 23
        $this->column = $column;
81 23
    }
82
83
    /**
84
     * @return string
85
     */
86 2
    public function getTitle()
87
    {
88 2
        if (empty($this->title)) {
89
            // fall back option: replace under line with space
90 1
            return ucwords(str_replace('_', ' ', $this->getKey()));
91
        }
92
93 1
        return $this->title;
94
    }
95
96
    /**
97
     * @param string $title
98
     */
99 1
    public function setTitle($title)
100
    {
101 1
        $this->title = $title;
102 1
    }
103
104
    /**
105
     * @return bool
106
     */
107 1
    public function isSelected()
108
    {
109 1
        return empty($this->isSelected) ? false : true;
110
    }
111
112
    /**
113
     * @param bool $isSelected
114
     */
115 4
    public function setIsSelected($isSelected)
116
    {
117 4
        $this->isSelected = $isSelected;
118 4
    }
119
120
    /**
121
     * @return string
122
     */
123 3
    public function getSortDir()
124
    {
125 3
        return $this->sortDir;
126
    }
127
128
    /**
129
     * @param string $sortDir
130
     */
131 5
    public function setSortDir($sortDir)
132
    {
133 5
        if (in_array($sortDir, ['asc', 'desc'])) {
134 3
            $this->sortDir = $sortDir;
135
        }
136 5
    }
137
138
    /**
139
     * @return string
140
     */
141 1
    public function getNewSortDir()
142
    {
143 1
        return $this->getSortDir() === 'asc' ? 'desc' : 'asc';
144
    }
145
146
    /**
147
     * @return string
148
     */
149 2
    public function getSortBy()
150
    {
151 2
        $table = $this->getTableAlias();
152
153 2
        if (empty($table)) {
154 1
            return $this->getColumn();
155
        }
156
157 2
        return $table.'.'.$this->getColumn();
158
    }
159
160
    /**
161
     * @return bool
162
     */
163 1
    public function isHidden()
164
    {
165 1
        return $this->isHidden;
166
    }
167
168
    /**
169
     * @param bool $isHidden
170
     */
171 1
    public function setIsHidden($isHidden)
172
    {
173 1
        $this->isHidden = $isHidden;
174 1
    }
175
}
176