Completed
Branch master (a2d832)
by Timo
02:40
created

Sorter::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace hamburgscleanest\DataTables\Models\DataComponents;
4
5
use hamburgscleanest\DataTables\Helpers\SessionHelper;
6
use hamburgscleanest\DataTables\Models\DataComponent;
7
use Illuminate\Database\Eloquent\Builder;
8
use function implode;
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 array $fields
0 ignored issues
show
Documentation introduced by
Should the type for parameter $fields not be null|array?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
25
     * @param bool $remember
26
     */
27 2
    public function __construct(array $fields = null, bool $remember = false)
28
    {
29 2
        $this->_rememberKey = 'sort';
30 2
        $this->_rememberState = $remember;
31
32 2
        if ($fields !== null)
33
        {
34 1
            foreach ($fields as $fieldName => $direction)
35
            {
36 1
                if ($fieldName === 0)
37
                {
38
                    $fieldName = $direction;
39
                    $direction = 'asc';
40
                }
41
42 1
                $this->_sortFields[$fieldName] = \mb_strtolower($direction);
43
            }
44
        }
45 2
    }
46
47
    /**
48
     * @param string $fields
49
     */
50
    private function _initFields(string $fields)
51
    {
52
        $this->_sortFields = [];
53
        foreach (\explode(self::COLUMN_SEPARATOR, $fields) as $field)
54
        {
55
            $sortParts = \explode(self::SORTING_SEPARATOR, $field);
56
            if (\count($sortParts) === 1)
57
            {
58
                $sortParts[1] = 'asc';
59
            }
60
61
            if ($sortParts[1] === 'none')
62
            {
63
                SessionHelper::removeState($this->_request, $this->_rememberKey . '.' . $sortParts[0]);
64
            }
65
66
            $this->_sortFields[$sortParts[0]] = $sortParts[1];
67
        }
68
    }
69
70
    protected function _readFromSession()
71
    {
72
        $this->_sortFields = SessionHelper::getState($this->_request, $this->_rememberKey, []);
0 ignored issues
show
Documentation Bug introduced by
It seems like \hamburgscleanest\DataTa...>_rememberKey, array()) of type * is incompatible with the declared type array of property $_sortFields.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
73
    }
74
75
    protected function _storeInSession()
76
    {
77
        SessionHelper::saveState($this->_request, $this->_rememberKey, $this->_sortFields);
78
    }
79
80 2
    protected function _afterInit()
81
    {
82
        /** @var string $sortFields */
83 2
        $sortFields = $this->_request->get('sort');
84 2
        if (empty($sortFields))
85
        {
86 2
            return;
87
        }
88
89
        $this->_initFields($sortFields);
90
    }
91
92
    /**
93
     * @return Builder
94
     */
95 2
    public function shapeData(): Builder
96
    {
97 2
        if (\count($this->_sortFields) > 0)
98
        {
99 2
            foreach ($this->_sortFields as $fieldName => $direction)
100
            {
101 2
                if ($direction === 'none')
102
                {
103
                    $this->removeField($fieldName);
104
                    continue;
105
                }
106
107 2
                $this->_queryBuilder->orderBy($fieldName, $direction);
108
            }
109
        }
110
111 2
        return $this->_queryBuilder;
112
    }
113
114
    /**
115
     * Sort by this column.
116
     *
117
     * @param string $field
118
     * @param string $direction
119
     *
120
     * @return $this
121
     */
122 1
    public function addField(string $field, string $direction = 'asc')
123
    {
124 1
        $this->_sortFields[$field] = \mb_strtolower($direction);
125
126 1
        return $this;
127
    }
128
129
    /**
130
     * Stop sorting by this column
131
     *
132
     * @param string $field
133
     *
134
     * @return $this
135
     */
136
    public function removeField(string $field)
137
    {
138
        if (isset($this->_sortFields[$field]))
139
        {
140
            unset($this->_sortFields[$field]);
141
        }
142
143
        return $this;
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public function render(): string
150
    {
151
        return implode(', ', $this->_sortFields);
152
    }
153
}