Completed
Push — master ( fde9d1...e3e1f6 )
by Midori
24:56 queued 10:02
created

QueryMaker::getOperator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace midorikocak\querymaker;
6
7
use function implode;
8
use function preg_match;
9
use function reset;
10
use function strlen;
11
use function substr;
12
use function uniqid;
13
14
class QueryMaker implements QueryInterface
15
{
16
    private string $query;
17
    private string $statement;
18
    private array $params;
19
20 21
    private function __construct()
21
    {
22 21
        $this->query = '';
23 21
        $this->statement = '';
24 21
        $this->params = [];
25 21
    }
26
27 18
    public static function select($table, array $columns = ['*']): QueryInterface
28
    {
29 18
        $instance = new QueryMaker();
30 18
        $columnsText = implode(', ', $columns);
31 18
        $instance->statement = 'SELECT '.$columnsText.' FROM '.$table;
32 18
        $instance->query = 'SELECT '.$columnsText.' FROM '.$table;
33
        return $instance;
34
    }
35 3
36
    public static function update($table, array $values): QueryInterface
37 3
    {
38 3
        $instance = new QueryMaker();
39 3
        $instance->statement = 'UPDATE '.$table.' SET ';
40 3
        $instance->query = 'UPDATE '.$table.' SET ';
41
        $instance->prepareParams($values, ', ');
42
        return $instance;
43 15
    }
44
45 15
    public static function delete($table): QueryInterface
46 15
    {
47
        $instance = new QueryMaker();
48
        $instance->statement = 'DELETE FROM '.$table;
49 15
        $instance->query = 'DELETE FROM '.$table;
50
        return $instance;
51
    }
52 15
53 15
    public function where($key, $value): QueryInterface
54 15
    {
55 15
        $operator = $this->getOperator($value);
56
        $this->statement .= ' WHERE '.$key.$operator.':'.$key;
57
        $this->query .= ' WHERE '.$key.$operator.'\''.$value.'\'';
58 6
        $this->params[$key] = $value;
59
        return $this;
60 6
    }
61 6
62 6
    public function and($key, $value): QueryInterface
63 6
    {
64
        $this->query .= " AND ";
65
        $this->statement .= " AND ";
66 6
        $this->prepareParam($key, $value, 'AND');
67
        return $this;
68 6
    }
69 6
70 6
    public function or($key, $value): QueryInterface
71 6
    {
72
        $this->query .= " OR ";
73
        $this->statement .= " OR ";
74
        $this->prepareParam($key, $value, 'OR');
75
        return $this;
76
    }
77
78
    public function between($key, $before, $after): QueryInterface
79
    {
80
        $this->query .= $key." BETWEEN $before AND $after";
81
        $this->statement .= $key.' BETWEEN :before AND :after';
82
83
        $this->params['before'] = $before;
84 21
        $this->params['after'] = $after;
85
        return $this;
86 21
    }
87
88
    public function getQuery(): string
89 21
    {
90
        return $this->query;
91 21
    }
92
93
    public function getStatement(): string
94
    {
95
        return $this->statement;
96
    }
97
98
    public function getParams(): array
99 12
    {
100
        return $this->params;
101 12
    }
102 12
103
    private function prepareParams(array $values, string $glue)
104 12
    {
105 12
        $params = [];
106 12
        $queryValues = [];
107
108
        foreach ($values as $key => $value) {
109
            $operator = $this->getOperator($value);
110 12
            if (!isset($this->params[$key])) {
111
                $queryValues[] = $key.$operator.'\''.$value.'\'';
112
                $params [] = $key.$operator.':'.$key;
113 12
114 12
                $this->params[$key] = $value;
115 12
            } else {
116
                $uniqid = uniqid('', true);
117 12
                $queryValues[] = $key.$operator.'\''.$value.'\'';
118
                $params [] = $key.$operator.':'.$key.$uniqid;
119
120
                $this->params[$key.$uniqid] = $value;
121
            }
122
        }
123
124
        $this->query .= implode($glue, $queryValues);
125
        $this->statement .= implode($glue, $params);
126
    }
127 12
128 12
    private function getOperator(&$value)
129 12
    {
130
        $hasOperator = preg_match('~^(([<>=])+(=)*)~', (string)$value, $matches);
131 9
        if (!empty($hasOperator)) {
132
            $operator = reset($matches);
133 9
            $value = substr($value, strlen($operator));
134 9
        } else {
135
            $operator = '=';
136
        }
137
138
        return $operator;
139
    }
140
141
    private function prepareParam(string $key, $value, string $glue)
142
    {
143
        $this->prepareParams([$key => $value], $glue);
144
    }
145
}
146