Completed
Push — master ( 50b857...e52a90 )
by Timo
07:53
created

src/Models/DataComponents/Sorter.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
use Illuminate\Support\Facades\DB;
10
11
/**
12
 * Class Sorter
13
 * @package hamburgscleanest\DataTables\Models\DataComponents
14
 */
15
class Sorter extends DataComponent {
16
17
    const SORTING_SEPARATOR = '~';
18
    const COLUMN_SEPARATOR  = '.';
19
20
    /** @var array */
21
    private $_sortFields = [];
22
23
    /**
24
     * Sorter constructor.
25
     * @param null|array $fields
26
     * @param bool $remember
27
     */
28 10
    public function __construct(array $fields = null, bool $remember = false)
29
    {
30 10
        $this->_rememberKey = 'sort';
31 10
        $this->_rememberState = $remember;
32
33 10
        if ($fields !== null)
34
        {
35 7
            foreach ($fields as $fieldName => $direction)
36
            {
37 7
                if (\is_int($fieldName))
38
                {
39 3
                    $fieldName = $direction;
40 3
                    $direction = 'asc';
41
                }
42
43 7
                $this->_sortFields[$fieldName] = \mb_strtolower($direction);
44
            }
45
        }
46 10
    }
47
48
    /**
49
     * Sort by this column.
50
     *
51
     * @param string $field
52
     * @param string $direction
53
     *
54
     * @return Sorter
55
     */
56 1
    public function addField(string $field, string $direction = 'asc') : Sorter
57
    {
58 1
        $this->_sortFields[$field] = \mb_strtolower($direction);
59
60 1
        return $this;
61
    }
62
63
    /**
64
     * @return string
65
     */
66 1
    public function render() : string
67
    {
68 1
        return implode(', ', \array_keys($this->_sortFields));
69
    }
70
71
    /**
72
     * @return Builder
73
     */
74 8
    protected function _shapeData() : Builder
75
    {
76 8
        if (\count($this->_sortFields) > 0)
77
        {
78 6
            foreach ($this->_sortFields as $fieldName => $direction)
79
            {
80 6
                if ($direction === 'none')
81
                {
82 1
                    $this->removeField($fieldName);
83 1
                    continue;
84
                }
85
86 5
                $this->_sortField($fieldName, $direction);
87
            }
88
        }
89
90 8
        return $this->_dataTable->query();
91
    }
92
93
    /**
94
     * Stop sorting by this column
95
     *
96
     * @param string $field
97
     *
98
     * @return Sorter
99
     */
100 1
    public function removeField(string $field) : Sorter
101
    {
102 1
        if (isset($this->_sortFields[$field]))
103
        {
104 1
            unset($this->_sortFields[$field]);
105
        }
106
107 1
        return $this;
108
    }
109
110
    /**
111
     * @param string $fieldName
112
     * @param string $direction
113
     */
114
    private function _sortField(string $fieldName, string $direction) : void
115
    {
116
        /** @var Column $column */
117 5
        $column = \array_first($this->_dataTable->getColumns(), function($column) use ($fieldName) {
118
            /** @var Column $column */
119 5
            return $column->getKey() === $fieldName;
120 5
        });
121
122 5
        if ($column !== null)
123
        {
124 5
            $this->_dataTable->query()->orderBy(DB::raw($column->getAttributeName()), $direction);
1 ignored issue
show
The method orderBy() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean enforceOrderBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
125
        }
126 5
    }
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 9
    protected function _afterInit() : void
139
    {
140
        /** @var string $sortFields */
141 9
        $sortFields = \request()->get('sort');
142 9
        if (empty($sortFields))
143
        {
144 7
            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
}