Completed
Push — master ( cc2b3d...e0d8bb )
by WEBEWEB
01:34
created

DataTablesOrder::parse()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
cc 4
nc 4
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\API;
13
14
/**
15
 * DataTables order.
16
 *
17
 * @author webeweb <https://github.com/webeweb/>
18
 * @package WBW\Bundle\JQuery\DataTablesBundle\API
19
 */
20
class DataTablesOrder {
21
22
    /**
23
     * Column.
24
     *
25
     * @var integer
26
     */
27
    private $column;
28
29
    /**
30
     * Dir.
31
     *
32
     * @var string
33
     */
34
    private $dir;
35
36
    /**
37
     * Constructor.
38
     */
39
    protected function __construct() {
40
        // NOTHING TO DO
41
    }
42
43
    /**
44
     * Get the column.
45
     *
46
     * @return integer Returns the column.
47
     */
48
    public function getColumn() {
49
        return $this->column;
50
    }
51
52
    /**
53
     * Get the dir.
54
     *
55
     * @return string Returns the dir.
56
     */
57
    public function getDir() {
58
        return $this->dir;
59
    }
60
61
    /**
62
     * Parse a raw orders.
63
     *
64
     * @param array $rawOrders The raw orders.
65
     * @return DataTablesOrder[] Returns the DataTables orders.
66
     */
67
    public static function parse(array $rawOrders) {
68
69
        // Initialize the DataTables orders.
70
        $dtOrders = [];
71
72
        // Handle each raw order.
73
        foreach ($rawOrders as $current) {
74
75
            // Determines if the raw order is valid.
76
            if (false === array_key_exists("column", $current)) {
77
                continue;
78
            }
79
            if (false === array_key_exists("dir", $current)) {
80
                continue;
81
            }
82
83
            // Create a DataTables order.
84
            $dtOrder = new DataTablesOrder();
85
            $dtOrder->setColumn(intval($current["column"]));
86
            $dtOrder->setDir($current["dir"]);
87
88
            // Add the DataTables order.
89
            $dtOrders[] = $dtOrder;
90
        }
91
92
        // Return the DataTables orders.
93
        return $dtOrders;
94
    }
95
96
    /**
97
     * Set the column.
98
     *
99
     * @param integer $column The column.
100
     * @return DataTablesOrder Returns this DataTables order.
101
     */
102
    protected function setColumn($column) {
103
        $this->column = (0 <= $column ? $column : null);
104
        return $this;
105
    }
106
107
    /**
108
     * Set the dir.
109
     *
110
     * @param string $dir The dir.
111
     * @return DataTablesOrder Returns this DataTables order.
112
     */
113
    protected function setDir($dir) {
114
        $this->dir = strtoupper(true === in_array($dir, ["asc", "desc"]) ? $dir : "asc");
115
        return $this;
116
    }
117
118
}
119