Passed
Push — main ( 87c1e5...f29b76 )
by William
01:42
created

Update::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Willry\QueryBuilder;
4
5
class Update extends Base
6
{
7
    protected function mountQuery(): void
8
    {
9
10
        $dateSet = [];
11
        foreach ($this->fields as $bind => $value) {
12
            $dateSet[] = "{$bind} = ?";
13
        }
14
        $dateSet = implode(", ", $dateSet);
15
16
        $this->query = "UPDATE {$this->entity} {$this->joins} SET {$dateSet} {$this->where}";
17
    }
18
19
    /**
20
     * @param array $data
21
     */
22
    public function update(array $data): static
23
    {
24
        $this->setBindings(array_values($data), 'update');
25
26
        $this->fields = $data;
27
28
        return $this;
29
    }
30
31
    /**
32
     * @param array $data
33
     */
34
    public function exec(): ?int
35
    {
36
        try {
37
            $this->mountQuery();
38
39
            $stmt = $this->db->prepare($this->query);
40
41
            QueryHelpers::bind($stmt, $this->flatBindings());
42
43
            $stmt->execute();
44
45
            return $stmt->rowCount() ?? 1;
46
        } catch (\PDOException $exception) {
47
            $this->handleError($exception);
48
        }
49
    }
50
}
51