Completed
Push — master ( d49c92...18b88e )
by Tim
02:28
created

Datatable19QueryParser::addColumnForOrdering()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 4
1
<?php
2
3
namespace OpenSkill\Datatable\Queries\Parser;
4
5
6
use OpenSkill\Datatable\Columns\ColumnConfiguration;
7
use OpenSkill\Datatable\DatatableException;
8
use OpenSkill\Datatable\Queries\QueryConfiguration;
9
use OpenSkill\Datatable\Queries\QueryConfigurationBuilder;
10
use Symfony\Component\HttpFoundation\JsonResponse;
11
use Symfony\Component\HttpFoundation\ParameterBag;
12
use Symfony\Component\HttpFoundation\Request;
13
14
class Datatable19QueryParser extends QueryParser
15
{
16
17
    /**
18
     * Method to determine if this parser can handle the query parameters. If so then the parser should return true
19
     * and be able to return a DTQueryConfiguration
20
     *
21
     * @param Request $request The current request, that should be investigated
22
     * @return bool true if the parser is able to parse the query parameters and to return a DTQueryConfiguration
23
     */
24
    public function canParse(Request $request)
25
    {
26
        return $request->query->has("sEcho");
27
    }
28
29
    /**
30
     * Method that should parse the request and return a DTQueryConfiguration
31
     *
32
     * @param Request $request The current request that should be investigated
33
     * @param ColumnConfiguration[] $columnConfiguration The configuration of the columns
34
     * @return QueryConfiguration the configuration the provider can use to prepare the data
35
     */
36 View Code Duplication
    public function parse(Request $request, array $columnConfiguration)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
    {
38
        $query = $request->query;
39
        $builder = QueryConfigurationBuilder::create();
40
41
        $this->getDrawCall($query, $builder);
42
43
        $this->getStart($query, $builder);
44
45
        $this->getLength($query, $builder);
46
47
        $this->getSearch($query, $builder);
48
49
        $this->determineSortableColumns($query, $builder, $columnConfiguration);
50
51
        $this->getRegex($query, $builder);
52
53
        $this->getSearchColumns($query, $builder, $columnConfiguration);
54
55
        return $builder->build();
56
    }
57
58
    /**
59
     * Helper function that will check if a variable is empty
60
     * @param mixed $string
61
     * @return bool true if empty, false otherwise
62
     */
63
    private function isEmpty($string)
64
    {
65
        return empty($string);
66
    }
67
68
    /**
69
     * Helper function that will check if a variable has a value
70
     *
71
     * NOTE: (this is almost the opposite of isEmpty, but it is *not* the same)
72
     *
73
     * @param mixed $string
74
     * @return bool true if empty, false otherwise
75
     */
76
    private function hasValue($string)
77
    {
78
        return isset($string) && (strlen($string) > 0);
79
    }
80
81
    /**
82
     * @param ParameterBag $query
83
     * @param QueryConfigurationBuilder $builder
84
     */
85
    public function getDrawCall($query, $builder)
86
    {
87
        if ($query->has('sEcho')) {
88
            $builder->drawCall($query->get('sEcho'));
89
        }
90
    }
91
92
    /**
93
     * @param ParameterBag $query
94
     * @param QueryConfigurationBuilder $builder
95
     */
96
    public function getStart($query, $builder)
97
    {
98
        if ($query->has('iDisplayStart')) {
99
            $builder->start($query->get('iDisplayStart'));
100
        }
101
    }
102
103
    /**
104
     * @param ParameterBag $query
105
     * @param QueryConfigurationBuilder $builder
106
     */
107
    public function getLength($query, $builder)
108
    {
109
        if ($query->has('iDisplayLength')) {
110
            $builder->length($query->get('iDisplayLength'));
111
        }
112
    }
113
114
    /**
115
     * @param ParameterBag $query
116
     * @param QueryConfigurationBuilder $builder
117
     */
118
    public function getSearch($query, $builder)
119
    {
120
        if ($query->has('sSearch') && !$this->isEmpty($query->get('sSearch'))) {
121
            $builder->searchValue($query->get('sSearch'));
122
        }
123
    }
124
125
    /**
126
     * @param ParameterBag $query
127
     * @param QueryConfigurationBuilder $builder
128
     * @param ColumnConfiguration[] $columnConfiguration
129
     */
130
    public function getSearchColumns($query, $builder, array $columnConfiguration)
131
    {
132
        // for each column we need to see if there is a search value
133
        foreach ($columnConfiguration as $i => $c) {
134
            // check if there is something search related
135
            if ($c->getSearch()->isSearchable() && $query->has("sSearch_" . $i) && !$this->isEmpty($query->get("sSearch_" . $i))) {
136
                // search for this column is available
137
                $builder->columnSearch($c->getName(), $query->get("sSearch_" . $i));
138
            }
139
        }
140
    }
141
142
    /**
143
     * @param ParameterBag $query
144
     * @param QueryConfigurationBuilder $builder
145
     * @param ColumnConfiguration[] $columnConfiguration
146
     * @throws DatatableException when a column for sorting is out of bounds
147
     * @return bool success?
148
     */
149
    private function determineSortableColumns($query, $builder, array $columnConfiguration)
150
    {
151
        $columns = $this->getNumberOfSortingColumns($query);
152
153
        for ($i = 0; $i < $columns; $i++) {
154
            if ($query->has("iSortCol_" . $i) && $this->hasValue($query->get("iSortCol_" . $i))) {
155
                $item = $query->get("iSortCol_" . $i);
156
                $direction = $query->get("sSortDir_" . $i);
157
158
                $this->addColumnForOrdering($builder, $columnConfiguration, $item, $direction);
159
            }
160
        }
161
162
        return true;
163
    }
164
165
    /**
166
     * Find out how many columns we are sorting by for the sorting loop
167
     * @see determineSortableColumns
168
     * @param ParameterBag $query
169
     * @return int
170
     */
171
    private function getNumberOfSortingColumns(ParameterBag $query)
172
    {
173
        if (!$query->has('iSortingCols'))
174
            return 0;
175
176
        return intval($query->get('iSortingCols'));
177
    }
178
179
    /**
180
     * Add a column for ordering to the QueryConfigurationBuilder
181
     * @see determineSortableColumns
182
     * @param $builder
183
     * @param $columnConfiguration
184
     * @param $item
185
     * @param $direction
186
     * @throws DatatableException
187
     */
188
    private function addColumnForOrdering($builder, $columnConfiguration, $item, $direction)
189
    {
190
        $c = $this->getColumnFromConfiguration($columnConfiguration, $item);
191
192
        if ($c->getOrder()->isOrderable()) {
193
            $builder->columnOrder($c->getName(), $direction);
194
        }
195
    }
196
197
    /**
198
     * @param ColumnConfiguration[] $columnConfiguration
199
     * @param int $item
200
     * @return ColumnConfiguration a specific item from $ColumnConfiguration
201
     * @throws DatatableException
202
     */
203
    private function getColumnFromConfiguration(array $columnConfiguration, $item)
204
    {
205
        $columnPosition = intval($item);
206
207
        if (!isset($columnConfiguration[$columnPosition])) {
208
            throw new DatatableException('The column requested for ordering does not exist');
209
        }
210
211
        return $columnConfiguration[$columnPosition];
212
    }
213
214
    /**
215
     * @param ParameterBag $query
216
     * @param QueryConfigurationBuilder $builder
217
     */
218
    public function getRegex($query, $builder)
219
    {
220
        if ($query->has('bRegex')) {
221
            $builder->searchRegex($query->get('bRegex'));
222
        }
223
    }
224
}
225