DataTablesMappingHelper::getComparator()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 16
rs 9.2222
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|null Returns the alias.
30
     */
31
    public static function getAlias(DataTablesMappingInterface $mapping): ?string {
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(".", [
42
            $mapping->getPrefix(),
43
            $mapping->getColumn(),
44
        ]);
45
    }
46
47
    /**
48
     * Get a comparator.
49
     *
50
     * @param DataTablesMappingInterface $mapping The mapping.
51
     * @return string Returns the comparator.
52
     */
53
    public static function getComparator(DataTablesMappingInterface $mapping): string {
54
55
        if (null === $mapping->getParent()) {
56
            return "LIKE";
57
        }
58
59
        switch ($mapping->getParent()->getType()) {
60
61
            case DataTablesColumnInterface::DATATABLES_TYPE_DATE:
62
            case DataTablesColumnInterface::DATATABLES_TYPE_HTML_NUM:
63
            case DataTablesColumnInterface::DATATABLES_TYPE_NUM:
64
            case DataTablesColumnInterface::DATATABLES_TYPE_NUM_FMT:
65
                return "=";
66
        }
67
68
        return "LIKE";
69
    }
70
71
    /**
72
     * Get a param.
73
     *
74
     * @param DataTablesMappingInterface $mapping The mapping.
75
     * @return string Returns the param.
76
     */
77
    public static function getParam(DataTablesMappingInterface $mapping): string {
78
79
        return implode("", [
80
            ":",
81
            $mapping->getPrefix(),
82
            $mapping->getColumn(),
83
        ]);
84
    }
85
86
    /**
87
     * Get a where.
88
     *
89
     * @param DataTablesMappingInterface $mapping The mapping.
90
     * @return string Returns the where.
91
     */
92
    public static function getWhere(DataTablesMappingInterface $mapping): string {
93
94
        return implode(" ", [
95
            DataTablesMappingHelper::getAlias($mapping),
96
            DataTablesMappingHelper::getComparator($mapping),
97
            DataTablesMappingHelper::getParam($mapping),
98
        ]);
99
    }
100
}
101