Passed
Branch master (8f17c6)
by RN
04:17 queued 31s
created

WhereQueryBuilder::buildWhereInQuery()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 3
nop 1
dl 0
loc 28
rs 9.0777
c 0
b 0
f 0
1
<?php
2
/**
3
 * The Query builder API.
4
 *
5
 * @author RN Kushwaha <[email protected]>
6
 *
7
 * @since v0.0.1 <Date: 16th April, 2019>
8
 */
9
10
namespace Dolphin\Builders;
11
/**
12
 * This class provides the mechanism to build the Where Queries.
13
 */
14
class WhereQueryBuilder extends QueryBuilder
15
{
16
  private $qb;
17
18
  public function __construct(){
19
    $this->qb = new QueryBuilder();
20
  }
21
22
    protected function prepareArrayForWhere($bindKey, $bindVal = null){
23
        $ar = $conditionAr = array();
24
        // expecting a string like 'status = :status'
25
        if ($this->checkWherePrepareUsed($bindKey)) {
26
            $conditionAr = preg_split('/:/', $bindKey);
27
        }
28
29
        // taking the second part just after :
30
        if (is_array($conditionAr) && count($conditionAr)) {
31
            $ar[':'.$conditionAr[1]] = $bindVal;
32
        }
33
34
        return $ar;
35
    }
36
37
    public function parseWhereQuery($whereQuery = [])
38
    {
39
        $ar = array();
40
41
        foreach ($whereQuery as $where) {
42
            if (is_array($where[1])) {
43
                foreach ($where[1] as $key => $value) {
44
                    $ar[':'.$key] = $value;
45
                }
46
            } elseif ($where[1] != '') {
47
                $arNext = $this->prepareArrayForWhere($where[0], $where[1]);
48
                if (count($arNext)) {
49
                    $ar = array_merge($ar, $arNext);
50
                }
51
            }
52
        }
53
54
        return $ar;
55
    }
56
57
    public function checkWherePrepareUsed($condition)
58
    {
59
        return preg_match('/:+[a-zA-Z_]/', $condition);
60
    }
61
62
    public function buildWhereQuery($conditions = array())
63
    {
64
        $query = array();
65
66
        if (count($conditions)) {
67
            $firstTime = true;
68
            $query[] = 'WHERE';
69
            $this->whereAdded = true;
70
71
            foreach ($conditions as $where) {
72
                $sign = '=';
73
                if(count($where)==3) {
74
                    $sign = $where[1];
75
                }
76
                if ($firstTime) {
77
                    $query[] = $this->qb->quote($where[0]).' '.$sign.' '.end($where);
78
                    $firstTime = false;
79
                } else {
80
                    $query[] = 'AND '.$this->qb->quote($where[0]).' '.$sign.' '.end($where);
81
                }
82
            }
83
        }
84
85
        return $query;
86
    }
87
88
    public function buildWhereInClauseQuery($terms = array())
89
    {
90
        if (is_int($terms[0])) {
91
            $dataStr = join(',', $terms);
92
        } elseif (is_string($terms[0])) {
93
            $dataStr = join("', '", $terms);
94
            $dataStr = "'".$dataStr."'";
95
        } else {
96
            return null;
97
        }
98
99
        return $dataStr;
100
    }
101
102
    public function buildWhereInQuery($conditions = array())
103
    {
104
        $query = array();
105
106
        if (count($conditions)) {
107
            $firstTime = false;
108
            if ($this->whereAdded === false) {
109
                $query[] = 'WHERE';
110
                $firstTime = true;
111
                $this->whereAdded = true;
112
            }
113
114
            foreach ($conditions as $whereIn) {
115
                $dataStr = $this->buildWhereInClauseQuery($whereIn[1]);
116
                if ($dataStr === null) {
117
                    continue;
118
                }
119
120
                if ($firstTime) {
121
                    $query[] = $whereIn[0].' IN ('.$dataStr.')';
122
                    $firstTime = false;
123
                } else {
124
                    $query[] = 'AND '.$whereIn[0].' IN ('.$dataStr.')';
125
                }
126
            }
127
        }
128
129
        return $query;
130
    }
131
132
    public function buildWhereNotInQuery($conditions = array())
133
    {
134
        $query = array();
135
136
        if (count($conditions)) {
137
            $firstTime = false;
138
            if ($this->whereAdded === false) {
139
                $query[] = 'WHERE';
140
                $firstTime = true;
141
                $this->whereAdded = true;
142
            }
143
144
            foreach ($conditions as $whereNotIn) {
145
                $dataStr = $this->buildWhereInClauseQuery($whereNotIn[1]);
146
                if ($dataStr === null) {
147
                    continue;
148
                }
149
150
                if ($firstTime) {
151
                    $query[] = $whereNotIn[0].' NOT IN ('.$dataStr.')';
152
                    $firstTime = false;
153
                } else {
154
                    $query[] = 'AND '.$whereNotIn[0].' NOT IN ('.$dataStr.')';
155
                }
156
            }
157
        }
158
159
        return $query;
160
    }
161
162
    public function buildWhereNullQuery($conditions = array())
163
    {
164
        $query = array();
165
166
        if (count($conditions)) {
167
            $firstTime = false;
168
            if ($this->whereAdded === false) {
169
                $query[] = 'WHERE';
170
                $firstTime = true;
171
                $this->whereAdded = true;
172
            }
173
174
            foreach ($conditions as $whereNull) {
175
                if ($firstTime) {
176
                    $query[] = $whereNull.' IS NULL';
177
                    $firstTime = false;
178
                } else {
179
                    $query[] = 'AND '.$whereNull.' IS NULL';
180
                }
181
            }
182
        }
183
184
        return $query;
185
    }
186
187
    public function buildWhereNotNullQuery($conditions = array())
188
    {
189
        $query = array();
190
191
        if (count($conditions)) {
192
            $firstTime = false;
193
            if ($this->whereAdded === false) {
194
                $query[] = 'WHERE';
195
                $firstTime = true;
196
                $this->whereAdded = true;
197
            }
198
199
            foreach ($conditions as $whereNotNull) {
200
                if ($firstTime) {
201
                    $query[] = $whereNotNull.' IS NOT NULL';
202
                    $firstTime = false;
203
                } else {
204
                    $query[] = 'AND '.$whereNotNull.' IS NOT NULL';
205
                }
206
            }
207
        }
208
209
        return $query;
210
    }
211
212
    public function buildAllWhereQuery($where, $whereIn, $whereNotIn, $whereNull, $whereNotNull)
213
    {
214
        $query = array();
215
        $whereQuery = $this->buildWhereQuery($where);
216
        if (count($whereQuery)) {
217
            $query = array_merge($query, $whereQuery);
218
        }
219
220
        $whereInQuery = $this->buildWhereInQuery($whereIn);
221
        if (count($whereInQuery)) {
222
            $query = array_merge($query, $whereInQuery);
223
        }
224
225
        $whereNotInQuery = $this->buildWhereNotInQuery($whereNotIn);
226
        if (count($whereNotInQuery)) {
227
            $query = array_merge($query, $whereNotInQuery);
228
        }
229
230
        $whereNullQuery = $this->buildWhereNullQuery($whereNull);
231
        if (count($whereNullQuery)) {
232
            $query = array_merge($query, $whereNullQuery);
233
        }
234
235
        $whereNotNullQuery = $this->buildWhereNotNullQuery($whereNotNull);
236
        if (count($whereNotNullQuery)) {
237
            $query = array_merge($query, $whereNotNullQuery);
238
        }
239
240
        return $query;
241
    }
242
}
243