WhereQueryParser::checkWherePrepareUsed()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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