Completed
Push — master ( a7ed84...65cc57 )
by WEBEWEB
02:22
created

DataTablesOrder::dtDirs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 implements DataTablesOrderInterface {
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
     * DataTables dirs.
45
     *
46
     * @return array Returns the dirs.
47
     */
48
    public static function dtDirs() {
49
        return [
50
            self::DATATABLES_DIR_ASC,
51
            self::DATATABLES_DIR_DESC,
52
        ];
53
    }
54
55
    /**
56
     * Get the column.
57
     *
58
     * @return integer Returns the column.
59
     */
60
    public function getColumn() {
61
        return $this->column;
62
    }
63
64
    /**
65
     * Get the dir.
66
     *
67
     * @return string Returns the dir.
68
     */
69
    public function getDir() {
70
        return $this->dir;
71
    }
72
73
    /**
74
     * Parse a raw orders.
75
     *
76
     * @param array $rawOrders The raw orders.
77
     * @return DataTablesOrder[] Returns the orders.
78
     */
79
    public static function parse(array $rawOrders) {
80
81
        // Initialize the orders.
82
        $dtOrders = [];
83
84
        // Handle each raw order.
85
        foreach ($rawOrders as $current) {
86
87
            // Determines if the raw order is valid.
88
            if (false === array_key_exists(self::DATATABLES_PARAMETER_COLUMN, $current)) {
89
                continue;
90
            }
91
            if (false === array_key_exists(self::DATATABLES_PARAMETER_DIR, $current)) {
92
                continue;
93
            }
94
95
            // Create a order.
96
            $dtOrder = new DataTablesOrder();
97
            $dtOrder->setColumn(intval($current[self::DATATABLES_PARAMETER_COLUMN]));
98
            $dtOrder->setDir($current[self::DATATABLES_PARAMETER_DIR]);
99
100
            // Add the order.
101
            $dtOrders[] = $dtOrder;
102
        }
103
104
        // Return the orders.
105
        return $dtOrders;
106
    }
107
108
    /**
109
     * Set the column.
110
     *
111
     * @param integer $column The column.
112
     * @return DataTablesOrder Returns this order.
113
     */
114
    protected function setColumn($column) {
115
        $this->column = (0 <= $column ? $column : null);
116
        return $this;
117
    }
118
119
    /**
120
     * Set the dir.
121
     *
122
     * @param string $dir The dir.
123
     * @return DataTablesOrder Returns this order.
124
     */
125
    protected function setDir($dir) {
126
        if (false === in_array($dir, self::dtDirs())) {
127
            $dir = self::DATATABLES_DIR_ASC;
128
        }
129
        $this->dir = strtoupper($dir);
130
        return $this;
131
    }
132
133
}
134