Passed
Push — master ( e89808...fde9d1 )
by Midori
13:40
created

QueryMaker::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 4
cp 0.75
crap 1.0156
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
        $hasOperator = preg_match('~^(([<>=])+(=)*)~', (string) $value);
56
        if (! empty($hasOperator)) {
57
            $operator = '';
58 6
        } else {
59
            $operator = '=';
60 6
        }
61 6
62 6
        $this->statement   .= ' WHERE ' . $key . $operator . ':' . $key;
63 6
        $this->query       .= ' WHERE ' . $key . $operator . '\'' . $value . '\'';
64
        $this->params[$key] = $value;
65
        return $this;
66 6
    }
67
68 6
    public function and($key, $value) : QueryInterface
69 6
    {
70 6
        $this->query     .= " AND ";
71 6
        $this->statement .= " AND ";
72
        $this->prepareParam($key, $value, 'AND');
73
        return $this;
74
    }
75
76
    public function or($key, $value) : QueryInterface
77
    {
78
        $this->query     .= " OR ";
79
        $this->statement .= " OR ";
80
        $this->prepareParam($key, $value, 'OR');
81
        return $this;
82
    }
83
84 21
    public function between($key, $before, $after) : QueryInterface
85
    {
86 21
        $this->query     .= $key . " BETWEEN $before AND $after";
87
        $this->statement .= $key . ' BETWEEN :before AND :after';
88
89 21
        $this->params['before'] = $before;
90
        $this->params['after']  = $after;
91 21
        return $this;
92
    }
93
94
    public function getQuery() : string
95
    {
96
        return $this->query;
97
    }
98
99 12
    public function getStatement() : string
100
    {
101 12
        return $this->statement;
102 12
    }
103
104 12
    public function getParams() : array
105 12
    {
106 12
        return $this->params;
107
    }
108
109
    private function prepareParams(array $values, string $glue)
110 12
    {
111
        $params      = [];
112
        $queryValues = [];
113 12
114 12
        foreach ($values as $key => $value) {
115 12
            $hasOperator = preg_match('~^(([<>=])+(=)*)~', $value, $matches);
116
            if (! empty($hasOperator)) {
117 12
                $operator = reset($matches);
118
                $value    = substr($value, strlen($operator));
119
            } else {
120
                $operator = '=';
121
            }
122
123
            if (! isset($this->params[$key])) {
124
                $queryValues[] = $key . $operator . '\'' . $value . '\'';
125
                $params []     = $key . $operator . ':' . $key;
126
127 12
                $this->params[$key] = $value;
128 12
            } else {
129 12
                $uniqid        = uniqid('', true);
130
                $queryValues[] = $key . $operator . '\'' . $value . '\'';
131 9
                $params []     = $key . $operator . ':' . $key . $uniqid;
132
133 9
                $this->params[$key . $uniqid] = $value;
134 9
            }
135
        }
136
137
        $this->query     .= implode($glue, $queryValues);
138
        $this->statement .= implode($glue, $params);
139
    }
140
141
    private function prepareParam(string $key, $value, string $glue)
142
    {
143
        $this->prepareParams([$key => $value], $glue);
144
    }
145
}
146