Completed
Push — master ( 207a43...23e505 )
by WEBEWEB
01:31
created

DataTablesMappingHelper::getComparator()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.0777
c 0
b 0
f 0
cc 6
nc 6
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\Helper;
13
14
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesColumnInterface;
15
use WBW\Bundle\JQuery\DataTablesBundle\API\DataTablesMappingInterface;
16
17
/**
18
 * DataTables mapping helper.
19
 *
20
 * @author webeweb <https://github.com/webeweb/>
21
 * @package WBW\Bundle\JQuery\DataTablesBundle\Helper
22
 */
23
class DataTablesMappingHelper {
24
25
    /**
26
     * Get an alias.
27
     *
28
     * @param DataTablesMappingInterface $mapping The mapping.
29
     * @return string Returns the alias.
30
     */
31
    public static function getAlias(DataTablesMappingInterface $mapping) {
32
33
        if (null === $mapping->getColumn()) {
34
            return null;
35
        }
36
37
        if (null === $mapping->getPrefix()) {
38
            return $mapping->getColumn();
39
        }
40
41
        return implode(".", [$mapping->getPrefix(), $mapping->getColumn()]);
42
    }
43
44
    /**
45
     * Get a comparator.
46
     *
47
     * @param DataTablesMappingInterface $mapping The mapping.
48
     * @return string Returns the comparator.
49
     */
50
    public static function getComparator(DataTablesMappingInterface $mapping) {
51
52
        if (null === $mapping->getParent()) {
53
            return "LIKE";
54
        }
55
56
        switch ($mapping->getParent()->getType()) {
57
58
            case DataTablesColumnInterface::DATATABLES_TYPE_DATE:
59
            case DataTablesColumnInterface::DATATABLES_TYPE_HTML_NUM:
60
            case DataTablesColumnInterface::DATATABLES_TYPE_NUM:
61
            case DataTablesColumnInterface::DATATABLES_TYPE_NUM_FMT:
62
                return "=";
63
        }
64
65
        return "LIKE";
66
    }
67
68
    /**
69
     * Get a param.
70
     *
71
     * @param DataTablesMappingInterface $mapping The mapping.
72
     * @return string Returns the param.
73
     */
74
    public static function getParam(DataTablesMappingInterface $mapping) {
75
76
        $param = [
77
            ":",
78
            $mapping->getPrefix(),
79
            $mapping->getColumn(),
80
        ];
81
82
        return implode("", $param);
83
    }
84
85
    /**
86
     * Get a where.
87
     *
88
     * @param DataTablesMappingInterface $mapping The mapping.
89
     * @return string Returns the where.
90
     */
91
    public static function getWhere(DataTablesMappingInterface $mapping) {
92
93
        $where = [
94
            static::getAlias($mapping),
95
            static::getComparator($mapping),
96
            static::getParam($mapping),
97
        ];
98
99
        return implode(" ", $where);
100
    }
101
}
102