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
|
|
|
$parts = array_merge([$query->mainKeyword], $query->flags); |
|
|
|
|
16
|
|
|
return $parts; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param InsertQueryBuilder $insertQuery |
21
|
|
|
* @param array $parts |
22
|
|
|
*/ |
23
|
|
|
private static function buildTable(InsertQueryBuilder $insertQuery, array &$parts) |
24
|
|
|
{ |
25
|
|
|
if (null !== $insertQuery->table) { |
|
|
|
|
26
|
|
|
$parts[] = sprintf('INTO %s', $insertQuery->escape($insertQuery->table)); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param InsertQueryBuilder $insertQuery |
32
|
|
|
* @param array $parts |
33
|
|
|
*/ |
34
|
|
|
private static function buildColumns(InsertQueryBuilder $insertQuery, array &$parts) |
35
|
|
|
{ |
36
|
|
|
$columns = $insertQuery->getColumns(); |
37
|
|
|
if (null !== $columns) { |
38
|
|
|
$parts[] = sprintf('(%s)', implode(', ', array_map([$insertQuery, 'escape'], $columns))); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param InsertQueryBuilder $insertQuery |
44
|
|
|
* @param array $parts |
45
|
|
|
*/ |
46
|
|
|
private static function buildValues(InsertQueryBuilder $insertQuery, array &$parts) |
47
|
|
|
{ |
48
|
|
|
$columns = $insertQuery->getColumns(); |
49
|
|
|
|
50
|
|
|
$parts[] = 'VALUES'; |
51
|
|
|
|
52
|
|
|
$nbColumns = count($columns); |
53
|
|
|
$nbValues = count($insertQuery->values); |
|
|
|
|
54
|
|
|
$pattern = sprintf('(%s)', implode(', ', array_fill(0, $nbColumns, '?'))); |
55
|
|
|
$valueParts = array_fill(0, $nbValues, $pattern); |
56
|
|
|
|
57
|
|
|
$parts[] = implode(', ', $valueParts); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param InsertQueryBuilder $insertQuery |
62
|
|
|
* @param array $parts |
63
|
|
|
*/ |
64
|
|
|
private static function buildDuplicateConditions(InsertQueryBuilder $insertQuery, array &$parts) |
65
|
|
|
{ |
66
|
|
|
$duplicateConditions = $insertQuery->onDuplicate; |
|
|
|
|
67
|
|
|
if ([] !== $duplicateConditions && null !== $duplicateConditions) { |
68
|
|
|
$parts[] = 'ON DUPLICATE KEY UPDATE'; |
69
|
|
|
$updateParts = []; |
70
|
|
|
array_walk($duplicateConditions, function ($value, $key) use ($insertQuery, &$updateParts) { |
71
|
|
|
$updateParts[] = sprintf('%s = %s', $insertQuery->escape($key), $value); |
72
|
|
|
}); |
73
|
|
|
|
74
|
|
|
$parts[] = implode(', ', $updateParts); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param InsertQueryBuilder $insertQuery |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public static function stringify(InsertQueryBuilder $insertQuery): string |
83
|
|
|
{ |
84
|
|
|
$parts = self::initBuild($insertQuery); |
85
|
|
|
self::buildTable($insertQuery, $parts); |
86
|
|
|
self::buildColumns($insertQuery, $parts); |
87
|
|
|
self::buildValues($insertQuery, $parts); |
88
|
|
|
self::buildDuplicateConditions($insertQuery, $parts); |
89
|
|
|
return implode(' ', $parts) . $insertQuery->end; |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
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.