|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
|
8
|
|
|
use Doctrine\DBAL\Types\Type; |
|
9
|
|
|
use function array_map; |
|
10
|
|
|
use function implode; |
|
11
|
|
|
use function is_array; |
|
12
|
|
|
use function is_bool; |
|
13
|
|
|
use function is_int; |
|
14
|
|
|
use function is_string; |
|
15
|
|
|
use function sprintf; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The ParameterFormatter class is responsible for formatting SQL query parameters to a string |
|
19
|
|
|
* for display output. |
|
20
|
|
|
* |
|
21
|
|
|
* @internal |
|
22
|
|
|
*/ |
|
23
|
|
|
final class ParameterFormatter implements ParameterFormatterInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var Connection */ |
|
26
|
|
|
private $connection; |
|
27
|
|
|
|
|
28
|
180 |
|
public function __construct(Connection $connection) |
|
29
|
|
|
{ |
|
30
|
180 |
|
$this->connection = $connection; |
|
31
|
180 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param mixed[] $params |
|
35
|
|
|
* @param mixed[] $types |
|
36
|
|
|
*/ |
|
37
|
39 |
|
public function formatParameters(array $params, array $types) : string |
|
38
|
|
|
{ |
|
39
|
39 |
|
if ($params === []) { |
|
40
|
27 |
|
return ''; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
20 |
|
$formattedParameters = []; |
|
44
|
|
|
|
|
45
|
20 |
|
foreach ($params as $key => $value) { |
|
46
|
20 |
|
$type = $types[$key] ?? 'string'; |
|
47
|
|
|
|
|
48
|
20 |
|
$formattedParameter = '[' . $this->formatParameter($value, $type) . ']'; |
|
49
|
|
|
|
|
50
|
20 |
|
$formattedParameters[] = is_string($key) |
|
51
|
3 |
|
? sprintf(':%s => %s', $key, $formattedParameter) |
|
52
|
20 |
|
: $formattedParameter; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
20 |
|
return sprintf('with parameters (%s)', implode(', ', $formattedParameters)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string|int $value |
|
60
|
|
|
* @param string|int $type |
|
61
|
|
|
* |
|
62
|
|
|
* @return string|int |
|
63
|
|
|
*/ |
|
64
|
20 |
|
private function formatParameter($value, $type) |
|
65
|
|
|
{ |
|
66
|
20 |
|
if (is_string($type) && Type::hasType($type)) { |
|
67
|
15 |
|
return Type::getType($type)->convertToDatabaseValue( |
|
68
|
15 |
|
$value, |
|
69
|
15 |
|
$this->connection->getDatabasePlatform() |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
6 |
|
return $this->parameterToString($value); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param int[]|bool[]|string[]|array|int|string|bool $value |
|
78
|
|
|
*/ |
|
79
|
6 |
|
private function parameterToString($value) : string |
|
80
|
|
|
{ |
|
81
|
6 |
|
if (is_array($value)) { |
|
82
|
|
|
return implode(', ', array_map(function ($value) : string { |
|
83
|
4 |
|
return $this->parameterToString($value); |
|
84
|
4 |
|
}, $value)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
6 |
|
if (is_int($value) || is_string($value)) { |
|
88
|
4 |
|
return (string) $value; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
3 |
|
if (is_bool($value)) { |
|
|
|
|
|
|
92
|
2 |
|
return $value === true ? 'true' : 'false'; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
return '?'; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|