Completed
Branch master (33b3f8)
by compolom
10:20 queued 05:46
created

Builder::setTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
3
namespace DrMVC\Orm;
4
5
use DrMVC\Orm\Interfaces\BuilderInterface;
6
7
class Builder implements BuilderInterface
8
{
9
10
    private $sql;
11
12
    private $placeholders = [];
13
14
    private $table;
15
16
    private $where = [];
17
18
    /**
19
     * Builder constructor.
20
     * @param string $table
21
     */
22 1
    public function __construct(string $table)
23
    {
24 1
        $this->setTable($table);
25 1
    }
26
27
    /**
28
     * @return string
29
     */
30 11
    public function __toString(): string
31
    {
32 11
        $this->prepareSql();
33 11
        $sql = $this->getSql();
34 11
        $this->clean();
35
36 11
        return trim($sql);
37
    }
38
39
    /**
40
     * Build and set sql query
41
     *
42
     * @return void
43
     */
44 11
    private function prepareSql()
45
    {
46 11
        $this->setSql(
47 11
            $this->getSql()
48 11
            . $this->prepareWhere()
49
        );
50 11
    }
51
52
    /**
53
     * @param string $sql
54
     */
55 11
    private function setSql(string $sql)
56
    {
57 11
        $this->sql = $sql;
58 11
    }
59
60
    /**
61
     * @return string
62
     */
63 11
    private function getSql(): string
64
    {
65 11
        return $this->sql;
66
    }
67
68
    /**
69
     * @return string
70
     */
71 11
    private function prepareWhere(): string
72
    {
73 11
        if (\count($this->where)) {
74 7
            $placeholders = $this->map($this->where, ':where');
75 7
            $where = $this->keyValueFormat($this->where, $placeholders);
76 7
            $this->setPlaceholders($this->where, 'where');
77
78 7
            return 'WHERE ' . implode(' AND ', $where) . ' ';
79
        }
80
81 4
        return '';
82
    }
83
84
    /**
85
     * @param array $array
86
     * @param string $prefix
87
     * @return array
88
     */
89 9
    private function map(array $array, string $prefix): array
90
    {
91 9
        $result = [];
92 9
        foreach ($array as $key => $value) {
93 9
            $result[$prefix . '_' . $key] = $value;
94
        }
95
96 9
        return $result;
97
    }
98
99
    /**
100
     * @param array $keys
101
     * @param array $values
102
     * @return array
103
     */
104 7
    private function keyValueFormat(array $keys, array $values): array
105
    {
106 7
        return array_map(
107 7
            function (string $key, string $placeholder) {
108 7
                return sprintf('%s = %s', $key, $placeholder);
109 7
            },
110 7
            array_keys($keys),
111 7
            array_keys($values)
112
        );
113
    }
114
115
    /**
116
     * @param array $placeholders
117
     * @param string $prefix
118
     */
119 9
    private function setPlaceholders(array $placeholders, string $prefix)
120
    {
121 9
        $this->placeholders += $this->map($placeholders, $prefix);
122 9
    }
123
124
    /**
125
     * @return void
126
     */
127 11
    private function clean()
128
    {
129 11
        $this->sql = null;
130 11
        $this->where = [];
131 11
    }
132
133
    /**
134
     * Allias method for where with id argument
135
     *
136
     * @param int $id
137
     * @return BuilderInterface
138
     */
139 3
    public function byId(int $id): BuilderInterface
140
    {
141 3
        $this->where = ['id' => $id];
142
143 3
        return $this;
144
    }
145
146
    /**
147
     * @param array $where
148
     * @return BuilderInterface
149
     */
150 6
    public function select(array $where = []): BuilderInterface
151
    {
152 6
        $this->where($where);
153 6
        $this->setSql('SELECT * FROM ' . $this->getTable() . ' ');
154
155 6
        return $this;
156
    }
157
158
    /**
159
     * @param array $where
160
     * @return BuilderInterface
161
     */
162 9
    public function where(array $where): BuilderInterface
163
    {
164 9
        $this->where += $where;
165
166 9
        return $this;
167
    }
168
169
    /**
170
     * @return string
171
     */
172 11
    private function getTable(): string
173
    {
174 11
        return $this->table;
175
    }
176
177
    /**
178
     * @param string $table
179
     * @return void
180
     */
181 1
    private function setTable(string $table)
182
    {
183 1
        $this->table = $table;
184 1
    }
185
186
    /**
187
     * @param array $data
188
     * @param array $where
189
     * @return BuilderInterface
190
     */
191 2
    public function update(array $data, array $where = []): BuilderInterface
192
    {
193 2
        $this->where($where);
194 2
        $placeholders = $this->map($data, ':update');
195 2
        $this->setSql('UPDATE ' . $this->getTable() . ' SET ' . implode(', ',
196 2
                $this->keyValueFormat($data, $placeholders)) . ' ');
197 2
        $this->setPlaceholders($data, 'update');
198
199 2
        return $this;
200
    }
201
202
    /**
203
     * @return array
204
     */
205 5
    public function getPlaceholders(): array
206
    {
207 5
        $placeholders = $this->placeholders;
208
        // clean placeholders
209 5
        $this->placeholders = [];
210
211 5
        return $placeholders;
212
    }
213
214
    /**
215
     * @param array $data
216
     * @return BuilderInterface
217
     */
218 2
    public function insert(array $data): BuilderInterface
219
    {
220 2
        $placeholders = $this->map($data, ':insert');
221 2
        $this->setSql('INSERT INTO ' . $this->getTable() . ' (' . implode(', ',
222 2
                array_keys($data)) . ') VALUES (' . implode(', ', array_keys($placeholders)) . ') ');
223 2
        $this->setPlaceholders($data, 'insert');
224
225 2
        return $this;
226
    }
227
228
    /**
229
     * @param array $where
230
     * @return BuilderInterface
231
     */
232 2
    public function delete(array $where = []): BuilderInterface
233
    {
234 2
        $this->where($where);
235 2
        $this->setSql('DELETE FROM ' . $this->getTable() . ' ');
236
237 2
        return $this;
238
    }
239
}
240