Passed
Push — master ( a7f0ec...c2460d )
by Artem
02:49
created

DataTableQuery::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 28
ccs 20
cts 20
cp 1
rs 9.6333
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
//----------------------------------------------------------------------
4
//
5
//  Copyright (C) 2015-2019 Artem Rodygin
6
//
7
//  This file is part of DataTables Symfony bundle.
8
//
9
//  You should have received a copy of the MIT License along with
10
//  the bundle. If not, see <http://opensource.org/licenses/MIT>.
11
//
12
//----------------------------------------------------------------------
13
14
namespace DataTables;
15
16
/**
17
 * A draw query from DataTables plugin.
18
 *
19
 * @see https://www.datatables.net/manual/server-side
20
 *
21
 * @property int      $start      Index of first row to return, zero-based.
22
 * @property int      $length     Total number of rows to return (-1 to return all rows).
23
 * @property Search   $search     Global search value.
24
 * @property Order[]  $order      Columns ordering (zero-based column index and direction).
25
 * @property Column[] $columns    Columns information (searchable, orderable, search value, etc).
26
 * @property array    $customData Custom data from DataTables.
27
 */
28
class DataTableQuery extends ValueObject implements \JsonSerializable
29
{
30
    protected $start;
31
    protected $length;
32
    protected $search;
33
    protected $order;
34
    protected $columns;
35
    protected $customData;
36
37
    /**
38
     * Initializing constructor.
39
     *
40
     * @param Parameters $params
41
     */
42 1
    public function __construct(Parameters $params)
43
    {
44 1
        $this->start  = (int) $params->start;
45 1
        $this->length = (int) $params->length;
46
47 1
        $this->search = new Search(
48 1
            $params->search['value'],
49 1
            $params->search['regex'] === 'true'
50
        );
51
52 1
        $this->order = array_map(function (array $order) {
53 1
            return new Order(
54 1
                (int) $order['column'],
55 1
                $order['dir']
56
            );
57 1
        }, $params->order);
58
59 1
        $this->columns = array_map(function (array $column) {
60 1
            return new Column(
61 1
                $column['data'],
62 1
                $column['name'],
63 1
                $column['searchable'] === 'true',
64 1
                $column['orderable'] === 'true',
65 1
                new Search($column['search']['value'], $column['search']['regex'] === 'true')
66
            );
67 1
        }, $params->columns);
68
69 1
        $this->customData = $params->customData;
70 1
    }
71
72
    /**
73
     * {@inheritdoc}
74
     *
75
     * @return array
76
     */
77
    public function jsonSerialize()
78
    {
79 1
        $callback = function (\JsonSerializable $item) {
80 1
            return $item->jsonSerialize();
81 1
        };
82
83
        return [
84
            'start'   => $this->start,
85
            'length'  => $this->length,
86
            'search'  => $this->search->jsonSerialize(),
87
            'order'   => array_map($callback, $this->order),
88
            'columns' => array_map($callback, $this->columns),
89
        ];
90
    }
91
}
92