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
|
|
|
final class ParameterFormatter |
18
|
|
|
{ |
19
|
|
|
/** @var Connection */ |
20
|
|
|
private $connection; |
21
|
|
|
|
22
|
123 |
|
public function __construct(Connection $connection) |
23
|
|
|
{ |
24
|
123 |
|
$this->connection = $connection; |
25
|
123 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param mixed[] $params |
29
|
|
|
* @param mixed[] $types |
30
|
|
|
*/ |
31
|
37 |
|
public function formatParameters(array $params, array $types) : string |
32
|
|
|
{ |
33
|
37 |
|
if ($params === []) { |
34
|
26 |
|
return ''; |
35
|
|
|
} |
36
|
|
|
|
37
|
19 |
|
$formattedParameters = []; |
38
|
|
|
|
39
|
19 |
|
foreach ($params as $key => $value) { |
40
|
19 |
|
$type = $types[$key] ?? 'string'; |
41
|
|
|
|
42
|
19 |
|
$formattedParameter = '[' . $this->formatParameter($value, $type) . ']'; |
43
|
|
|
|
44
|
19 |
|
$formattedParameters[] = is_string($key) |
45
|
3 |
|
? sprintf(':%s => %s', $key, $formattedParameter) |
46
|
19 |
|
: $formattedParameter; |
47
|
|
|
} |
48
|
|
|
|
49
|
19 |
|
return sprintf('with parameters (%s)', implode(', ', $formattedParameters)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string|int $value |
54
|
|
|
* @param string|int $type |
55
|
|
|
* |
56
|
|
|
* @return string|int |
57
|
|
|
*/ |
58
|
19 |
|
private function formatParameter($value, $type) |
59
|
|
|
{ |
60
|
19 |
|
if (is_string($type) && Type::hasType($type)) { |
61
|
14 |
|
return Type::getType($type)->convertToDatabaseValue( |
62
|
14 |
|
$value, |
63
|
14 |
|
$this->connection->getDatabasePlatform() |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
6 |
|
return $this->parameterToString($value); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param int[]|bool[]|string[]|array|int|string|bool $value |
72
|
|
|
*/ |
73
|
6 |
|
private function parameterToString($value) : string |
74
|
|
|
{ |
75
|
6 |
|
if (is_array($value)) { |
76
|
|
|
return implode(', ', array_map(function ($value) : string { |
77
|
4 |
|
return $this->parameterToString($value); |
78
|
4 |
|
}, $value)); |
79
|
|
|
} |
80
|
|
|
|
81
|
6 |
|
if (is_int($value) || is_string($value)) { |
82
|
4 |
|
return (string) $value; |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
if (is_bool($value)) { |
|
|
|
|
86
|
2 |
|
return $value === true ? 'true' : 'false'; |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
return '?'; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|