Passed
Push — main ( 440f49...00eeb9 )
by Peter
02:41
created

Update::where()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 7
rs 10
ccs 4
cts 4
cp 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace QB\Generic\Statement;
6
7
use QB\Generic\Clause\Table;
8
use QB\Generic\Expr\Expr;
9
use QB\Generic\IQueryPart;
10
11
class Update implements IUpdate
12
{
13
    /** @var array<int,string|Table> */
14
    protected array $tables = [];
15
16
    /** @var string[] */
17
    protected array $modifiers = [];
18
19
    /** @var array<string,mixed> */
20
    protected array $rawValues = [];
21
22
    /** @var IQueryPart[] */
23
    protected array $whereParts = [];
24
25
    /**
26
     * @param string|Table ...$tables
27
     */
28 11
    public function __construct(string|Table ...$tables)
29
    {
30 11
        $this->tables = array_merge($this->tables, $tables);
31 11
    }
32
33
    /**
34
     * @param string ...$modifiers
35
     *
36
     * @return $this
37
     */
38 1
    public function modifier(string ...$modifiers): static
39
    {
40 1
        $this->modifiers = array_merge($this->modifiers, $modifiers);
41
42 1
        return $this;
43
    }
44
45
    /**
46
     * @param array<string,mixed> $values
47
     *
48
     * @return $this
49
     */
50 9
    public function values(array $values): static
51
    {
52 9
        $this->rawValues = $values;
53
54 9
        return $this;
55
    }
56
57
    /**
58
     * @param string|IQueryPart ...$whereParts
59
     *
60
     * @return $this
61
     */
62 9
    public function where(string|IQueryPart ...$whereParts): static
63
    {
64 9
        foreach ($whereParts as $wherePart) {
65 9
            $this->whereParts[] = is_string($wherePart) ? new Expr($wherePart) : $wherePart;
66
        }
67
68 9
        return $this;
69
    }
70
71
    /**
72
     * @return string
73
     */
74 9
    public function __toString(): string
75
    {
76 9
        if (!$this->isValid()) {
77 2
            throw new \RuntimeException('Under-initialized UPDATE query. Table, values and where are necessary');
78
        }
79
80 7
        $sqlParts = array_merge(
81 7
            [$this->getCommand()],
82 7
            $this->getSet(),
83 7
            $this->getWhere(),
84
        );
85
86 7
        $sqlParts = array_filter($sqlParts);
87
88 7
        return implode(PHP_EOL, $sqlParts);
89
    }
90
91 9
    public function isValid(): bool
92
    {
93 9
        return count($this->tables) === 1 && count($this->rawValues) > 0 && count($this->whereParts) > 0;
94
    }
95
96
    /**
97
     * @return string
98
     */
99 7
    protected function getCommand(): string
100
    {
101 7
        $sql   = [];
102 7
        $sql[] = 'UPDATE';
103 7
        $sql[] = implode(' ', $this->modifiers);
104 7
        $sql[] = $this->tables[0];
105
106 7
        $sql = array_filter($sql);
107
108 7
        return implode(' ', $sql);
109
    }
110
111
    /**
112
     * @return string[]
113
     */
114 7
    protected function getSet(): array
115
    {
116 7
        $values = [];
117 7
        foreach ($this->rawValues as $column => $value) {
118 7
            $values[] = sprintf('%s = %s', $column, $value);
119
        }
120
121 7
        return ['SET ' . implode(', ', $values)];
122
    }
123
124
    /**
125
     * @return string[]
126
     */
127 7
    protected function getWhere(): array
128
    {
129 7
        $whereParts = [];
130 7
        foreach ($this->whereParts as $wherePart) {
131 7
            $whereParts[] = (string)$wherePart;
132
        }
133
134 7
        return ['WHERE ' . implode(' AND ', $whereParts)];
135
    }
136
137
    /**
138
     * @return array
139
     */
140 2
    public function getParams(): array
141
    {
142 2
        $params = [];
143
144 2
        foreach ($this->rawValues as $values) {
145 2
            if ($values instanceof IQueryPart) {
146 2
                $params = array_merge($params, $values->getParams());
147
            }
148
        }
149
150 2
        foreach ($this->whereParts as $wherePart) {
151 2
            $params = array_merge($params, $wherePart->getParams());
152
        }
153
154 2
        return $params;
155
    }
156
}
157