1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace BenTools\Where\InsertQuery; |
5
|
|
|
|
6
|
|
|
class InsertQueryStringifier |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @param InsertQueryBuilder $query |
11
|
|
|
* @return array |
12
|
|
|
*/ |
13
|
|
|
private static function initBuild(InsertQueryBuilder $query): array |
14
|
|
|
{ |
15
|
|
|
return \array_merge([$query->mainKeyword], $query->flags); |
|
|
|
|
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param InsertQueryBuilder $insertQuery |
20
|
|
|
* @param array $parts |
21
|
|
|
*/ |
22
|
|
|
private static function buildTable(InsertQueryBuilder $insertQuery, array &$parts) |
23
|
|
|
{ |
24
|
|
|
if (null !== $insertQuery->table) { |
|
|
|
|
25
|
|
|
$parts[] = \sprintf('INTO %s', $insertQuery->escape($insertQuery->table)); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param InsertQueryBuilder $insertQuery |
31
|
|
|
* @param array $parts |
32
|
|
|
*/ |
33
|
|
|
private static function buildColumns(InsertQueryBuilder $insertQuery, array &$parts) |
34
|
|
|
{ |
35
|
|
|
$columns = $insertQuery->getColumns(); |
36
|
|
|
if (null !== $columns) { |
37
|
|
|
$parts[] = \sprintf('(%s)', \implode(', ', \array_map([$insertQuery, 'escape'], $columns))); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param InsertQueryBuilder $insertQuery |
43
|
|
|
* @param array $parts |
44
|
|
|
*/ |
45
|
|
|
private static function buildValues(InsertQueryBuilder $insertQuery, array &$parts) |
46
|
|
|
{ |
47
|
|
|
$columns = $insertQuery->getColumns(); |
48
|
|
|
|
49
|
|
|
$parts[] = 'VALUES'; |
50
|
|
|
|
51
|
|
|
$nbColumns = \count($columns); |
52
|
|
|
$nbValues = \count($insertQuery->values); |
|
|
|
|
53
|
|
|
$pattern = \sprintf('(%s)', \implode(', ', \array_fill(0, $nbColumns, '?'))); |
54
|
|
|
$valueParts = \array_fill(0, $nbValues, $pattern); |
55
|
|
|
|
56
|
|
|
$parts[] = \implode(', ', $valueParts); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param InsertQueryBuilder $insertQuery |
61
|
|
|
* @param array $parts |
62
|
|
|
*/ |
63
|
|
|
private static function buildDuplicateConditions(InsertQueryBuilder $insertQuery, array &$parts) |
64
|
|
|
{ |
65
|
|
|
$duplicateConditions = $insertQuery->onDuplicate; |
|
|
|
|
66
|
|
|
if ([] !== $duplicateConditions && null !== $duplicateConditions) { |
67
|
|
|
$parts[] = 'ON DUPLICATE KEY UPDATE'; |
68
|
|
|
$updateParts = []; |
69
|
|
|
\array_walk($duplicateConditions, function ($value, $key) use ($insertQuery, &$updateParts) { |
70
|
|
|
$updateParts[] = \sprintf('%s = %s', $insertQuery->escape($key), $value); |
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
$parts[] = \implode(', ', $updateParts); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param InsertQueryBuilder $insertQuery |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public static function stringify(InsertQueryBuilder $insertQuery): string |
82
|
|
|
{ |
83
|
|
|
$parts = self::initBuild($insertQuery); |
84
|
|
|
self::buildTable($insertQuery, $parts); |
85
|
|
|
self::buildColumns($insertQuery, $parts); |
86
|
|
|
self::buildValues($insertQuery, $parts); |
87
|
|
|
self::buildDuplicateConditions($insertQuery, $parts); |
88
|
|
|
return \implode(' ', $parts) . $insertQuery->end; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.