DataTableResults::jsonSerialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 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
use Symfony\Component\Validator\Constraints as Assert;
17
18
/**
19
 * Data to return to DataTables plugin.
20
 *
21
 * @see https://www.datatables.net/manual/server-side
22
 *
23
 * @property int   $recordsTotal    Total records, before filtering
24
 * @property int   $recordsFiltered Total records, after filtering
25
 * @property array $data            The data to be displayed in the table
26
 */
27
class DataTableResults implements \JsonSerializable
28
{
29
    public const DT_ROW_ID    = 'DT_RowId';
30
    public const DT_ROW_CLASS = 'DT_RowClass';
31
    public const DT_ROW_DATA  = 'DT_RowData';
32
    public const DT_ROW_ATTR  = 'DT_RowAttr';
33
34
    /**
35
     * @Assert\NotNull
36
     * @Assert\GreaterThanOrEqual(value="0")
37
     */
38
    public $recordsTotal = 0;
39
40
    /**
41
     * @Assert\NotNull
42
     * @Assert\GreaterThanOrEqual(value="0")
43
     */
44
    public $recordsFiltered = 0;
45
46
    /**
47
     * @Assert\NotNull
48
     * @Assert\Type(type="array")
49
     */
50
    public $data = [];
51
52
    /**
53
     * @Assert\NotNull
54
     * @Assert\GreaterThanOrEqual(value="0")
55
     */
56
    private $draw = 0;
57
58
    /**
59
     * Convert results into array as expected by DataTables plugin.
60
     */
61
    public function jsonSerialize(): array
62
    {
63 1
        return [
64
            'draw'            => (int) $this->draw,
65
            'recordsTotal'    => (int) $this->recordsTotal,
66 1
            'recordsFiltered' => (int) $this->recordsFiltered,
67 1
            'data'            => $this->data,
68 1
        ];
69 1
    }
70
}
71