Completed
Push — master ( 65fba0...ebf204 )
by WEBEWEB
01:36
created

DataTablesFactory::parseOrders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
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\Factory;
13
14
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesOrder;
15
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesOrderInterface;
16
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesSearch;
17
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesSearchInterface;
18
use WBW\Library\Core\Argument\BooleanHelper;
19
20
/**
21
 * DataTables factory.
22
 *
23
 * @author webeweb <https://github.com/webeweb/>
24
 * @package WBW\Bundle\JQuery\DataTablesBundle\Factory
25
 */
26
class DataTablesFactory {
27
28
    /**
29
     * Parse a raw order.
30
     *
31
     * @param array $rawOrder The raw order.
32
     * @return DataTablesOrderInterface Returns the order.
33
     */
34
    public static function parseOrder(array $rawOrder) {
35
36
        // Initialize an order.
37
        $dtOrder = new DataTablesOrder();
38
39
        // Determines if the raw order is valid.
40
        if (false === array_key_exists(DataTablesOrderInterface::DATATABLES_PARAMETER_COLUMN, $rawOrder)) {
41
            return $dtOrder;
42
        }
43
        if (false === array_key_exists(DataTablesOrderInterface::DATATABLES_PARAMETER_DIR, $rawOrder)) {
44
            return $dtOrder;
45
        }
46
47
        // Set the order.
48
        $dtOrder->setColumn(intval($rawOrder[DataTablesOrderInterface::DATATABLES_PARAMETER_COLUMN]));
49
        $dtOrder->setDir($rawOrder[DataTablesOrderInterface::DATATABLES_PARAMETER_DIR]);
50
51
        // Return the order.
52
        return $dtOrder;
53
    }
54
55
    /**
56
     * Parse raw orders.
57
     *
58
     * @param array $rawOrders The raw orders.
59
     * @return DataTablesOrderInterface[] Returns the orders.
60
     */
61
    public static function parseOrders(array $rawOrders) {
62
63
        // Initialize the orders.
64
        $dtOrders = [];
65
66
        // Handle each raw order.
67
        foreach ($rawOrders as $current) {
68
            $dtOrders[] = static::parseOrder($current);
69
        }
70
71
        // Return the orders.
72
        return $dtOrders;
73
    }
74
75
    /**
76
     * Parse a raw search.
77
     *
78
     * @param array $rawSearch The raw search.
79
     * @return DataTablesSearchInterface Returns the search.
80
     */
81
    public static function parseSearch(array $rawSearch) {
82
83
        // Initialize a search.
84
        $dtSearch = new DataTablesSearch();
85
86
        // Determines if the raw search is valid.
87
        if (false === array_key_exists(DataTablesSearchInterface::DATATABLES_PARAMETER_REGEX, $rawSearch)) {
88
            return $dtSearch;
89
        }
90
        if (false === array_key_exists(DataTablesSearchInterface::DATATABLES_PARAMETER_VALUE, $rawSearch)) {
91
            return $dtSearch;
92
        }
93
94
        // Set the search.
95
        $dtSearch->setRegex(BooleanHelper::parseString($rawSearch[DataTablesSearchInterface::DATATABLES_PARAMETER_REGEX]));
96
        $dtSearch->setValue($rawSearch[DataTablesSearchInterface::DATATABLES_PARAMETER_VALUE]);
97
98
        // Return the search.
99
        return $dtSearch;
100
    }
101
102
}
103