Passed
Push — master ( 36c8a5...213898 )
by RN
01:43
created

WhereQueryParser::parseWhereQuery()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 12
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 21
rs 8.8333
1
<?php
2
/**
3
 * The Query builder API.
4
 *
5
 * @author RN Kushwaha <[email protected]>
6
 * @since v0.0.1 <Date: 16th April, 2019>
7
 */
8
9
namespace Dolphin\Parsers;
10
/**
11
 * This class provides the mechanism to build the Where Queries.
12
 */
13
class WhereQueryParser
14
{
15
16
    protected function prepareArrayForWhere($bindKey, $bindVal = null){
17
        $ar = $conditionAr = [];
18
        // expecting a string like 'status = :status'
19
        if ($this->checkWherePrepareUsed($bindKey)) {
20
            $conditionAr = preg_split('/:/', $bindKey);
21
        }
22
23
        // taking the second part just after :
24
        if (is_array($conditionAr) && count($conditionAr)) {
25
            $ar[':'.$conditionAr[1]] = $bindVal;
26
        }
27
28
        return $ar;
29
    }
30
31
    public function parseWhereQuery($whereQuery = [])
32
    {
33
        $ar = [];
34
        if(!count($whereQuery)){
35
          return $ar;
36
        }
37
38
        foreach ($whereQuery as $where) {
39
            if (is_array($where[1])) {
40
                foreach ($where[1] as $key => $value) {
41
                    $ar[':'.$key] = $value;
42
                }
43
            } elseif ($where[1] != '') {
44
                $arNext = $this->prepareArrayForWhere($where[0], $where[1]);
45
                if (count($arNext)) {
46
                    $ar = array_merge($ar, $arNext);
47
                }
48
            }
49
        }
50
51
        return $ar;
52
    }
53
54
    public function checkWherePrepareUsed($condition)
55
    {
56
        return preg_match('/:+[a-zA-Z_]/', $condition);
57
    }
58
}
59