Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
33 / 33 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
Set | |
100.00% |
33 / 33 |
|
100.00% |
11 / 11 |
14 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
create | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
empty | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
parse | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
4 | |||
createSet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createValueField | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
params | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
positionalFields | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
positionalParams | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
positionalParamValues | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Projom\Storage\SQL\Component; |
6 | |
7 | use Projom\Storage\SQL\Component\ComponentInterface; |
8 | use Projom\Storage\SQL\Util; |
9 | |
10 | class Set implements ComponentInterface |
11 | { |
12 | private readonly array $sets; |
13 | private readonly array $fields; |
14 | private readonly array $params; |
15 | private readonly array $positionalParams; |
16 | private int $id = 0; |
17 | |
18 | public function __construct(array $fieldsWithValues) |
19 | { |
20 | $this->parse($fieldsWithValues); |
21 | } |
22 | |
23 | public static function create(array $fieldsWithValues): Set |
24 | { |
25 | return new Set($fieldsWithValues); |
26 | } |
27 | |
28 | public function __toString(): string |
29 | { |
30 | return Util::join($this->sets, ', '); |
31 | } |
32 | |
33 | public function empty(): bool |
34 | { |
35 | return empty($this->sets); |
36 | } |
37 | |
38 | public function parse(array $fieldsWithValues): void |
39 | { |
40 | $sets = []; |
41 | $fields = []; |
42 | $params = []; |
43 | |
44 | foreach ($fieldsWithValues as $index => $fieldValueList) { |
45 | foreach ($fieldValueList as $field => $value) { |
46 | $this->id++; |
47 | $valueField = $this->createValueField($field, $this->id); |
48 | $quotedField = Util::splitAndQuoteThenJoin($field, '.'); |
49 | $sets[] = $this->createSet($quotedField, $valueField); |
50 | $fields[$quotedField] = $quotedField; |
51 | $params[$index][$valueField] = $value; |
52 | } |
53 | } |
54 | |
55 | $this->sets = $sets; |
56 | $this->fields = $fields; |
57 | $this->params = Util::flatten($params); |
58 | |
59 | $positionalParams = []; |
60 | foreach ($params as $parameters) |
61 | $positionalParams[] = array_fill(0, count($parameters), '?'); |
62 | |
63 | $this->positionalParams = $positionalParams; |
64 | } |
65 | |
66 | private function createSet(string $quotedField, string $valueField): string |
67 | { |
68 | return "{$quotedField} = :{$valueField}"; |
69 | } |
70 | |
71 | private function createValueField(string $field, int $id): string |
72 | { |
73 | $field = str_replace(['.', ','], '_', $field); |
74 | $field = strtolower($field); |
75 | return "set_{$field}_{$id}"; |
76 | } |
77 | |
78 | public function params(): array |
79 | { |
80 | return $this->params; |
81 | } |
82 | |
83 | public function positionalFields(): string |
84 | { |
85 | return Util::join($this->fields, ', '); |
86 | } |
87 | |
88 | public function positionalParams(): array |
89 | { |
90 | return array_map( |
91 | fn ($positionalParams) => Util::join($positionalParams, ', '), |
92 | $this->positionalParams |
93 | ); |
94 | } |
95 | |
96 | public function positionalParamValues(): array |
97 | { |
98 | return array_values($this->params); |
99 | } |
100 | } |