Completed
Push — master ( 895c5c...49cc4e )
by Timo
11:39
created

Sorter::addField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
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 8
    public function __construct(array $fields = null, bool $remember = false)
28
    {
29 8
        $this->_rememberKey = 'sort';
30 8
        $this->_rememberState = $remember;
31
32 8
        if ($fields !== null)
33
        {
34 5
            foreach ($fields as $fieldName => $direction)
35
            {
36 5
                if (\is_int($fieldName))
37
                {
38 2
                    $fieldName = $direction;
39 2
                    $direction = 'asc';
40
                }
41
42 5
                $this->_sortFields[$fieldName] = \mb_strtolower($direction);
43
            }
44
        }
45 8
    }
46
47
    /**
48
     * @return Builder
49
     */
50 7
    public 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 4
            if (\is_int($column))
96
            {
97
                $column = $index;
1 ignored issue
show
Coding Style introduced by
Consider using a different name than the parameter $column. This often makes code more readable.
Loading history...
98
            }
99
100
            /** @var Column $column */
101 4
            return $column->getKey() === $fieldName;
102 4
        });
103
104 4
        if ($column !== null)
105
        {
106 4
            $this->_queryBuilder->orderBy($column->getAttributeName(), $direction);
107
        }
108 4
    }
109
110
    /**
111
     * Sort by this column.
112
     *
113
     * @param string $field
114
     * @param string $direction
115
     *
116
     * @return Sorter
117
     */
118 1
    public function addField(string $field, string $direction = 'asc') : Sorter
119
    {
120 1
        $this->_sortFields[$field] = \mb_strtolower($direction);
121
122 1
        return $this;
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function render() : string
129
    {
130
        return implode(', ', $this->_sortFields);
131
    }
132
133 1
    protected function _readFromSession() : void
134
    {
135 1
        $this->_sortFields = (array) SessionHelper::getState($this->_rememberKey, []);
136 1
    }
137
138 1
    protected function _storeInSession() : void
139
    {
140 1
        SessionHelper::saveState($this->_rememberKey, $this->_sortFields);
141 1
    }
142
143 8
    protected function _afterInit() : void
144
    {
145
        /** @var string $sortFields */
146 8
        $sortFields = \request()->get('sort');
147 8
        if (empty($sortFields))
148
        {
149 6
            return;
150
        }
151
152 2
        $this->_initFields($sortFields);
153 2
    }
154
155
    /**
156
     * @param string $fields
157
     */
158 2
    private function _initFields(string $fields) : void
159
    {
160 2
        $this->_sortFields = [];
161 2
        foreach (\explode(self::COLUMN_SEPARATOR, $fields) as $field)
162
        {
163 2
            $sortParts = \explode(self::SORTING_SEPARATOR, $field);
164 2
            if (\count($sortParts) === 1)
165
            {
166 1
                $sortParts[1] = 'asc';
167
            }
168
169 2
            if ($sortParts[1] === 'none')
170
            {
171 1
                SessionHelper::removeState($this->_rememberKey . '.' . $sortParts[0]);
172
            }
173
174 2
            $this->_sortFields[$sortParts[0]] = $sortParts[1];
175
        }
176
    }
177
}