Completed
Push — master ( 37f225...776e9c )
by Artem
04:27
created

DataTableQuery::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
crap 1
1
<?php
2
3
//----------------------------------------------------------------------
4
//
5
//  Copyright (C) 2015-2016 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-read   int      $start   Index of first row to return, zero-based.
22
 * @property-read   int      $length  Total number of rows to return (-1 to return all rows).
23
 * @property-read   Search   $search  Global search value.
24
 * @property-read   Order[]  $order   Columns ordering (zero-based column index and direction).
25
 * @property-read   Column[] $columns Columns information (searchable, orderable, search value, etc).
26
 */
27
class DataTableQuery extends ValueObject implements \JsonSerializable
28
{
29
    protected $start;
30
    protected $length;
31
    protected $search;
32
    protected $order;
33
    protected $columns;
34
35
    /**
36
     * Initializing constructor.
37
     *
38
     * @param   Parameters $params
39
     */
40 5
    public function __construct(Parameters $params)
41
    {
42 5
        $this->start  = (int) $params->start;
43 5
        $this->length = (int) $params->length;
44
45 5
        $this->search = new Search(
46 5
            $params->search['value'],
47 5
            (bool) $params->search['regex']
48
        );
49
50
        $this->order = array_map(function (array $order) {
51 2
            return new Order(
52 2
                (int) $order['column'],
53 2
                $order['dir']
54
            );
55 5
        }, $params->order);
56
57
        $this->columns = array_map(function (array $column) {
58 2
            return new Column(
59 2
                $column['data'],
60 2
                $column['name'],
61 2
                (bool) $column['searchable'],
62 2
                (bool) $column['orderable'],
63 2
                new Search($column['search']['value'], (bool) $column['search']['regex'])
64
            );
65 5
        }, $params->columns);
66 5
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function jsonSerialize()
72
    {
73 1
        $callback = function (\JsonSerializable $item) {
74 1
            return $item->jsonSerialize();
75 1
        };
76
77
        return [
78 1
            'start'   => $this->start,
79 1
            'length'  => $this->length,
80 1
            'search'  => $this->search->jsonSerialize(),
81 1
            'order'   => array_map($callback, $this->order),
82 1
            'columns' => array_map($callback, $this->columns),
83
        ];
84
    }
85
}
86