Completed
Push — master ( a91069...e573fc )
by WEBEWEB
01:35
created

DataTablesOrder::setDir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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