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

QueryBuilderRule::quoteMixed()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.0555
c 0
b 0
f 0
cc 9
nc 12
nop 2
1
<?php
2
3
/*
4
 * This file is part of the jquery-querybuilder-bundle package.
5
 *
6
 * (c) 2017 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\Rule;
13
14
use WBW\Bundle\JQuery\QueryBuilderBundle\API\QueryBuilderConditionInterface;
15
use WBW\Bundle\JQuery\QueryBuilderBundle\API\QueryBuilderOperatorInterface;
16
use WBW\Bundle\JQuery\QueryBuilderBundle\Data\AbstractQueryBuilderData;
17
use WBW\Bundle\JQuery\QueryBuilderBundle\Decorator\QueryBuilderDecoratorInterface;
18
19
/**
20
 * jQuery QueryBuilder rule.
21
 *
22
 * @author webeweb <https://github.com/webeweb/>
23
 * @package WBW\Bundle\JQuery\QueryBuilderBundle\Rule
24
 */
25
class QueryBuilderRule extends AbstractQueryBuilderData implements QueryBuilderConditionInterface, QueryBuilderOperatorInterface, QueryBuilderRuleInterface {
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function toSQL() {
31
32
        // Check the decorator.
33
        if (null !== $this->decorator) {
34
            return $this->decorator->toSQL($this);
35
        }
36
37
        // Initialize the SQL.
38
        $sql   = [];
39
        $sql[] = $this->getField();
40
        $sql[] = self::OPERATORS[$this->operator];
41
42
        // Switch into operator.
43
        switch ($this->operator) {
44
45
            case self::OPERATOR_BEGINS_WITH:
46
            case self::OPERATOR_NOT_BEGINS_WITH:
47
                $sql[] = "'" . $this->quoteMixed($this->value, false) . "%'";
48
                break;
49
50
            case self::OPERATOR_BETWEEN:
51
            case self::OPERATOR_NOT_BETWEEN:
52
                $sql[] = implode(" " . self::CONDITION_AND . " ", $this->quoteArray($this->value, true));
53
                break;
54
55
            case self::OPERATOR_CONTAINS:
56
            case self::OPERATOR_NOT_CONTAINS:
57
                $sql[] = "'%" . $this->quoteMixed($this->value, false) . "%'";
58
                break;
59
60
            case self::OPERATOR_ENDS_WITH:
61
            case self::OPERATOR_NOT_ENDS_WITH:
62
                $sql[] = "'%" . $this->quoteMixed($this->value, false) . "'";
63
                break;
64
65
            case self::OPERATOR_EQUAL:
66
            case self::OPERATOR_GREATER:
67
            case self::OPERATOR_GREATER_OR_EQUAL:
68
            case self::OPERATOR_LESS:
69
            case self::OPERATOR_LESS_OR_EQUAL:
70
            case self::OPERATOR_NOT_EQUAL:
71
                $sql[] = $this->quoteMixed($this->value, true);
72
                break;
73
74
            case self::OPERATOR_IN:
75
            case self::OPERATOR_NOT_IN:
76
                $sql[] = "(" . implode(", ", $this->quoteArray($this->value, true)) . ")";
77
                break;
78
79
            case self::OPERATOR_IS_EMPTY:
80
            case self::OPERATOR_IS_NOT_EMPTY:
81
            case self::OPERATOR_IS_NOT_NULL:
82
            case self::OPERATOR_IS_NULL:
83
                // NOTHING TO DO.
84
                break;
85
        }
86
87
        // Return the SQL.
88
        return implode(" ", $sql);
89
    }
90
}
91