Completed
Push — master ( a1d335...58f4fb )
by Timo
05:05
created

Sorter::removeField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace hamburgscleanest\DataTables\Models\DataComponents;
4
5
use hamburgscleanest\DataTables\Facades\SessionHelper;
6
use hamburgscleanest\DataTables\Models\Column;
7
use hamburgscleanest\DataTables\Models\DataComponent;
8
use Illuminate\Database\Eloquent\Builder;
9
10
/**
11
 * Class Sorter
12
 * @package hamburgscleanest\DataTables\Models\DataComponents
13
 */
14
class Sorter extends DataComponent {
15
16
    const SORTING_SEPARATOR = '~';
17
    const COLUMN_SEPARATOR  = '.';
18
19
    /** @var array */
20
    private $_sortFields = [];
21
22
    /**
23
     * Sorter constructor.
24
     * @param null|array $fields
25
     * @param bool $remember
26
     */
27 9
    public function __construct(array $fields = null, bool $remember = false)
28
    {
29 9
        $this->_rememberKey = 'sort';
30 9
        $this->_rememberState = $remember;
31
32 9
        if ($fields !== null)
33
        {
34 6
            foreach ($fields as $fieldName => $direction)
35
            {
36 6
                if (\is_int($fieldName))
37
                {
38 3
                    $fieldName = $direction;
39 3
                    $direction = 'asc';
40
                }
41
42 6
                $this->_sortFields[$fieldName] = \mb_strtolower($direction);
43
            }
44
        }
45 9
    }
46
47
    /**
48
     * @return Builder
49
     */
50 7
    protected function _shapeData() : Builder
51
    {
52 7
        if (\count($this->_sortFields) > 0)
53
        {
54 5
            foreach ($this->_sortFields as $fieldName => $direction)
55
            {
56 5
                if ($direction === 'none')
57
                {
58 1
                    $this->removeField($fieldName);
59 1
                    continue;
60
                }
61
62 4
                $this->_sortField($fieldName, $direction);
63
            }
64
        }
65
66 7
        return $this->_queryBuilder;
67
    }
68
69
    /**
70
     * Stop sorting by this column
71
     *
72
     * @param string $field
73
     *
74
     * @return Sorter
75
     */
76 1
    public function removeField(string $field) : Sorter
77
    {
78 1
        if (isset($this->_sortFields[$field]))
79
        {
80 1
            unset($this->_sortFields[$field]);
81
        }
82
83 1
        return $this;
84
    }
85
86
    /**
87
     * @param string $fieldName
88
     * @param string $direction
89
     */
90
    private function _sortField(string $fieldName, string $direction) : void
91
    {
92
        /** @var Column $column */
93 4
        $column = \array_first($this->_columns, function($index, $column) use ($fieldName)
94
        {
95
            /** @var Column $column */
96 4
            return $column->getKey() === $fieldName;
97 4
        });
98
99 4
        if ($column !== null)
100
        {
101 4
            $this->_queryBuilder->orderBy($column->getAttributeName(), $direction);
102
        }
103 4
    }
104
105
    /**
106
     * Sort by this column.
107
     *
108
     * @param string $field
109
     * @param string $direction
110
     *
111
     * @return Sorter
112
     */
113 1
    public function addField(string $field, string $direction = 'asc') : Sorter
114
    {
115 1
        $this->_sortFields[$field] = \mb_strtolower($direction);
116
117 1
        return $this;
118
    }
119
120
    /**
121
     * @return string
122
     */
123 1
    public function render() : string
124
    {
125 1
        return implode(', ', \array_keys($this->_sortFields));
126
    }
127
128 1
    protected function _readFromSession() : void
129
    {
130 1
        $this->_sortFields = (array) SessionHelper::getState($this->_rememberKey, []);
131 1
    }
132
133 1
    protected function _storeInSession() : void
134
    {
135 1
        SessionHelper::saveState($this->_rememberKey, $this->_sortFields);
136 1
    }
137
138 8
    protected function _afterInit() : void
139
    {
140
        /** @var string $sortFields */
141 8
        $sortFields = \request()->get('sort');
142 8
        if (empty($sortFields))
143
        {
144 6
            return;
145
        }
146
147 2
        $this->_initFields($sortFields);
148 2
    }
149
150
    /**
151
     * @param string $fields
152
     */
153 2
    private function _initFields(string $fields) : void
154
    {
155 2
        $this->_sortFields = [];
156 2
        foreach (\explode(self::COLUMN_SEPARATOR, $fields) as $field)
157
        {
158 2
            $sortParts = \explode(self::SORTING_SEPARATOR, $field);
159 2
            if (\count($sortParts) === 1)
160
            {
161 1
                $sortParts[1] = 'asc';
162
            }
163
164 2
            if ($sortParts[1] === 'none')
165
            {
166 1
                SessionHelper::removeState($this->_rememberKey . '.' . $sortParts[0]);
167
            }
168
169 2
            $this->_sortFields[$sortParts[0]] = $sortParts[1];
170
        }
171
    }
172
}