DataTableQuery   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 2
eloc 34
c 2
b 0
f 0
dl 0
loc 57
ccs 24
cts 24
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 28 1
A jsonSerialize() 0 12 1
1
<?php
2
3
//----------------------------------------------------------------------
4
//
5
//  Copyright (C) 2015-2022 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
    public function __construct(Parameters $params)
41
    {
42 1
        $this->start  = (int) $params->start;
43
        $this->length = (int) $params->length;
44 1
45 1
        $this->search = new Search(
46
            $params->search['value'],
47 1
            'true' === $params->search['regex']
48 1
        );
49 1
50
        $this->order = array_map(function (array $order): Order {
51
            return new Order(
52 1
                (int) $order['column'],
53 1
                $order['dir']
54 1
            );
55 1
        }, $params->order);
56
57 1
        $this->columns = array_map(function (array $column): Column {
58
            return new Column(
59 1
                $column['data'],
60 1
                $column['name'],
61 1
                'true'  === $column['searchable'],
62 1
                'true'  === $column['orderable'],
63 1
                new Search($column['search']['value'], 'true' === $column['search']['regex'])
64 1
            );
65 1
        }, $params->columns);
66
67 1
        $this->customData = $params->customData;
68
    }
69 1
70 1
    /**
71
     * {@inheritdoc}
72
     */
73
    public function jsonSerialize(): array
74
    {
75
        $callback = function (\JsonSerializable $item): array {
76
            return $item->jsonSerialize();
77
        };
78
79 1
        return [
80 1
            'start'   => $this->start,
81 1
            'length'  => $this->length,
82
            'search'  => $this->search->jsonSerialize(),
83
            'order'   => array_map($callback, $this->order),
84
            'columns' => array_map($callback, $this->columns),
85
        ];
86
    }
87
}
88