Passed
Push — master ( 557494...54d8cd )
by RN
01:38
created

WhereQueryParser::parseWhereQuery()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 10
nc 6
nop 1
dl 0
loc 18
rs 9.2222
c 1
b 0
f 0
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 = array();
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 = array();
34
35
        foreach ($whereQuery as $where) {
36
            if (is_array($where[1])) {
37
                foreach ($where[1] as $key => $value) {
38
                    $ar[':'.$key] = $value;
39
                }
40
            } elseif ($where[1] != '') {
41
                $arNext = $this->prepareArrayForWhere($where[0], $where[1]);
42
                if (count($arNext)) {
43
                    $ar = array_merge($ar, $arNext);
44
                }
45
            }
46
        }
47
48
        return $ar;
49
    }
50
51
    public function checkWherePrepareUsed($condition)
52
    {
53
        return preg_match('/:+[a-zA-Z_]/', $condition);
54
    }
55
}
56