-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetTrait.php
More file actions
77 lines (64 loc) · 1.65 KB
/
SetTrait.php
File metadata and controls
77 lines (64 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
namespace ModernPDO\Traits;
use ModernPDO\Escaper;
use ModernPDO\Functions\Scalar\ScalarFunction;
/**
* Trait for working with 'where'.
*/
trait SetTrait
{
/**
* @var array<string, scalar|ScalarFunction|null> values for SET
*/
protected array $set = [];
/**
* Returns set query.
*/
protected function setQuery(Escaper $escaper): string
{
$query = '';
foreach ($this->set as $column => $value) {
if ($value instanceof ScalarFunction) {
$value = $value->buildQuery();
} elseif (\is_bool($value)) {
$value = $escaper->boolValue($value);
} else {
$value = '?';
}
$query .= $escaper->column($column) . '=' . $value . ', ';
}
return mb_substr($query, 0, -2);
}
/**
* Returns set placeholders.
*
* @return list<mixed>
*/
protected function setPlaceholders(): array
{
$placeholders = [];
foreach ($this->set as $value) {
if ($value instanceof ScalarFunction) {
$placeholders = array_merge($placeholders, $value->buildParams());
} elseif (!\is_bool($value)) {
$placeholders[] = $value;
}
}
return $placeholders;
}
/**
* Set values for SET.
*
* @param array<string, scalar|ScalarFunction|null> $values array of values for SET
*
* @return $this
*/
public function set(array $values): object
{
if (empty($values)) {
return $this;
}
$this->set = $values;
return $this;
}
}