Completed
Push — master ( 3fc573...634850 )
by WEBEWEB
11:27
created

queryBuilderRule2SQL()   D

Complexity

Conditions 21
Paths 21

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 4.1666
c 0
b 0
f 0
cc 21
nc 21
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the jquery-querybuilder-bundle package.
5
 *
6
 * (c) 2019 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\QueryBuilderBundle\Helper;
13
14
use WBW\Bundle\JQuery\QueryBuilderBundle\API\QueryBuilderConditionInterface;
15
use WBW\Bundle\JQuery\QueryBuilderBundle\API\QueryBuilderEnumerator;
16
use WBW\Bundle\JQuery\QueryBuilderBundle\API\QueryBuilderOperatorInterface;
17
use WBW\Bundle\JQuery\QueryBuilderBundle\API\QueryBuilderRuleInterface;
18
use WBW\Bundle\JQuery\QueryBuilderBundle\API\QueryBuilderRuleSetInterface;
19
use WBW\Bundle\JQuery\QueryBuilderBundle\API\QueryBuilderTypeInterface;
20
use WBW\Library\Core\Argument\IntegerHelper;
21
22
/**
23
 * QueryBuilder repository helper.
24
 *
25
 * @author webeweb <https://github.com/webeweb/>
26
 * @package WBW\Bundle\JQuery\QueryBuilderBundle\Helper
27
 */
28
class QueryBuilderRepositoryHelper implements QueryBuilderConditionInterface, QueryBuilderOperatorInterface, QueryBuilderTypeInterface {
29
30
    /**
31
     * Convert a rule into a SQL string representation.
32
     *
33
     * @param QueryBuilderRuleInterface $rule The rule.
34
     * @return string Returns the SQL string representing the rule.
35
     */
36
    public static function queryBuilderRule2SQL(QueryBuilderRuleInterface $rule) {
37
38
        $sql   = [];
39
        $sql[] = $rule->getField();
40
        $sql[] = QueryBuilderEnumerator::enumOperators()[$rule->getOperator()];
41
42
        switch ($rule->getOperator()) {
43
44
            case self::OPERATOR_BEGINS_WITH:
45
            case self::OPERATOR_NOT_BEGINS_WITH:
46
                $sql[] = "'" . static::quoteMixedValue($rule->getType(), $rule->getValue(), false) . "%'";
47
                break;
48
49
            case self::OPERATOR_BETWEEN:
50
            case self::OPERATOR_NOT_BETWEEN:
51
                $sql[] = implode(" " . self::CONDITION_AND . " ", static::quoteArrayValue($rule->getType(), $rule->getValue(), true));
52
                break;
53
54
            case self::OPERATOR_CONTAINS:
55
            case self::OPERATOR_NOT_CONTAINS:
56
                $sql[] = "'%" . static::quoteMixedValue($rule->getType(), $rule->getValue(), false) . "%'";
57
                break;
58
59
            case self::OPERATOR_ENDS_WITH:
60
            case self::OPERATOR_NOT_ENDS_WITH:
61
                $sql[] = "'%" . static::quoteMixedValue($rule->getType(), $rule->getValue(), false) . "'";
62
                break;
63
64
            case self::OPERATOR_EQUAL:
65
            case self::OPERATOR_GREATER:
66
            case self::OPERATOR_GREATER_OR_EQUAL:
67
            case self::OPERATOR_LESS:
68
            case self::OPERATOR_LESS_OR_EQUAL:
69
            case self::OPERATOR_NOT_EQUAL:
70
                $sql[] = static::quoteMixedValue($rule->getType(), $rule->getValue(), true);
71
                break;
72
73
            case self::OPERATOR_IN:
74
            case self::OPERATOR_NOT_IN:
75
                $sql[] = "(" . implode(", ", static::quoteArrayValue($rule->getType(), $rule->getValue(), true)) . ")";
76
                break;
77
78
            case self::OPERATOR_IS_EMPTY:
79
            case self::OPERATOR_IS_NOT_EMPTY:
80
            case self::OPERATOR_IS_NOT_NULL:
81
            case self::OPERATOR_IS_NULL:
82
                // NOTHING TO DO.
83
                break;
84
        }
85
86
        return implode(" ", $sql);
87
    }
88
89
    /**
90
     * Convert a rule set into a SQL string representation.
91
     *
92
     * @param QueryBuilderRuleSetInterface $ruleSet The rule set.
93
     * @return string Returns the SQL string representing the rule set.
94
     */
95
    public static function queryBuilderRuleSet2SQL(QueryBuilderRuleSetInterface $ruleSet) {
96
97
        if (0 === count($ruleSet->getRules())) {
98
            return "";
99
        }
100
101
        $sql = [];
102
103
        foreach ($ruleSet->getRules() as $current) {
104
105
            if (true === ($current instanceof QueryBuilderRuleSetInterface)) {
106
107
                /** @var QueryBuilderRuleSetInterface $current */
108
                $sql[] = static::queryBuilderRuleSet2SQL($current);
109
                continue;
110
            }
111
112
            /** @var QueryBuilderRuleInterface $current */
113
            $sql[] = static::queryBuilderRule2SQL($current);
114
        }
115
116
        return "(" . implode(" " . $ruleSet->getCondition() . " ", $sql) . ")";
117
    }
118
119
    /**
120
     * Quote an array of values.
121
     *
122
     * @param string $type The type.
123
     * @param mixed $value The value.
124
     * @param bool $wrap Wrap ?
125
     * @return array Returns the quoted values.
126
     */
127
    protected static function quoteArrayValue($type, $value, $wrap = false) {
128
        $output = [];
129
        foreach ($value as $current) {
130
            $output[] = static::quoteMixedValue($type, $current, $wrap);
131
        }
132
        return $output;
133
    }
134
135
    /**
136
     * Quote a mixed value.
137
     *
138
     * @param string $type The type.
139
     * @param mixed $value The value.
140
     * @param bool $wrap Wrap ?
141
     * @return mixed|null Returns the quoted value in case of success, null otherwise.
142
     */
143
    protected static function quoteMixedValue($type, $value, $wrap = false) {
144
145
        $output = null;
146
147
        switch ($type) {
148
149
            case self::TYPE_BOOLEAN:
150
                $output = IntegerHelper::parseBoolean($value);
151
                break;
152
153
            case self::TYPE_DATE:
154
            case self::TYPE_DATETIME:
155
            case self::TYPE_STRING:
156
            case self::TYPE_TIME:
157
                $output = true === $wrap ? "'" . addslashes($value) . "'" : addslashes($value);
158
                break;
159
160
            case self::TYPE_DOUBLE:
161
            case self::TYPE_INTEGER:
162
                $output = $value;
163
                break;
164
        }
165
166
        return $output;
167
    }
168
}
169