Completed
Push — master ( 184043...459679 )
by WEBEWEB
02:58
created

DataTablesFactory::parseOrders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * This file is part of the jquery-datatables-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\JQuery\DataTablesBundle\Factory;
13
14
use Symfony\Component\HttpFoundation\ParameterBag;
15
use Symfony\Component\HttpFoundation\Request;
16
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesColumnInterface;
17
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesOrder;
18
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesOrderInterface;
19
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesRequest;
20
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesRequestInterface;
21
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesSearch;
22
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesSearchInterface;
23
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesWrapper;
24
use WBW\Bundle\JQuery\DataTablesBundle\Helper\DataTablesRequestHelper;
25
use WBW\Library\Core\Argument\BooleanHelper;
26
use WBW\Library\Core\Network\HTTP\HTTPInterface;
27
28
/**
29
 * DataTables factory.
30
 *
31
 * @author webeweb <https://github.com/webeweb/>
32
 * @package WBW\Bundle\JQuery\DataTablesBundle\Factory
33
 */
34
class DataTablesFactory {
35
36
    /**
37
     * Parse a raw column.
38
     *
39
     * @param array $rawColumn The raw column.
40
     * @param DataTablesWrapper $wrapper The wrapper.
41
     * @return DataTablesColumnInterface Returns the column.
42
     */
43
    public static function parseColumn(array $rawColumn, DataTablesWrapper $wrapper) {
44
45
        // Determines if the raw column is valid.
46
        if (false === array_key_exists(DataTablesColumnInterface::DATATABLES_PARAMETER_DATA, $rawColumn)) {
47
            return null;
48
        }
49
        if (false === array_key_exists(DataTablesColumnInterface::DATATABLES_PARAMETER_NAME, $rawColumn)) {
50
            return null;
51
        }
52
53
        // Check the column.
54
        $dtColumn = $wrapper->getColumn($rawColumn[DataTablesColumnInterface::DATATABLES_PARAMETER_DATA]);
55
        if (null === $dtColumn) {
56
            return null;
57
        }
58
        if ($dtColumn->getName() !== $rawColumn[DataTablesColumnInterface::DATATABLES_PARAMETER_NAME]) {
59
            return null;
60
        }
61
        if (false === $dtColumn->getSearchable()) {
62
            $dtColumn->setSearch(static::parseSearch([])); // Set a default search.
63
            return $dtColumn;
64
        }
65
66
        // Set the search.
67
        $dtColumn->setSearch(static::parseSearch($rawColumn[DataTablesColumnInterface::DATATABLES_PARAMETER_SEARCH]));
68
69
        // Return the column.
70
        return $dtColumn;
71
    }
72
73
    /**
74
     * Parse a raw columns.
75
     *
76
     * @param array $rawColumns The raw columns.
77
     * @param DataTablesWrapper $wrapper The wrapper.
78
     * @return DataTablesColumnInterface[] Returns the columns.
79
     */
80
    public static function parseColumns(array $rawColumns, DataTablesWrapper $wrapper) {
81
82
        // Initialize the columns.
83
        $dtColumns = [];
84
85
        // Handle each column.
86
        foreach ($rawColumns as $current) {
87
88
            // Parse the column.
89
            $dtColumn = static::parseColumn($current, $wrapper);
90
91
            // Check the column.
92
            if (null === $dtColumn) {
93
                continue;
94
            }
95
96
            // Add the column.
97
            $dtColumns[] = $dtColumn;
98
        }
99
100
        // Returns the columns.
101
        return $dtColumns;
102
    }
103
104
    /**
105
     * Parse a raw order.
106
     *
107
     * @param array $rawOrder The raw order.
108
     * @return DataTablesOrderInterface Returns the order.
109
     */
110
    public static function parseOrder(array $rawOrder) {
111
112
        // Initialize an order.
113
        $dtOrder = new DataTablesOrder();
114
115
        // Determines if the raw order is valid.
116
        if (false === array_key_exists(DataTablesOrderInterface::DATATABLES_PARAMETER_COLUMN, $rawOrder)) {
117
            return $dtOrder;
118
        }
119
        if (false === array_key_exists(DataTablesOrderInterface::DATATABLES_PARAMETER_DIR, $rawOrder)) {
120
            return $dtOrder;
121
        }
122
123
        // Set the order.
124
        $dtOrder->setColumn(intval($rawOrder[DataTablesOrderInterface::DATATABLES_PARAMETER_COLUMN]));
125
        $dtOrder->setDir($rawOrder[DataTablesOrderInterface::DATATABLES_PARAMETER_DIR]);
126
127
        // Return the order.
128
        return $dtOrder;
129
    }
130
131
    /**
132
     * Parse raw orders.
133
     *
134
     * @param array $rawOrders The raw orders.
135
     * @return DataTablesOrderInterface[] Returns the orders.
136
     */
137
    public static function parseOrders(array $rawOrders) {
138
139
        // Initialize the orders.
140
        $dtOrders = [];
141
142
        // Handle each raw order.
143
        foreach ($rawOrders as $current) {
144
            $dtOrders[] = static::parseOrder($current);
145
        }
146
147
        // Return the orders.
148
        return $dtOrders;
149
    }
150
151
    /**
152
     * Parse a parameter bag.
153
     *
154
     * @param ParameterBag $request The request.
155
     * @param ParameterBag $bag The bag.
156
     * @return void
157
     */
158
    protected static function parseParameterBag(ParameterBag $request, ParameterBag $bag) {
159
        foreach ($request->keys() as $current) {
160
            if (true === in_array($current, DataTablesRequestHelper::dtParameters())) {
161
                continue;
162
            }
163
            $bag->set($current, $request->get($current));
164
        }
165
    }
166
167
    /**
168
     * Parse a request.
169
     *
170
     * @param DataTablesWrapper $wrapper The wrapper.
171
     * @param Request $request The request.
172
     * @return DataTablesRequestInterface Returns the request.
173
     */
174
    public static function parseRequest(DataTablesWrapper $wrapper, Request $request) {
175
176
        // Initialize a request.
177
        $dtRequest = new DataTablesRequest();
178
179
        // Recopy the parameter bags.
180
        static::parseParameterBag($request->query, $dtRequest->getQuery());
181
        static::parseParameterBag($request->request, $dtRequest->getRequest());
182
183
        // Get the parameter bag.
184
        if (HTTPInterface::HTTP_METHOD_GET === $request->getMethod()) {
185
            $parameterBag = $request->query;
186
        } else {
187
            $parameterBag = $request->request;
188
        }
189
190
        // Get the request parameters.
191
        $columns = null !== $parameterBag->get(DataTablesRequestInterface::DATATABLES_PARAMETER_COLUMNS) ? $parameterBag->get(DataTablesRequestInterface::DATATABLES_PARAMETER_COLUMNS) : [];
192
        $orders  = null !== $parameterBag->get(DataTablesRequestInterface::DATATABLES_PARAMETER_ORDER) ? $parameterBag->get(DataTablesRequestInterface::DATATABLES_PARAMETER_ORDER) : [];
193
        $search  = null !== $parameterBag->get(DataTablesRequestInterface::DATATABLES_PARAMETER_SEARCH) ? $parameterBag->get(DataTablesRequestInterface::DATATABLES_PARAMETER_SEARCH) : [];
194
195
        // Set the request.
196
        $dtRequest->setColumns(static::parseColumns($columns, $wrapper));
0 ignored issues
show
Documentation introduced by
static::parseColumns($columns, $wrapper) is of type array<integer,object<WBW...TablesColumnInterface>>, but the function expects a array<integer,object<WBW...\API\DataTablesColumn>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
197
        $dtRequest->setDraw($parameterBag->getInt(DataTablesRequestInterface::DATATABLES_PARAMETER_DRAW));
198
        $dtRequest->setLength($parameterBag->getInt(DataTablesRequestInterface::DATATABLES_PARAMETER_LENGTH));
199
        $dtRequest->setOrder(static::parseOrders($orders));
200
        $dtRequest->setSearch(static::parseSearch($search));
201
        $dtRequest->setStart($parameterBag->getInt(DataTablesRequestInterface::DATATABLES_PARAMETER_START));
202
        $dtRequest->setWrapper($wrapper);
203
204
        // Return the request.
205
        return $dtRequest;
206
    }
207
208
    /**
209
     * Parse a raw search.
210
     *
211
     * @param array $rawSearch The raw search.
212
     * @return DataTablesSearchInterface Returns the search.
213
     */
214
    public static function parseSearch(array $rawSearch) {
215
216
        // Initialize a search.
217
        $dtSearch = new DataTablesSearch();
218
219
        // Determines if the raw search is valid.
220
        if (false === array_key_exists(DataTablesSearchInterface::DATATABLES_PARAMETER_REGEX, $rawSearch)) {
221
            return $dtSearch;
222
        }
223
        if (false === array_key_exists(DataTablesSearchInterface::DATATABLES_PARAMETER_VALUE, $rawSearch)) {
224
            return $dtSearch;
225
        }
226
227
        // Set the search.
228
        $dtSearch->setRegex(BooleanHelper::parseString($rawSearch[DataTablesSearchInterface::DATATABLES_PARAMETER_REGEX]));
229
        $dtSearch->setValue($rawSearch[DataTablesSearchInterface::DATATABLES_PARAMETER_VALUE]);
230
231
        // Return the search.
232
        return $dtSearch;
233
    }
234
235
}
236